我一直在阅读有关内容,可惜无法解决我的问题。我正在尝试仅将字符串发送到设备,但无法通过连接阶段。我尝试参考Google Doc Bluetooth信息以及他们的聊天应用程序示例,但无济于事。还引用了几个堆栈溢出问题1,2。以下是我使用linux时的连接代码片段以及hcidump的输出。任何帮助将不胜感激。
Android Connect线程:
private class ConnectThread extends Thread {
private final BluetoothDevice mmDevice;
private BluetoothSocket mmSocket;
public ConnectThread(BluetoothDevice device) {
Log.d(TAG, "ConnectThread");
BluetoothSocket temp = null;
Log.d(TAG, "Device returned :" + device);
mmDevice = device;
Log.d(TAG, "Device returned: " + device);
try {
//Use device as we are connecting to device
temp = device.createInsecureRfcommSocketToServiceRecord(device.getUuids()[0].getUuid());
} catch (IOException e) {
Log.e(TAG, "Error create failed.", e);
}
mmSocket = temp;
mState = STATE_CONNECTING;
}
public void run() {
Log.d(TAG, "Begin connect thread" + this);
mBluetoothAdapter.cancelDiscovery();
try {
mmSocket.connect();
//BREAKS ABOVE ON CONNECT as this is a blocking call that throws IO or returns
//connection
} catch (IOException connectException) {
Log.e(TAG, "Could not connect to device " + mmDevice.getName(), connectException);
try {
//Try catch was removed as is not needed
/*try {
Log.e("", "trying fallback...");
mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, 1);
Thread.sleep(500);
mmSocket.connect();
Log.e("", "Connected");
} catch (Exception e) {
Log.e(TAG, "Error on backup connection", e);
}*/
mmSocket.close();
} catch (IOException closeException) {
Log.e(TAG, "Could not close client socket", closeException);
}
connectionLost();
return;
}
connected(mmSocket, mmDevice); // Manges Coach socket in separate thread
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close the client socket", e);
}
}
}'
Linux终端上的HCI转储:
HCI Event: Connect Request (0x04) plen 10
bdaddr XXX class 0x5a020c type ACL
HCI Event: Command Status (0x0f) plen 4
Accept Connection Request (0x01|0x0009) status 0x00 ncmd 1
HCI Event: Connect Complete (0x03) plen 11
status 0x00 handle 256 bdaddr XXX type ACL encrypt 0x00
HCI Event: Command Status (0x0f) plen 4
Read Remote Supported Features (0x01|0x001b) status 0x00 ncmd 1
HCI Event: Read Remote Supported Features (0x0b) plen 11
status 0x00 handle 256
Features: 0xff 0xfe 0x8f 0xfe 0xd8 0x3f 0x5b 0x87
HCI Event: Command Status (0x0f) plen 4
Read Remote Extended Features (0x01|0x001c) status 0x00 ncmd 1
HCI Event: Read Remote Extended Features (0x23) plen 13
status 0x00 handle 256 page 1 max 1
Features: 0x03 0x00 0x00 0x00 0x00 0x00 0x00 0x00
HCI Event: Command Status (0x0f) plen 4
Remote Name Request (0x01|0x0019) status 0x00 ncmd 1
HCI Event: Remote Name Req Complete (0x07) plen 255
status 0x00 bdaddr XXX name 'XT1030'
HCI Event: Command Complete (0x0e) plen 10
Link Key Request Reply (0x01|0x000b) ncmd 1
status 0x00 bdaddr XXX
HCI Event: Encrypt Change (0x08) plen 4
status 0x00 handle 256 encrypt 0x01
HCI Event: Command Complete (0x0e) plen 7
Read Encryption Key Size (0x05|0x0008) ncmd 1
HCI Event: Disconn Complete (0x05) plen 4
status 0x00 handle 256 reason 0x13
Reason: Remote User Terminated Connection
代码引发的错误:
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:518)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:529)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:325)
at ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:518)
at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:495)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:329)
at com.example.myapplication.logic.bluetooth.Setup.BluetoothUtil$ConnectThread.run(BluetoothUtil.java:325)
编辑
该问题是由于该设备正在连接到不是蓝牙经典设备的BLE设备而导致的。上面的代码是正确的,不需要带备份连接的try catch。见上面的评论。