BluetoothLE startAdvertising:设置更改无效

时间:2015-04-27 11:08:20

标签: android bluetooth-lowenergy android-bluetooth

我正在开发一个Cordova插件,以便在我的Ionic App中使用API​​ 21中引入的新Android BluetoothLE API。到目前为止一切正常。我可以扫描设备,我也可以通过BLE成功开始广告。

对于广告,我调用startAdvertising并传递我从JSONObject转换的相应AdvertiseSettings。问题是,一旦我开始使用这些设置做广告,我就无法在之后使用不同的设置开始广告。 换句话说,startAdvertising将始终使用收到的第一个AdvertiseSettings当我重新编译应用并在设备上更新时,设置甚至会保留。只有通过禁用和启用蓝牙才能重置设置。但这并不是我每次都想做的事情。

以下是我如何开始在代码中做广告:

// ... check if advertising is supported 

// start advertising in new thread
cordova.getThreadPool().execute(new Runnable() {
    public void run() {

        AdvertiseSettings advertiseSettings = getAdvertiseSettings(settingsJSON);
        AdvertiseData advertiseData = getAdvertiseData(advertiseDataJSON);
        AdvertiseCallback advertiseCallback = getAdvertiseCallback();

        Log.w(LOG_TAG, "'startAdvertising' settings before call: "+advertiseSettings.toString() );

        bluetoothLeAdvertiser.startAdvertising(advertiseSettings, advertiseData, advertiseCallback);
    }
});

如您所见,我将传递的advertiseSettings记录到logcat。要仔细检查哪些设置有效,我还会在调用onStartSuccess时记录它们:

advertiseCallback = new AdvertiseCallback() {
    @Override
    public void onStartSuccess(AdvertiseSettings settingsInEffect) {
        advertising = true;

        // ... some output to the JavaScript interface

        Log.i(LOG_TAG, "'startAdvertising' result: " + settingsInEffect.toString() );

    }
    // ... onStartFailure
}

当我尝试以不同的超时开始广告时,这是输出:

I/BleAdvertising_Plugin(19094): initializing BleAdvertising plugin...
I/BleAdvertising_Plugin(19094): 'disableBluetooth' result: true
I/BleAdvertising_Plugin(19094): 'enableBluetooth' result: true
W/BleAdvertising_Plugin(19094): 'startAdvertising' settings before call: Settings [mAdvertiseMode=1, mAdvertiseTxPowerLevel=2, mAdvertiseConnectable=false, mAdvertiseTimeoutMillis=5000]
I/BleAdvertising_Plugin(19094): 'startAdvertising' result: Settings [mAdvertiseMode=1, mAdvertiseTxPowerLevel=2, mAdvertiseConnectable=false, mAdvertiseTimeoutMillis=5000]
I/BleAdvertising_Plugin(19094): 'startAdvertising' result: Stopped advertising after specified time.
W/BleAdvertising_Plugin(19094): 'startAdvertising' settings before call: Settings [mAdvertiseMode=1, mAdvertiseTxPowerLevel=2, mAdvertiseConnectable=false, mAdvertiseTimeoutMillis=6000]
I/BleAdvertising_Plugin(19094): 'startAdvertising' result: Settings [mAdvertiseMode=1, mAdvertiseTxPowerLevel=2, mAdvertiseConnectable=false, mAdvertiseTimeoutMillis=5000]
I/BleAdvertising_Plugin(19094): 'startAdvertising' result: Stopped advertising after specified time.

重新启动蓝牙后,我使用mAdvertiseTimeoutMillis=5000设置了设置,settingsInEffect的日志返回值5000,这与预期一致。但是我设置了超时6000的新设置,而settingsInEffect仍然有5000.后者也是,该设备确实做了什么(Nexus 9,Android 5.0.1) - 它在5000ms后停止广告不是6000ms。

我在这里做错了吗?这是一个错误吗?或者实际上Android如何管理广告?

一旦一切正常,我想开源这个插件,我会提到帮助我的每个人。谢谢!

0 个答案:

没有答案