小编Dar*_*ryn的帖子

使用库RxAndroidBle从Android设备读取多个特征

我正在使用RxAndroidBle库来扫描设备,然后连接到一个特定的设备并读取4 GATT特征。

我可以通过以下代码读取一个特性(电池电量):

scanSubscription = rxBleClient.scanBleDevices(
            new ScanSettings.Builder()
                    .build()
    )
            .observeOn(AndroidSchedulers.mainThread())
            .doOnNext(
                    scanResult -> {
                        if(scanResult.getBleDevice().getName() != null){
                            if(scanResult.getBleDevice().getName().equals("NODE 1")){
                                Log.e("BLE SCAN", "SUCCESS");
                                Log.e("BLE SCAN", scanResult.getBleDevice().getName());
                                Log.e("BLE SCAN", scanResult.getBleDevice().getMacAddress());
                                scanSubscription.unsubscribe();

                                RxBleDevice device = scanResult.getBleDevice();
                                subscription = device.establishConnection(false) // <-- autoConnect flag
                                        .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb")))
                                        .subscribe(
                                                characteristicValue -> {
                                                    Log.e("Characteristic", characteristicValue[0]+"");
                                                },
                                                throwable -> {
                                                    Log.e("Error", throwable.getMessage());
                                                }
                                        );
                            }
                        }
                    }
            )
            .subscribe();
Run Code Online (Sandbox Code Playgroud)

我可以使用来阅读两个:

.flatMap(rxBleConnection -> Observable.combineLatest( // use the same connection and combine latest emissions
                rxBleConnection.readCharacteristic(aUUID),
                rxBleConnection.readCharacteristic(bUUID),
                Pair::new
        )) …
Run Code Online (Sandbox Code Playgroud)

bluetooth-lowenergy android-bluetooth rxandroidble

0
推荐指数
1
解决办法
657
查看次数