android ble advertise,android - BLE advertising ADVERTISE_FAILED_ALREADY_STARTED - Stack Overflow

I have a problem with my Galaxy S6 with Android 7.0 when I try to use BLE advertising. After a few times that I get onStartSuccess callback I start to get onStartFailure callback with error ADVERTISE_FAILED_ALREADY_STARTED and then it evolves into ADVERTISE_FAILED_TOO_MANY_ADVERTISERS. I had looked through the source code specificly at the startAdvertising method and found this code snippet:

if (mLeAdvertisers.containsKey(callback)) {

postStartFailure(callback, AdvertiseCallback.ADVERTISE_FAILED_ALREADY_STARTED);

return;

}

Using reflection I tried to get the mLeAdvertisers in my code just before calling startAdvertising to see if its size is greater than 0. Every time I check its size was always 0. Even when I checked it in my onStartFailure callback.

I didn't find any other place in the source code which could give me ADVERTISE_FAILED_ALREADY_STARTED error. Has anyone seen this error and knows how to deal with it?

This is how I start my advertising:

public void startAdvertise(String serviceUUID) {

if(bluetoothAdapter.isEnabled()) {

Log.e(TAG, "eddie startAdvertise: ");

Config config = Config.getInstance(mContext);

ParcelUuid pUuid = new ParcelUuid(UUID.fromString(serviceUUID));

AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();

dataBuilder.addServiceUuid(pUuid);

dataBuilder.setIncludeDeviceName(false);

dataBuilder.setIncludeTxPowerLevel(true);

int currentTime = (int) (System.currentTimeMillis() / 1000);

byte[] key = CryptoManager.getInstance(mContext).mySelf.generateEphemeralId(currentTime, BLEScannerManager.sGeoHash);

dataBuilder.addServiceData(pUuid, key);

AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();

settingsBuilder.setAdvertiseMode(config.getAdvertiseMode());

settingsBuilder.setTimeout((int) config.getAdvertiseDuration());

settingsBuilder.setTxPowerLevel(config.getAdvertiseTXPowerLevel());

settingsBuilder.setConnectable(false);

try {

Log.e(TAG, "startAdvertise: reflection");

Field f = bluetoothAdapter.getBluetoothLeAdvertiser().getClass().getDeclaredField("mLeAdvertisers");

f.setAccessible(true);

HashMap table = (HashMap) f.get(bluetoothAdapter.getBluetoothLeAdvertiser());

Log.e(TAG, "startAdvertise: size of mLeAdvertises is: " + table.keySet().size());

} catch (Exception e) {

e.printStackTrace();

}

bluetoothAdapter.getBluetoothLeAdvertiser().startAdvertising(settingsBuilder.build(), dataBuilder.build(), advertiseCallback);

}

}

This is how I stop advertising:

public void stopAdvertise() {

if(bluetoothAdapter.isEnabled()){

Log.e(TAG, "eddie stopAdvertise: " );

bluetoothAdapter.getBluetoothLeAdvertiser().stopAdvertising(advertiseCallback);

mEventListenerCallback.onEvent(ADVERTISING_STATUS, false);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值