Android app unexpectedly quitted when I closed the bluetooth serversocket or bluetooth socket
- by Lewisou
The android app quitted without any warning and error when bluetoothServersocket.close() called in the main thread while bluetoothServersocket.accept(); was blocked for incoming connections in another thread.
Here is pieces of my code.
public void run() {
try {
bluetoothServerSocket = BluetoothAdapter.
getDefaultAdapter().listenUsingRfcommWithServiceRecord(LISTEN_NAME, LISTEN_UUID);
/* the thread blocked here for incoming connections */
bluetoothSocket = bluetoothServerSocket.accept();
...
} catch(Exception e) {} finally {
try {bluetoothServerSocket.close();} catch(Exception ex) {}
try {bluetoothSocket.close();} catch(Exception ex) {}
}
}
And in the activity.
public void onStop () {
try{thread.bluetoothSocket.close();} catch(Exception ex) {}
try{thread.bluetoothServerSocket.close();} catch(Exception ex) {}
super.onStop();
}
When I clicked the back button. The activity closed but after about one second the app quitted without any warning.
My android os version is 2.2. a HTC Desire device.