1
votes

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.

1

1 Answers

1
votes

We should close a bluetooth connection like this:

try { synchronized (bluetoothServerSocket) {bluetoothServerSocket.close();}} catch(Exception ex) {}