Reputation: 185
I'm in desperate need of a solution to this issue, for some reason on another device i have tested my bluetooth app on, when i close the app (onDestroy()) and reenter it the bluetooth connections fail. The only solution to this currently is to turn bluetooth off and on for the device.
The code i use is more or less the bluetoothchat sample for android. I create 2 bt connections to a device previously selected.
I then communicate back and forth with these sockets using inputstream and output stream.
When my app is destroyed - i close input and output and bluetooth sockets, I even then kill the process (found some code on here) but when i go back into the app the connection fails.
Can anyone offer any assistance from what i said so far? My code is quite a lot at the moment so wouldnt know what to paste but the bt connection is basically using the classes from the sample and then passing these created sockets to my other classes.
Thanks in advance
Upvotes: 6
Views: 2161
Reputation: 3606
As Gaks mentioned, your issue may involve onResume()/onPause(). Are you absolutely certain that your onDestroy cleanup is called (as in, are you logging and/or watching the logcat)?
If your app isn't persistent, it might be better to do cleanup when the app is no longer being used (onPause).
Also, if you have two connections is there anything special you need to do to close both of them? Just guessing on that one.
Upvotes: 0
Reputation: 3499
The best way to use onDestroy is in following manner, hope you are doing something similar.
@Override
protected void onDestroy() {
if (localBT != null) {
localBT.close();
}
super.onDestroy();
}
Upvotes: 3