我正在使用这段正常的代码打开/连接蓝牙插座。虽然它在所有设备上都能完美运行 - 只有最新的Nexus和Android 4.2.1才会出现这种IOException:“读取失败的套接字可能会关闭”。
我尝试重新配对设备,检查了UUID,一切都很好。如上所述,完全相同的代码适用于所有其他设备。目前我没有想到的其他地方......
私有类ConnectBluetoothThread扩展Thread { 私人最终BluetoothSocket mmSocket; 私人最终的BluetoothDevice mmDevice;
public ConnectBluetoothThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
try {
tmp = mmDevice
.createInsecureRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
...other code
}
mmSocket = tmp;
}
public void run() {
mBluetoothAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
mmSocket.connect(); // <=== this is where the IOException is beeing raised!!!!
} catch (IOException e) {
Log.e(this.getClass().getSimpleName(),
" -> FAILED:", e);
connectionFailed(e);
// Close the socket
try {
mmSocket.close();
} catch (IOException e2) {
}
return;
}
// Reset the ConnectThread because we're done
synchronized (InterBT.this) {
mConnectBluetoothThread = null;
}
// Start the connected thread
connected(mmSocket, mmDevice);
}