有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java RxAndroidBle如何从多个服务读取通知?

我使用rx安卓:2.1.0、rxjava:1.4.0和rx安卓:1.8.1

我正在尝试连接到设备,并从位于不同服务(温度服务、电池服务、自定义服务)中的特征接收通知

我已经浏览了很多页面,但还没有找到合适的解决方案。也许有人知道如何做到这一点


共 (2) 个答案

  1. # 1 楼答案

    我已经把我的答案贴在这里了

    我无法使用rxAndroidBle库,但standart one对我来说很好

  2. # 2 楼答案

    要在任何给定的外围设备上设置所有可能的通知/指示,首先需要连接到设备,然后发现所有服务并获取所有符合条件的特征。在这些特征上,应设置通知/指示,然后与其来源配对(如果需要)

    示例代码:

    device.establishConnection(false)
            .flatMap(connection -> connection.discoverServices()
                    .map(RxBleDeviceServices::getBluetoothGattServices) // get them
                    .flatMapObservable(Observable::fromIterable) // map to individual services
                    .map(BluetoothGattService::getCharacteristics) // for each service take all characteristics
                    .flatMap(Observable::fromIterable) // map to individual characteristic)
                    .filter(characteristic -> (characteristic.getProperties() & (BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_INDICATE)) != 0) // consider only characteristics that have indicate or notify property
                    .flatMap(characteristic -> {
                        NotificationSetupper notificationSetupper;
                        if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
                            notificationSetupper = setupNotifications(characteristic); // if supports notifications
                        } else {
                            notificationSetupper = setupIndications(characteristic); // else indications
                        }
                        return notificationSetupper.setupOn(connection);
                    })
            )
            .subscribe(
                    pair -> {
                        BluetoothGattCharacteristic characteristic = pair.first;
                        byte[] value = pair.second;
                        // do your thing with the emissions
                    },
                    throwable -> {
                        // log potential errors
                    }
            );
    

    帮助者包括:

    interface NotificationSetupper {
    
        Observable<Pair<BluetoothGattCharacteristic, byte[]>> setupOn(RxBleConnection connection);
    }
    
    static NotificationSetupper setupNotifications(BluetoothGattCharacteristic characteristic) {
        return connection -> connection.setupNotification(characteristic, getModeFor(characteristic))
                .compose(pairNotificationsWith(characteristic));
    }
    
    static NotificationSetupper setupIndications(BluetoothGattCharacteristic characteristic) {
        return connection -> connection.setupIndication(characteristic, getModeFor(characteristic))
                .compose(pairNotificationsWith(characteristic));
    }
    
    static NotificationSetupMode getModeFor(BluetoothGattCharacteristic characteristic) { // a different mode is needed if a characteristic has no Client Characteristic Configuration Descriptor
        UUID clientCharacteristicConfigurationDescriptorUuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
        return characteristic.getDescriptor(clientCharacteristicConfigurationDescriptorUuid) != null ? NotificationSetupMode.DEFAULT : NotificationSetupMode.COMPAT;
    }
    
    static ObservableTransformer<Observable<byte[]>, Pair<BluetoothGattCharacteristic, byte[]>> pairNotificationsWith(BluetoothGattCharacteristic characteristic) { // to distinguish the source of notification we need to pair the value with the source
        return upstream -> upstream.flatMap(it -> it).map(bytes -> Pair.create(characteristic, bytes));
    }