我正在使用这段普通代码打开/连接到蓝牙套接字。虽然它在所有设备上都能完美运行 - 仅在最新的 Android 4.2.1 Nexus 上,我收到此 IOException:“读取失败的套接字可能已关闭”。
我尝试重新配对设备,检查 UUID,一切都很好。如前所述,完全相同的代码适用于所有其他设备。目前我不知道还有什么地方可以看......
私有类 ConnectBluetoothThread 扩展 Thread { 私有最终 BluetoothSocket mmSocket; 私有最终蓝牙设备 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);
}