BLE广告失败

问题描述 投票:0回答:1

我正在将牛轧糖用于三星Galaxy S6。当我尝试使用广告时,会得到一些成功的回调,但是随后进入一个循环,我得到ADVERTISE_FAILED_TOO_MANY_ADVERTISERSADVERTISE_FAILED_ALREADY_STARTED甚至是ADVERTISE_FAILED_INTERNAL_ERROR。我在日志中注意到,它创建了一个AdvertiseManager的新实例,但限制为4,然后陷入失败的回调中。我尝试不时禁用蓝牙,然后再启用它。似乎没有效果。

这些是日志:(一旦达到4,它就会开始连续失败)

2020-06-08 11:00:18.125 22871-22935/? D/BtGatt.AdvertiseManager: number of adv instance running = 0
2020-06-08 11:04:18.995 22871-22935/? D/BtGatt.AdvertiseManager: number of adv instance running = 1
2020-06-08 11:08:19.988 22871-22935/? D/BtGatt.AdvertiseManager: number of adv instance running = 2
2020-06-08 11:09:20.275 22871-22935/? D/BtGatt.AdvertiseManager: number of adv instance running = 3
2020-06-08 11:13:21.082 22871-22935/? D/BtGatt.AdvertiseManager: number of adv instance running = 4
2020-06-08 11:15:21.597 22871-22935/? D/BtGatt.AdvertiseManager: number of adv instance running = 4
2020-06-08 11:18:22.089 22871-22935/? D/BtGatt.AdvertiseManager: number of adv instance running = 4
2020-06-08 11:19:22.283 22871-22935/? D/BtGatt.AdvertiseManager: number of adv instance running = 4
2020-06-08 11:20:22.634 22871-22935/? D/BtGatt.AdvertiseManager: number of adv instance running = 4
2020-06-08 11:23:23.173 22871-22935/? D/BtGatt.AdvertiseManager: number of adv instance running = 4
2020-06-08 11:24:23.463 22871-22935/? D/BtGatt.AdvertiseManager: number of adv instance running = 4
2020-06-08 11:25:23.794 22871-22935/? D/BtGatt.AdvertiseManager: number of adv instance running = 4

这是我开始做广告的方式:

public void startAdvertise(String serviceUUID) {
if(advertisingCounter == 0 && !bluetoothAdapter.isEnabled())
{
    bluetoothAdapter.enable();
    Log.e(TAG, "eddie startAdvertise: turning on bluetooth"); // TODO: remove
}
if(advertisingCounter == 3)
{
    bluetoothAdapter.disable();
    advertisingCounter = 0;
    Log.e(TAG, "eddie startAdvertise: turning off bluetooth"); // TODO: remove
    return;
}
if(bluetoothAdapter.isEnabled()) {

    Log.e(TAG, "eddie startAdvertise: " + advertisingCounter); // TODO: remove
    advertisingCounter ++;

    if(advertiser == null)
        advertiser = bluetoothAdapter.getBluetoothLeAdvertiser(); // if we turned the bluetooth on while the service is running

    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);

    if(advertiser != null)
        advertiser.startAdvertising(settingsBuilder.build(), dataBuilder.build(), advertiseCallback);
}
}

有人遇到过类似的事情吗?

android bluetooth-lowenergy android-bluetooth
1个回答
0
投票

Android设备使用的蓝牙芯片具有数量有限的BLE广告,它们可以同时处于活动状态-这是硬件的限制。这是全球资源,因此其他应用也可能正在使用某些广告客户位置。较新的设备往往具有更多的广告位。我的Pixel 3a超过10。但是较旧的华为P9 Lite只有两个。

© www.soinside.com 2019 - 2024. All rights reserved.