Android 连接到蓝牙设备失败

Android 连接到蓝牙设备失败,android,bluetooth,Android,Bluetooth,我有一个配对设备的列表视图,单击所选索引,我将连接到它 listView_pairedDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

我有一个配对设备的
列表视图
,单击所选索引,我将连接到它

listView_pairedDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                String deviceInfo = listAdapter_pairedDevices.getItem(position - 1);
                String lines[] = deviceInfo.split("\\r?\\n");
                String deviceAddress = lines[1];
                BluetoothDevice selectedDevice = btAdapter.getRemoteDevice(deviceAddress);
                btChatService.connect(selectedDevice, false);
            }
        });
我的logcat上一直有连接失败

09-01 16:11:32.085 3255-3255/com.example.matamatamata.mdpbtlayout D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
09-01 16:11:32.225 3255-3255/com.example.matamatamata.mdpbtlayout D/BluetoothChatService: connect to: C4:43:8F:98:9A:9A
09-01 16:11:32.225 3255-3255/com.example.matamatamata.mdpbtlayout D/BluetoothChatService: setState() 1 -> 2
09-01 16:11:32.225 3255-15464/com.example.matamatamata.mdpbtlayout I/BluetoothChatService: BEGIN mConnectThread SocketType:Insecure

                                                                                           [ 09-01 16:11:32.225  4143: 4210 E/         ]
                                                                                           ### ASSERT : external/bluetooth/bluedroid/main/../btif/src/btif_dm.c line 2025 unhandled search services event (6) ###
09-01 16:11:32.235 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
09-01 16:11:32.235 3255-15464/com.example.matamatamata.mdpbtlayout W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
09-01 16:11:32.235 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[54]}
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: close() in, this: android.bluetooth.BluetoothSocket@1dbcf50f, channel: -1, state: INIT
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@1dbcf50f, channel: -1, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@14b9199c, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@23459fa5mSocket: android.net.LocalSocket@19e76f7a impl:android.net.LocalSocketImpl@3dddd32b fd:FileDescriptor[54]
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: Closing mSocket: android.net.LocalSocket@19e76f7a impl:android.net.LocalSocketImpl@3dddd32b fd:FileDescriptor[54]
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothChatService: start
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: close() in, this: android.bluetooth.BluetoothSocket@1dbcf50f, channel: -1, state: CLOSED
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothChatService: setState() 2 -> 1
09-01 16:11:34.325 3255-15464/com.example.matamatamata.mdpbtlayout E/BluetoothChatService: Connection Fail

错误消息:“连接读取失败,套接字可能已关闭或超时,读取ret:-1”`

我将UUID更改为
00001101-0000-1000-8000-00805F9B34FB
,由于某种原因,该操作有效。我不太清楚为什么,可能是喜欢对此发表评论的人。

您的代码通过更改UUID开始工作的原因是您后来更改的UUID是serialport的UUID

无论何时,创建rfcomm服务器,您可以为其分配自己的UUID,也可以使用serialport的默认UUID(00001101-0000-1000-8000-00805F9B34FB)。现在,当您使用自己的UUID时,连接到服务器的客户端应用程序也应该使用与服务器相同的UUID,然后只建立连接

在您的情况下,您正在使用的客户端应用程序必须尝试连接到串行端口的默认UUID,因此当您更改UUID时,您的代码开始工作

09-01 16:11:32.085 3255-3255/com.example.matamatamata.mdpbtlayout D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
09-01 16:11:32.225 3255-3255/com.example.matamatamata.mdpbtlayout D/BluetoothChatService: connect to: C4:43:8F:98:9A:9A
09-01 16:11:32.225 3255-3255/com.example.matamatamata.mdpbtlayout D/BluetoothChatService: setState() 1 -> 2
09-01 16:11:32.225 3255-15464/com.example.matamatamata.mdpbtlayout I/BluetoothChatService: BEGIN mConnectThread SocketType:Insecure

                                                                                           [ 09-01 16:11:32.225  4143: 4210 E/         ]
                                                                                           ### ASSERT : external/bluetooth/bluedroid/main/../btif/src/btif_dm.c line 2025 unhandled search services event (6) ###
09-01 16:11:32.235 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
09-01 16:11:32.235 3255-15464/com.example.matamatamata.mdpbtlayout W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
09-01 16:11:32.235 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[54]}
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: close() in, this: android.bluetooth.BluetoothSocket@1dbcf50f, channel: -1, state: INIT
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@1dbcf50f, channel: -1, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@14b9199c, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@23459fa5mSocket: android.net.LocalSocket@19e76f7a impl:android.net.LocalSocketImpl@3dddd32b fd:FileDescriptor[54]
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: Closing mSocket: android.net.LocalSocket@19e76f7a impl:android.net.LocalSocketImpl@3dddd32b fd:FileDescriptor[54]
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothChatService: start
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: close() in, this: android.bluetooth.BluetoothSocket@1dbcf50f, channel: -1, state: CLOSED
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothChatService: setState() 2 -> 1
09-01 16:11:34.325 3255-15464/com.example.matamatamata.mdpbtlayout E/BluetoothChatService: Connection Fail