大家好!
当我通过蓝牙连接嵌入式设备时遇到问题。当我连接到以下代码时:
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秒的等待时间并发送异常从而终止pair进程