gpt4 book ai didi

android - BLE 通知订阅获得 133

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:57:22 24 4
gpt4 key购买 nike

1 部 Android 手机 (N5X 6.0.1) 正在运行 BLE 服务器,另一部 (N5X O) 正在订阅。可以启用特性通知,但是,在写入描述符部分,我一直收到 133。

Server.java

private void createServer() {
bluetoothGattServer = bluetoothManager.openGattServer(this, serverCallback);
BluetoothGattService service = new BluetoothGattService(Constants.SERVICE,
BluetoothGattService.SERVICE_TYPE_PRIMARY);

characteristic =
new BluetoothGattCharacteristic(Constants.CHARACTERISTIC,
BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);

// public static UUID DESCRIPTOR = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
characteristic.addDescriptor(new BluetoothGattDescriptor(Constants.DESCRIPTOR,
BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE));

characteristic.setWriteType(
BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);

service.addCharacteristic(characteristic);
bluetoothGattServer.addService(service);
}

private BluetoothGattServerCallback serverCallback = new BluetoothGattServerCallback() {
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
super.onConnectionStateChange(device, status, newState);
Log.d(TAG, "onConnectionStateChange " + device.getName() + " " + status + " " + newState);
if (newState == BluetoothGatt.STATE_CONNECTED) {
bluetoothDevice = device;
} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
bluetoothGattServer.cancelConnection(bluetoothDevice);
bluetoothGattServer.close();
}
}
};

private void sendData(String message) {
characteristic.setValue(message);
bluetoothGattServer.notifyCharacteristicChanged(bluetoothDevice, characteristic, true);
}

所有其他 UUID 都是从 here 创建的.

客户端.java

device.establishConnection(false)
.flatMap(bleConnection -> bleConnection.setupNotification(Constants.CHARACTERISTIC))
.flatMap(onNotificationReceived -> onNotificationReceived)
.subscribe(data -> {
Log.d(TAG, "data: " + data);
}, throwable -> {
Log.d(TAG, "data error " + throwable);
});

logcat

05-15 15:26:50.097 D/BluetoothGatt: setCharacteristicNotification() - uuid: 8d7dda32-3759-11e7-a919-92ebcb67fe33 enable: true
05-15 15:26:50.105 D/RxBle#Radio: QUEUED RxBleRadioOperationDescriptorWrite(60042487)
05-15 15:26:50.110 D/RxBle#Radio: FINISHED RxBleRadioOperationServicesDiscover(231218312)
05-15 15:26:50.112 D/RxBle#Radio: STARTED RxBleRadioOperationDescriptorWrite(60042487)
05-15 15:27:20.119 D/RxBle#Radio: FINISHED RxBleRadioOperationDescriptorWrite(60042487)
05-15 15:27:20.121 D/BluetoothGatt: setCharacteristicNotification() - uuid: 8d7dda32-3759-11e7-a919-92ebcb67fe33 enable: false
05-15 15:27:20.126 D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=133
05-15 15:27:20.129 D/BLE: data error BleGattDescriptorException{macAddress=42:EE:5A:C6:C1:F0, status=133 (0x85 -> https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.1.0_r1/stack/include/gatt_api.h), bleGattOperationType=BleGattOperation{description='DESCRIPTOR_WRITE'}}

注意:如果我使用的是原生 Android API,则无需写入描述符即可订阅和接收通知。

更新:不过有趣的是,当写入描述符过程正在进行时(它需要大约 30 秒才能返回错误),我能够接收到 onCharacteristicChanged

update2: 添加回调并写入特征代码

最佳答案

蓝牙核心规范指出,如果一个特性支持通知,它应该包含一个客户端特性配置描述符,并且仅当 CCC 描述符将被写入一个正确的值时才开始通知。

在您的配置中似乎有一个问题,表现为 status = 133。看来您在为 characteristic 设置属性时可能犯了一个错误。我假设您希望拥有可以读取、写入和设置通知的特性 - 在这种情况下它看起来像:

characteristic =
new BluetoothGattCharacteristic(Constants.CHARACTERISTIC,
BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);

编辑 - 请记住,如果中央请求打开通知,它会尝试编写客户端特征配置描述符。除非它是一个带有 NO_RESPONSE 设置的请求,否则 central 会期待一个响应。

正确的解决方案是向onDescriptorWriteRequest 添加回调并向中央发送响应:

BluetoothGattServerCallback serverCallback = new BluetoothGattServerCallback() {
// (...)

@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,
boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded, offset, value);

// validate if the request is exactly what you expect if needed
bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
}

// (...)
};

可能的解决方法

一些中国制造商不符合蓝牙核心规范,他们在发送通知的特性下没有 CCC 描述符。如果您能够在不设置 CCC 的情况下获得通知,那么您可以使用兼容模式 RxBleConnection.setupNotifications(characteristic, NotificationSetupMode.COMPAT) 尽管不鼓励这样做并且应该对配置进行适当的修复。

关于android - BLE 通知订阅获得 133,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43989855/

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