At what point should BLE ads be stopped?
I have heard that it is recommended for peripherals to stop advertising when it is no longer waiting for new connections to conserve battery.
So, on my periphery, internally onConnectionStateChange
I call advertiser.stopAdvertising(advertiseCallback)
like this:
private final BluetoothGattServerCallback bluetoothServerCallback = new BluetoothGattServerCallback() {
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
Log.d(TAG, String.format("onConnectionStateChange status=%d, newState=%d", status, newState));
super.onConnectionStateChange(device, status, newState);
// advertiser should be turned off when connection state changes
// but the problem is - this breaks the client side service discovery
// so, I'm not sure what would be the right moment to stop advertising
advertiser.stopAdvertising(advertiseCallback);
}
On the client side, right after connecting and receiving onConnectionStateChange
with, BluetoothProfile.STATE_CONNECTED
I call
connectedPeripheralGatt.discoverServices()
and wait for the onServicesDiscovered
callback.
The problem is that if the periphery stops advertising, it onServicesDiscovered
gets a status code of 257 (which is a constant for the generic undefined GATT_FAILURE) and does not detect any services.
If I comment stopAdvertising
, the client side onServicesDiscovered
works as expected, but then the periphery continues to advertise even though I no longer need it.
How is it supposed to work then? How to stop ads when they are no longer needed?
Both devices are not paired / linked. I'm not sure yet what will happen with the paired devices - might onServicesDiscovered
work even when ads are stopped due to real MAC addresses, but I want it to work without pairing in the first place.
source to share
No one has answered this question yet
See similar questions:
or similar: