蓝牙连接失败。 java.io.IOException:读取失败,socket可能关闭或超时,读取ret:-1

时间:2016-04-14 03:32:03

标签: android sockets bluetooth

这个问题困扰了我很久。我见过各种解决方案,包括IOException: read failed, socket might closed - Bluetooth on Android 4.3,但都不起作用。

主要观点: 1.粘合设备; 2.用uuid创建一个socket; 3. cancelDiscovery并连接。

日志列表如下:

04-14 11:12:53.044: W/BluetoothAdapter(22212): getBluetoothService() called with no BluetoothManagerCallback
04-14 11:12:53.044: W/bt-l2cap(2445): L2CA_ErtmConnectReq()  PSM: 0x0001  BDA: dc2c2600006c  p_ertm_info: 0x00000000 allowed:0x0 preferred:0
04-14 11:12:53.044: W/bt-l2cap(2445): L2CAP - st: CLOSED evt: 21
04-14 11:12:53.044: W/bt-l2cap(2445): L2CAP - st: CLOSED evt: 7
04-14 11:12:53.064: W/bt-l2cap(2445): L2CAP - st: W4_L2CAP_CON_RSP evt: 11
04-14 11:12:53.064: W/bt-l2cap(2445): L2CAP - st: CONFIG evt: 24
04-14 11:12:53.074: W/bt-l2cap(2445): L2CAP - st: CONFIG evt: 14
04-14 11:12:53.074: W/bt-l2cap(2445): L2CAP - st: CONFIG evt: 25
04-14 11:12:53.074: W/bt-l2cap(2445): L2CAP - st: CONFIG evt: 15
04-14 11:12:53.264: W/bt-sdp(2445): process_service_search_attr_rsp
04-14 11:12:53.264: W/bt-l2cap(2445): L2CA_DisconnectReq()  CID: 0x004a
04-14 11:12:53.274: W/bt-l2cap(2445): L2CAP - st: W4_L2CAP_DISC_RSP evt: 18
04-14 11:12:53.274: E/bt-btif(2445): DISCOVERY_COMP_EVT slot id:91, failed to find channle,                                       status:1, scn:0
04-14 11:12:53.274: W/bt-btif(2445): invalid rfc slot id: 91
04-14 11:12:53.274: W/System.err(22212): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
04-14 11:12:53.274: W/System.err(22212):    at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:574)
04-14 11:12:53.274: W/System.err(22212):    at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:585)
04-14 11:12:53.274: W/System.err(22212):    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:326)
04-14 11:12:53.274: W/System.err(22212):    at com.wormhole.bluetoothdemo.MainActivity$ListItemClickListener.onItemClick(MainActivity.java:152)
04-14 11:12:53.274: W/System.err(22212):    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
04-14 11:12:53.274: W/System.err(22212):    at android.widget.AbsListView.performItemClick(AbsListView.java:1115)
04-14 11:12:53.284: W/System.err(22212):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2928)
04-14 11:12:53.284: W/System.err(22212):    at android.widget.AbsListView$3.run(AbsListView.java:3691)
04-14 11:12:53.284: W/System.err(22212):    at android.os.Handler.handleCallback(Handler.java:733)
04-14 11:12:53.284: W/System.err(22212):    at android.os.Handler.dispatchMessage(Handler.java:95)
04-14 11:12:53.284: W/System.err(22212):    at android.os.Looper.loop(Looper.java:136)
04-14 11:12:53.284: W/System.err(22212):    at android.app.ActivityThread.main(ActivityThread.java:5072)
04-14 11:12:53.284: W/System.err(22212):    at java.lang.reflect.Method.invokeNative(Native Method)
04-14 11:12:53.294: W/System.err(22212):    at java.lang.reflect.Method.invoke(Method.java:515)
04-14 11:12:53.294: W/System.err(22212):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-14 11:12:53.294: W/System.err(22212):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
04-14 11:12:53.294: W/System.err(22212):    at dalvik.system.NativeStart.main(Native Method)

其他信息: 主设备是我的手机(android 4.4.4或android 5.1.1),从设备是HID设备,UUID是" 00001124-0000-1000-8000-00805f9b34fb"

感谢所有人,如果能提供帮助的话。

2 个答案:

答案 0 :(得分:1)

在我的情况下,系统附带的蓝牙设置可以正常工作。

所以,我看一下蓝牙设置的源代码。

将lib frameworks/base/packages/SettingsLib/src/com/android/settingslib/bluetooth复制到我的项目中。

使用reflect来调用hide类和函数或变量。

最后,它有效。

我希望这可以提供帮助。

答案 1 :(得分:0)

我的解决方案

 private void connect(BluetoothDevice bluetoothDevice) {
        try {
            int sdk = Build.VERSION.SDK_INT;
            if (sdk >= 10) {
                mBluetoothSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(BltContant.SPP_UUID);
            } else {
                mBluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(BltContant.SPP_UUID);
            }
            if (mBluetoothSocket != null) {
                APP.bluetoothSocket = mBluetoothSocket;
                if (bluetoothadapter.isDiscovering()) {
                    bluetoothadapter.cancelDiscovery();
                }
                if (!mBluetoothSocket.isConnected()) {
                    mBluetoothSocket.connect();
                }
                //EventBus.getDefault().post(new BluRxBean(connectsuccess, bluetoothDevice));
            }
        } catch (IOException connectException) {
            connectException.printStackTrace();
            try {
                Method m = bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
                mBluetoothSocket = (BluetoothSocket) m.invoke(bluetoothDevice, 1);
                mBluetoothSocket.connect();
            } catch (Exception e) {
                Log.e("BLUE", e.toString());
                try {
                    mBluetoothSocket.close();
                } catch (IOException ie) {
                    ie.printStackTrace();
                }
            }

        }

    }