开发者

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

开发者 https://www.devze.com 2023-02-01 00:18 出处:网络
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

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.bluetoot开发者_如何学运维hServerSocket.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.


We should close a bluetooth connection like this:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消