Hey guys!
Got a problem when I connect with a embedded device via bluetooth.
When I connect with this code:
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
}
I search for the device, find it and then connect it via the mmSocket.connect(). After that the request is sent to the embedded device in which i enter the PIN. After that the request is sent to the phone were I a notification says that a device is trying to pair etc.
Firstly, it shouldn’t be a notification! It should be a dialog and I
guess it’s not my fault.
Secondly why do I get the following exception about 20 seconds after I recieve my notification:
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
Found out that it’s the embedded device that has a 20 sec wait time before it closes the socket and sends a exception thus terminating the pair-process