gpt4 book ai didi

Android Lollipop BLE 外设 : calling BluetoothLeAdvertiser. stopAdvertising(AdvertiseCallback) 从连接的中央断开

转载 作者:行者123 更新时间:2023-11-29 01:34:18 26 4
gpt4 key购买 nike

我希望在中心连接(并订阅特定特征)时停止广告:

private final BluetoothGattServerCallback bleCallbacks = new BluetoothGattServerCallback() {
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value){
Log.v(DEBUG_TAG, "onDescriptorWriteRequest()...");

BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
Log.v(DEBUG_TAG, "----- characteristic: " + characteristic.getUuid().toString());

if (characteristic.equals(peripheralCharacteristic)){
descriptor.setValue(value);
if (bluetoothGattServer.sendResponse(device,
requestId,
BluetoothGatt.GATT_SUCCESS,
offset,
value)){
central = device;
stopAdvertising(); //causes disconnection
return;
}
}

bluetoothGattServer.sendResponse(device,
requestId,
BluetoothGatt.GATT_WRITE_NOT_PERMITTED,
0,
null);
}
...
}

private void stopAdvertising(){
if (bluetoothLeAdvertiser != null) {
bluetoothLeAdvertiser.stopAdvertising(advertiseCallback);
}
}

在调用 stopAdvertising() 后,中央和外围断开连接来自 logcat:

04-01 11:26:06.179 7068-7085/package.Class﹕ onDescriptorWriteRequest()...

04-01 11:26:06.179 7068-7085/package.Class﹕ ----- characteristic: 80a1a1a5-8b5b-e88b-9d24-2e609654b852

04-01 11:26:06.207 7068-7085/package D/BluetoothGattServer﹕ onServerConnectionState() - status=0 serverIf=5 device=00:07:80:2F:0F:A2

随着 stopAdvertising() 的评论,与中央的连接(和通信)继续。

有没有人在 Android 的 BLE 外设实现中遇到过这个问题?在 iOS 中,没有这样的问题。

最佳答案

进入连接后不需要调用stopAdvertising

来自 LE Controller 的链路层有 5 种状态:“空闲”、“广告”、“扫描”、“启动”和“连接”。

当您做广告时,您处于“广告”状态。当它连接时,它会进入“已连接”状态。

最有可能的是,stopAdvertising 方法假定您在调用时处于“广告”状态,并且在不检查这一点的情况下执行您在“广告”中调用它时应该执行的操作:它进入“空闲”状态。

因此,当您调用它时,无论当前状态如何,LL 都会进入“空闲”状态。

这似乎是 Android 的 BLE 主机堆栈中的错误。当您在“已连接”状态下调用 stopAdvertising 时,正确的行为应该是返回错误代码(例如“此命令的状态无效”)或直接忽略。

关于Android Lollipop BLE 外设 : calling BluetoothLeAdvertiser. stopAdvertising(AdvertiseCallback) 从连接的中央断开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29398515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com