Android app unexpectedly quitted when I closed the bluetooth serversocket or bluetooth socket

Posted by Lewisou on Stack Overflow See other posts from Stack Overflow or by Lewisou
Published on 2010-12-26T04:13:18Z Indexed on 2011/01/02 9:53 UTC
Read the original article Hit count: 336

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about android

Related posts about bluetooth