Xavier Gonsalves
Xavier Gonsalves

Reputation: 73

Android BLE advertiser transmits beacons even if stop advertising method is called, specifically when app returns from background

I can stop advertising if the app is in the foreground and I call the bleAdvertiser.stopAdvertising() method.
The problem occurs when my screen goes off and my app is still advertising. The app advertises when my screen is off, which is expected. But, when my app comes into the foreground and I press a button which does bleAdvertiser.stopAdvertising(), the BLE beacon still remains on. I tested it by scanning on a 3rd pert app called nRF Connect.
Just to be sure that it is not an nRF Connect app problem, I tested it with a hardware board which detects BLE beacons and got the same, which means that my app is still sending out beacons.
The beacon stops when I close/destroy the app (swipe sideways in tabs).
I do the following in my app:

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bleAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
advertiseSettings = new AdvertiseSettings.Builder() ....
bleAdvertiser.startAdvertising(advertiseSettings, advertisedata, advertisingCallback);

On button press for stopping the beacon I do:

bleAdvertiser.stopAdvertising(advertisingCallback);

Upvotes: 0

Views: 818

Answers (1)

davidgyoung
davidgyoung

Reputation: 64941

I suspect that the advertisingCallback variable is somehow not the same instance as when you started advertising the first time. If not, stopping will not work.

I suggest logging that instance both when you start and stop to look for any differences. It is easy to have Android app lifecycle callbacks alter variables like this unexpectedly, causing issues like this.

Upvotes: 2

Related Questions