Android 蓝牙连接超时过快? 嘿,伙计们!
通过蓝牙连接嵌入式设备时出现问题。 当我使用此代码连接时:Android 蓝牙连接超时过快? 嘿,伙计们!,android,timeout,connection,bluetooth,Android,Timeout,Connection,Bluetooth,通过蓝牙连接嵌入式设备时出现问题。 当我使用此代码连接时: public void run() { Looper.prepare(); try { // Connect the device through the socket. This will block // until it succeeds or throws an exception mmSocket.connect(
public void run() {
Looper.prepare();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
if(!cancel){
toMain = threadhandler.obtainMessage();
toMain.arg1 = 1;
threadhandler.sendMessage(toMain);
}
} catch (SecurityException e) {
if(!cancel){
Log.e("SecurityExcep", "Oh noes" , e);
toMain = threadhandler.obtainMessage();
toMain.arg1 = 2;
threadhandler.sendMessage(toMain);
Log.w("MESSAGE", e.getMessage());
}
}catch (IOException e) {
//Bad connection, let's get the hell outta here
try {
Log.e("IOExcep", "Oh noes" , e);
Log.w("MESSAGE", e.getMessage());
if(!cancel){
mmSocket.close();
toMain = threadhandler.obtainMessage();
toMain.arg1 = 2;
threadhandler.sendMessage(toMain);
}
return;
} catch (IOException e1) {
Log.e("IOExcep2", "Oh noes" , e);
}
}
try {
mmSocket.close();
} catch (IOException e) {
Log.d("CONNECT_CONSTRUCTOR", "Unable to close the socket", e);
}
if(!cancel){
toMain = threadhandler.obtainMessage();
toMain.arg1 = 3;
threadhandler.sendMessage(toMain);
}
Looper.loop();
return;
// Now it should be paired.. only thing to do now is let the user commit to the rest
}
我搜索设备,找到它,然后通过mmSocket.connect进行连接。之后,请求被发送到我输入PIN的嵌入式设备。在这之后,请求被发送到手机,我收到一个通知,说设备正在尝试配对等
首先,它不应该是一个通知!这应该是一个对话,我
我想这不是我的错
其次,为什么在收到通知后20秒左右会出现以下异常:
07-28 11:13:00.537: ERROR/IOExcep(1064): Oh noes
07-28 11:13:00.537: ERROR/IOExcep(1064): java.io.IOException: Connection timed out
07-28 11:13:00.537: ERROR/IOExcep(1064): at android.bluetooth.BluetoothSocket.connectNative(Native Method)
07-28 11:13:00.537: ERROR/IOExcep(1064): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:204)
07-28 11:13:00.537: ERROR/IOExcep(1064): at com.se.mainActivity.ConnectThread.run(ConnectThread.java:61)
07-28 11:13:00.537: WARN/MESSAGE(1064): Connection timed out
07-28 11:13:00.537: INFO/MESSAGEHANDLER(1064): Message 2 recieved, on ConnectThread
07-28 11:13:00.567: ERROR/BluetoothEventLoop.cpp(234): event_filter: Received signal org.bluez.Adapter:PropertyChanged from /org/bluez/377/hci0
07-28 11:13:00.567: ERROR/BluetoothEventLoop.cpp(234): event_filter: Received signal org.bluez.Adapter:DeviceRemoved from /org/bluez/377/hci0
07-28 11:13:00.567: DEBUG/BluetoothService(234): 00:0E:9F:88:A3:C1 bond state 11 -> 10 (9)
07-28 11:13:00.577: VERBOSE/BluetoothEventRedirector(1111): Received android.bluetooth.device.action.BOND_STATE_CHANGED
07-28 11:13:00.577: WARN/CachedBluetoothDeviceManager(1111): showUnbondMessage: Not displaying any message for reason:9
07-28 11:13:00.577: INFO/BluetoothEventLoop.cpp(234): agent_event_filter: Received method org.bluez.Agent:Cancel
07-28 11:13:00.627: VERBOSE/BluetoothEventRedirector(1111): Received android.bluetooth.device.action.PAIRING_CANCEL
发现是嵌入式设备在关闭套接字之前有20秒的等待时间,并发送异常,从而终止配对过程