Android app does not release Bluetooth properly when exiting

I am desperate for a solution to this problem, for some reason on another device on which I tested the bluetooth application when I close the application (onDestroy ()) and reconnect it to the bluetooth connections. The only solution for this is to turn Bluetooth off and on for the device.

The code I use is more or less a bluetoothchat example for Android. I create 2 bt connections to a previously selected device.

Then I communicate with these sockets using input and output streams.

When my application is destroyed - I close the input and output data and bluetooth sockets, I even kill the process (I found some code here), but when I return to the application, the connection will fail.

Can anyone offer any help from what I said so far? My code is pretty much right now, so I didn't know what to insert, but the bt connection basically uses the classes from the sample and then passes these created sockets to my other classes.

Thanks in advance

+6
source share
2 answers

The best way to use onDestroy is as follows, hope you do something similar.

@Override protected void onDestroy() { if (localBT != null) { localBT.close(); } super.onDestroy(); } 
+3
source

As Gax mentioned, your problem might include onResume () / onPause (). Are you absolutely sure that onDestroy cleanup is called (for example, you register and / or view the logarithm)?

If your application is not permanent, it would be better to perform a cleanup if the application is no longer in use (onPause).

Also, if you have two connections, is there something special you need to do to close both of them? Just guessing at that.

0
source

Source: https://habr.com/ru/post/895135/


All Articles