我迫切需要在我的申请中进一步推进这一点。
我对Android非常熟悉,多年来一直在使用。
我有下面的代码来启用通知,它在我的外围设备上工作了多年。启用通知时,使用"OK_N1“调用OK_N1方法。
private void enableNotification(String serviceUUID, String characteristicUUID) {
if (bluetoothGatt == null) {
return;
}
BluetoothGattService service = bluetoothGatt.getService(UUID.fromString(serviceUUID));
if (service == null) {
return;
}
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(characteristicUUID));
bluetoothGatt.setCharacteristicNotification(characteristic, true);
enableDescriptor(characteristic);
}
private void enableDescriptor(BluetoothGattCharacteristic bluetoothGattCharacteristic) {
if (bluetoothGatt == null) {
return;
}
BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(
UUID.fromString(PodsServiceCharacteristics.CLIENT_CHARACTERISTIC_CONFIG));
if (descriptor == null)
return;
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
}现在,我使用Polidea RxAndroidble(ver1.7.0)和RxJava2来简化事情。
下面的代码与Polidea的RxAndroidBle不起作用。
public void enableNotifications(@NotNull String[] characteristics) {
if (isConnected()) {
mNotificationSubscriber = mRxBleConnection.setupNotification(UUID.fromString(characteristics[0]))
.doOnNext(notificationObservable -> notificationHasBeenSetUp())
.flatMap(notificationObservable -> notificationObservable)
.subscribe(this::onNotificationReceived, this::onNotificationSetupFailure);
}
}
private void onNotificationReceived(byte[] bytes) {
Log.i(TAG, "onNotificationReceived");
}
private void onNotificationSetupFailure(Throwable throwable) {
Log.i(TAG, "onNotificationSetupFailure" + throwable.getMessage());
}
private void notificationHasBeenSetUp() {
Log.i(TAG, "notificationHasBeenSetUp");
}调用notificationHasBeenSetUp(),但不调用onNotificationReceived(),在这里我得到"OK_N1“字节。
发布于 2018-08-13 15:24:47
这可能是因为您的外围设备在设置Client Characteristic Configuration Descriptor之后立即发送通知。
默认情况下,RxAndroidBle在发射Observable<byte[]>之前完全设置通知,即在发射之前必须成功地编写Observable<byte[]>描述符。
由于库版本1.8.0,所以有可能使用NotificationSetupMode.QUICK_SETUP,它会在编写CCC描述符之前,尽快地发射Observable<byte[]>,从而允许捕获此类早期通知/指示。
mRxBleConnection.setupNotification(bluetoothGattCharacteristic, NotificationSetupMode.QUICK_SETUP)预1.8.0版本答案
为了避免错过任何“早期”排放,您可以利用NotificationSetupMode.COMPAT,其中库不负责编写CCC描述符*。
(...)
mNotificationSubscriber = mRxBleConnection.discoverServices()
.flatMap(rxBleDeviceServices -> rxBleDeviceServices.getCharacteristic(characteristicUuid))
.flatMapObservable(bluetoothGattCharacteristic -> {
BluetoothGattDescriptor cccDescriptor = bluetoothGattCharacteristic.getDescriptor(PodsServiceCharacteristics.CLIENT_CHARACTERISTIC_CONFIG);
Completable enableNotificationCompletable = mRxBleConnection.writeDescriptor(cccDescriptor, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
Completable disableNotificationCompletable = mRxBleConnection.writeDescriptor(cccDescriptor, BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE).onErrorComplete();
return mRxBleConnection.setupNotification(bluetoothGattCharacteristic, NotificationSetupMode.COMPAT)
.doOnNext(notificationObservable -> notificationHasBeenSetUp())
.flatMap(notificationObservable -> notificationObservable)
.mergeWith(enableNotificationCompletable)
.doOnDispose(disableNotificationCompletable::subscribe) // fire and forget
.share(); // this can be omitted but I put it here in case the resulting `Observable<byte[]>` would be shared among multiple subscribers
})
.subscribe(this::onNotificationReceived, this::onNotificationSetupFailure);
(...)我认为,由于这显然是相当普遍的情况,它将在某个时候由图书馆处理。
* NotificationSetupMode.COMPAT主要用于不符合BLE规范且没有CCC描述符但始终会发送通知的BLE外围设备。
https://stackoverflow.com/questions/51824612
复制相似问题