bluetooth BluetoothSocket.connect() thread. how to close this thread
- by Hia
I am trying to make an android app that makes connection with bluetooth device. It works fine but when I call BluetoothSocket.connect() and it is not able to connect to devise its blocking. The thread and does not throw any exception. So when I try to close application while connect() is running its not responding. How can I cancel it? Used BluetoothSocket.close() in ... but still its not working for me.
protected void simpleComm(Integer port) {
// The documents tell us to cancel the discovery process.
try {
Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
mmSocket = (BluetoothSocket) m.invoke(mmDevice, port);
mmSocket.connect(); // <== blocks untill is not connected
Log.d(TAG, " connection success===");
}catch(Exception e){
if (!abort) {
connectionFailed();
// Close the socket
try {
mmSocket.close();
// Start the service over to restart listening mode
BluetoothService.this.start();
} catch (IOException e2) {
Log.e(TAG,"unable to close() socket during connection failure", e2);
}
}
return;
}
}
public void cancel() {
try {
abort = true;
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}