Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BluetoothAdapter causes activity leak

I have an activity that listens for a bluetooth headset because it plays audio. In the activity's onCreate() method, I register the listener via the adapter:

mBluetoothAdapter = BluetoothAdapter..getDefaultAdapter();
if (mBluetoothAdapter != null) 
    mBluetoothAdapter.getProfileProxy(getApplicationContext(), mBluetoothProfileServiceListener,BluetoothProfile.HEADSET);

Then in onDestroy(), I close the proxy:

mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);

If I open and close the activity several times without connecting a bluetooth headset, all of the activities remain in memory. Using a memory dump, I can see that all of them are being held by a single reference from mServiceListener in android.bluetooth.BluetoothHeadset.

I can't find anything in the developer docs about how to remove the listener in order to release the activity. Is there any way to remove the activity as a listener so that the memory is released?

like image 373
Todd J. Avatar asked Jan 25 '26 17:01

Todd J.


1 Answers

It could possibly be better if you use Bluetooth API from your own Service as the Activity can be destroyed any time. I think this will take care of the problem, if any, because there will be only one Service. The service could pass messages to activities using Handler.

like image 73
Alexander Kulyakhtin Avatar answered Jan 27 '26 06:01

Alexander Kulyakhtin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!