如何有效地同时从两个BLE设备读取温度? [英] How do I effectively read temperature from two BLE devices at the same time?

查看:196
本文介绍了如何有效地同时从两个BLE设备读取温度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我正在使用 RxAndroidBLE 库来管理我的BLE连接。

First of all, I am using RxAndroidBLE library to manage my BLE connections.

我有两个 SensorTag设备和我想同时读取两者的温度。例如,我想每500ms准确地从两个设备读取温度并将其显示在两个TextViews中。

I have two SensorTag devices and I want to read temperature from both at the same time. For example, I'd like to read the temperature from both devices exactly every 500ms and display it to user in two TextViews.

我的应用程序当前已成功连接到两个BLE设备像这样:

My app currently successfully connects to both BLE devices like this:

@OnClick(R.id.connectButton1)
public void connectFirstSensorTag(Button b) {
    if (!isDeviceConnected(sensorTag1)) {
        connectionObservable1 = sensorTag1.establishConnection(getApplicationContext(), false).compose(new ConnectionSharingAdapter());
    }

    connectionObservable1.subscribe(new Subscriber<RxBleConnection>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            updateStatus(statusTextView1, "SensorTag not found");
        }

        @Override
        public void onNext(RxBleConnection rxBleConnection) {
            updateStatus(statusTextView1, "Connected");
            enableSensorTagTemperatureSensor(connectionObservable1);
        }
    });
}

@OnClick(R.id.connectButton2)
public void connectSecondSensorTag(Button b) {
    if (!isDeviceConnected(sensorTag2)) {
        connectionObservable2 = sensorTag2.establishConnection(getApplicationContext(), false).compose(new ConnectionSharingAdapter());
    }

    connectionObservable2.subscribe(new Subscriber<RxBleConnection>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            updateStatus(statusTextView2, "SensorTag not found");
        }

        @Override
        public void onNext(RxBleConnection rxBleConnection) {
            updateStatus(statusTextView2, "Connected");
            enableSensorTagTemperatureSensor(connectionObservable2);
        }
    });
}

现在,我正在寻找从这两个位置读取温度的最佳方法每500毫秒一次。

Now I'm looking for the best way to read temperature from both at the same time every 500ms.

现在,我正在执行以下操作:

Right now, I'm doing something like this:

connectionObservable1
                .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(uuidFromShortCode("AA01")))
                .subscribe(bytes -> {

                    // first temperature was successfully read here

                    connectionObservable2
                            .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(uuidFromShortCode("AA01")))
                            .subscribe(bytes -> {

                                // second temperature was successfully read here

                            }, error -> {
                                updateStatus(error.toString());
                            });
                }, error -> {
                    updateStatus(error.toString());
                });

这段代码位于一个可运行的内部,每500毫秒调用一次。

And this block of code is inside a runnable that gets called every 500ms.

我觉得这是一种极其低效的方式。有人可以让我知道是否有更好的方法吗?

I feel like this an extremely inefficient way to do it. Could someone please let me know if there is a better way to do this?

推荐答案

首先,您无法进行真正的并行读取或BLE上的任何其他操作,因为您只有一个无线电,并且操作需要顺序进行。您能做的最好的事情就是尽可能快地将它们发射一次。使用RxAndroidBle,您可以为您管理序列化。

First of all you cannot make truly parallel reads or any other operations on the BLE as you have only one radio and operations need to be sequential. The best you can do is to fire them as soon one after another as possible. Using the RxAndroidBle you are getting the serialization managed for you.

执行所需操作的方式如下:

The way to doing what you want I see this way:

    RxBleDevice sensorTag0 = // your first SensorTag
    RxBleDevice sensorTag1 = // your second SensorTag
    UUID characteristicUuid = uuidFromShortCode("AA01");

    Subscription flowSubscription = Observable.combineLatest(
            sensorTag0.establishConnection(this, false), // establishing connections
            sensorTag1.establishConnection(this, false),
            Pair::new // merging them together
    )
            .flatMap(connections -> Observable
                    .interval(500, TimeUnit.MILLISECONDS) // every 500 ms
                    .flatMap(aLong -> Observable.combineLatest(
                            connections.first.readCharacteristic(characteristicUuid), // performing reads
                            connections.second.readCharacteristic(characteristicUuid),
                            Pair::new // and merging the results
                    )))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    readings -> {
                        updateUISensorTag0(readings.first); // updating the UI
                        updateUISensorTag1(readings.second);
                    },
                    throwable -> updateStatus(throwable.toString()) // or showing the error
            );

希望这对您有所帮助。

最好问候。

这篇关于如何有效地同时从两个BLE设备读取温度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆