本文介绍了使用库 RxAndroidBle 从 Android 设备读取多个特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the library RxAndroidBle in order to scan devices and then connect to one specific device and read 4 GATT characteristics.

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

I can read one characteristic (Battery Level) ith this code :

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();

我可以使用 :

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

但我不明白如何用 4 个特征来做到这一点.

But I don't understand how to do that with 4 characteristics for example.

谢谢

推荐答案

上面的例子很好,你只需要一些可以接受比 Pair 多的值的数据对象.例如类似的东西:

The above example is just fine you would only need some data object that would accept more values than Pair. For instance something like:

class ReadResult {
    final byte[] aValue;
    final byte[] bValue;
    final byte[] cValue;
    final byte[] dValue;

    ReadResult(byte[] aValue, byte[] bValue, byte[] cValue, byte[] dValue) {
        this.aValue = aValue;
        this.bValue = bValue;
        this.cValue = cValue;
        this.dValue = dValue;
    }
}

然后示例可能如下所示:

And then the example could look like this:

disposable = rxBleClient.scanBleDevices(
        new ScanSettings.Builder().build(),
        new ScanFilter.Builder().setDeviceName("NODE 1").build() // one can set filtering by name here
)
        .take(1) // take only the first result and then the upstream will get unsubscribed (scan will end)
        .flatMap(scanResult -> scanResult.getBleDevice().establishConnection(false)) // connect to the first scanned device that matches the filter
        .flatMapSingle(rxBleConnection -> Single.zip( // once connected read all needed values
                rxBleConnection.readCharacteristic(aUUID),
                rxBleConnection.readCharacteristic(bUUID),
                rxBleConnection.readCharacteristic(cUUID),
                rxBleConnection.readCharacteristic(dUUID),
                ReadResult::new // merge them into a single result
        ))
        .take(1) // once the result of all reads is available unsubscribe from the upstream (connection will end)
        .subscribe(
                readResult -> Log.d("Characteristics", /* print the readResult */),
                throwable -> Log.e("Error", throwable.getMessage())
        );

基于 RxJava1 的 RxAndroidBle 原始/遗留解决方案:

Original/Legacy solution for RxAndroidBle based on RxJava1:

subscription = rxBleClient.scanBleDevices(
        new ScanSettings.Builder().build(),
        new ScanFilter.Builder().setDeviceName("NODE 1").build() // one can set filtering by name here
)
        .take(1) // take only the first result and then the upstream will get unsubscribed (scan will end)
        .flatMap(scanResult -> scanResult.getBleDevice().establishConnection(false)) // connect to the first scanned device that matches the filter
        .flatMap(rxBleConnection -> Observable.combineLatest( // once connected read all needed values
                rxBleConnection.readCharacteristic(aUUID),
                rxBleConnection.readCharacteristic(bUUID),
                rxBleConnection.readCharacteristic(cUUID),
                rxBleConnection.readCharacteristic(dUUID),
                ReadResult::new // merge them into a single result
        ))
        .take(1) // once the result of all reads is available unsubscribe from the upstream (connection will end)
        .subscribe(
                readResult -> Log.d("Characteristics", /* print the readResult */),
                throwable -> Log.e("Error", throwable.getMessage())
        );

这篇关于使用库 RxAndroidBle 从 Android 设备读取多个特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 08:38