代码之家  ›  专栏  ›  技术社区  ›  Sundeep1501

Polidea rxandroidable启用通知

  •  1
  • Sundeep1501  · 技术社区  · 7 年前

    我非常需要这个在我的申请中进一步进行。

    我非常熟悉Android BLE,并且已经使用了很多年。

    onCharacteristicChanged() 当启用通知时,使用“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 (1.7.0版)使用RxJava2使事情变得简单。

    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”字节

    1 回复  |  直到 7 年前
        1
  •  0
  •   Dariusz Seweryn    7 年前

    可能是因为你的外设在 Client Characteristic Configuration Descriptor 准备好了。

    RxAndroidBle 在发出 Observable<byte[]> i、 e.必须在排放前成功写入CCC描述符。

    为了不错过任何“早期”排放,我们可以利用 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);
    
    (...)
    

    * 主要是为不遵循BLE规范并且没有CCC描述符的BLE外围设备引入的,这些外围设备将一直发送通知