Chen Zarfati
Chen Zarfati

Reputation: 23

Android 4.5 L - BLE Advertising error

while trying to implement a simple advertising app I manage to start the advertising process but when try to stop advertising, I receive an error with an integer value of 4. - attached is the implementation of the advertiseStopCallBack which called when stop advertising. It seems that using two different callback methods for start/stop advertising makes an error, can someone explain why?

private AdvertiseCallback advertiseStopCallback = new AdvertiseCallback() {
    @Override
    public void onSuccess(AdvertiseSettings advertiseSettings) {
        String successMsg = "Advertisement stop command attempt successful";
        Log.d(TAG, successMsg);
    }

    @Override
    public void onFailure(int i) {
        String failMsg = "Advertisement stop command attempt failed: " + i;
        Log.e(TAG, failMsg);
    }
};
 private AdvertiseCallback advertiseStopCallback = new AdvertiseCallback() {
    @Override
    public void onSuccess(AdvertiseSettings advertiseSettings) {
        String successMsg = "Advertisement stop command attempt successful";
        Log.d(TAG, successMsg);
    }

    @Override
    public void onFailure(int i) {
        String failMsg = "Advertisement stop command attempt failed: " + i;
        Log.e(TAG, failMsg);
    }
};

Upvotes: 1

Views: 725

Answers (1)

Torrence
Torrence

Reputation: 448

From the Android L SDK reference, error with value 4 is:

ADVERTISE_FAILED_NOT_STARTED

And its meaning is:

ADVERTISE_FAILED_NOT_STARTED

Fails to stop advertising as the advertising is not started.

Constant Value: 4 (0x00000004)

A callback object should be set when BluetoothAdvertiser starts advertising data (two approaches to start), and when stopAdvertising() is called, it will remove the (Observer) relationship between the BluetoothAdvertiser and the corresponding callback object. If you startAdvertising() with callback1, and stopAdvertising() with callback2, the system find callback2 is not related to this BluetoothAdvertiser object, it will lead to the ADVERTISE_FAILED_NOT_STARTED failure.

Actually you could set multiple callbacks for a single BluetoothAdvertiser, and make sure you stop all the callbacks when disable advertising function.

Upvotes: 1

Related Questions