安卓蓝牙插槽连接错误

问题描述:

我想通过蓝牙从Android设备连接到HC-06。我设法让它在使用之前能够正常工作:安卓蓝牙插槽连接错误

bluetoothSocket = (BluetoothSocket) bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(this.bluetoothDevice,1); 

问题是它只能工作一次。当我断开并关闭输入和输出流,然后插座,然后尝试重新连接它给了我这个错误:

D/BluetoothSocket: connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[55]} 
D/BluetoothSocket: close() in, this: [email protected], channel: 1, state: INIT 
D/BluetoothSocket: close() this: [email protected], channel: 1, mSocketIS: [email protected], mSocketOS: [email protected]: [email protected] impl:[email protected] fd:FileDescriptor[55] 
D/BluetoothSocket: Closing mSocket: [email protected] impl:[email protected] fd:FileDescriptor[55] 
W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1 
W/System.err:  at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:739) 
W/System.err:  at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:716) 
W/System.err:  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:482) 
W/System.err:  at com.example.work.dmm.clientBluetoothConnection.run(clientBluetoothConnection.java:65) 

我试图寻找问题,发现各岗位说createRfcommSocketToServiceRecord()应代替使用。我试过了:

ParcelUuid[] uuids = bluetoothDevice.getUuids(); 
UUID uuid = uuids[0].getUuid(); 
bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid); 

但是这根本不起作用。我不知道该从哪里出发,所以任何帮助表示赞赏。

也有同样的问题,当断开连接,然后重新连接我得到一个错误,但在我的情况下有一个广播接收器接收蓝牙适配器切换回来时的行动。这是什么干扰适配器完全切换回来,所以我删除了代码的一部分,它的工作原理...断开连接后重新连接它工作正常。