Wednesday, 17 October 2018

Bluetooth (BLE) bluetoothLeAdvertiser.startAdvertising throws error code 2 (ADVERTISE_FAILED_TOO_MANY_ADVERTISERS)

I have a Samsung SM-G950F which supports BLE advertising but after a few advertisements, it starts throwing error code 2 (ADVERTISE_FAILED_TOO_MANY_ADVERTISERS) in which the android documentation says "Failed to start advertising because no advertising instance is available." and the only way to make it work again for a while is to restart the device. Another device, the HTC 10 has a similar issue, it works just fine at the start and eventually it ends up slowly missing say 1 in every ten or 20 transmissions but quickly degrades to sending say 1 in every 50. Finally, the device needs restarting to make the app work again.

My use case is as follows:

  1. call bluetoothLeAdvertiser.startAdvertising(...)
  2. Call that method for say approximately 100 times every 300ms (works just fine until a certain point)
  3. Then at one point the AdvertiseCallback onStartFailure(int errorCode) starts getting called and returns error code 2
  4. restart the phone (closing the Bluetooth and turning back or closing the app does not fix the issue)
  5. repeat the process

Is this an issue specifically for Samsung S8 device or am I doing something wrong with my implementation? is there a way to clear/reset the advertisers programmatically?

Also, if you are aware of any other devices with a similar issue it would be nice if you could share to help others be aware of them.

Note: the same code works just fine on my Samsung S5 SM-G900F running android 6.0.1

code snippets

public void startAdvertising() {
    // making sure that the advertiser object was initialised, it is null if the user opened the app
    // without having the bluetooth enable
    if (bluetoothLeAdvertiser == null) {
        bluetoothLeAdvertiser = bluetoothChecker.getBluetoothAdapter().getBluetoothLeAdvertiser();

        // if it's still null it means the bluetooth of the device is off
        if (bluetoothLeAdvertiser == null) {
            bluetoothLeAdvertiserWrapperCallback.onBleAdvertiseIsNull();
            return;
        }
    }

    stopAdvertising();
    bluetoothLeAdvertiser.startAdvertising(advertiseSettings, advertiseData, bleAdvertiseCallback);
}

and

public void stopAdvertising() {
    isAdvertising = false;
    bluetoothLeAdvertiser.stopAdvertising(bleAdvertiseCallback);
    bluetoothLeAdvertiserWrapperCallback.onBleAdvertiseStoppedOrTimeout();
}



from Bluetooth (BLE) bluetoothLeAdvertiser.startAdvertising throws error code 2 (ADVERTISE_FAILED_TOO_MANY_ADVERTISERS)

1 comment: