Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何有效地同时从两个BLE设备读取温度?_Android_Android Bluetooth_Rx Android_Android Ble_Rxandroidble - Fatal编程技术网

Android 如何有效地同时从两个BLE设备读取温度?

Android 如何有效地同时从两个BLE设备读取温度?,android,android-bluetooth,rx-android,android-ble,rxandroidble,Android,Android Bluetooth,Rx Android,Android Ble,Rxandroidble,首先,我使用库来管理我的BLE连接 我有两个,我想同时读取两个的温度。例如,我想准确地每隔500毫秒读取两个设备的温度,并在两个文本视图中向用户显示 我的应用程序当前已成功连接到两个BLE设备,如下所示: @OnClick(R.id.connectButton1) public void connectFirstSensorTag(Button b) { if (!isDeviceConnected(sensorTag1)) { connectionObservable1

首先,我使用库来管理我的BLE连接

我有两个,我想同时读取两个的温度。例如,我想准确地每隔500毫秒读取两个设备的温度,并在两个文本视图中向用户显示

我的应用程序当前已成功连接到两个BLE设备,如下所示:

@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);
        }
    });
}
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());
                });
这段代码在一个runnable中,每500毫秒调用一次


我觉得这样做效率极低。有人能告诉我是否有更好的方法吗?

我建议您启动两个线程。线程1检查第一个设备,线程2检查第二个设备。这样可以确保同时读取这两个文件。为了在两者都完成后才继续代码,我将执行一个阻塞连接循环

//Threads array has been created, with 0 checking the first 
//device and 1 checking the second
for(i = 0; i < threads.length(); i++)
    threads[i].join();
//已创建线程数组,0正在检查第一个线程
//设备和1正在检查第二个
对于(i=0;i
首先,您无法对BLE进行真正的并行读取或任何其他操作,因为您只有一个收音机,操作需要是顺序的。你能做的最好的事情就是尽快一个接一个地解雇他们。使用RxAndroidBle,您可以为自己管理序列化

做你想做的事的方法我是这样看的:

    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
            );
希望这对你有帮助


致以最良好的祝愿。

您可能应该将阅读并行化,不是吗?还请注意,没有理由有两个匿名的“新订阅者”((将其提取为一个内部类,并由构造函数传递
statusTextView1/2
)嘿,谢谢你的回答。你能详细说明一下你提到的for循环吗?我不认为我完全理解它如何帮助我知道两个线程何时完成。
thread.join
允许你等待一个线程在完成之前结束。现在,如果你循环所有线程,确保它们都完成了,那么这正是你想要的!这是有道理的,谢谢!我接受了另一个答案,因为它对我的示例更可行,但您的信息也有帮助。谢谢!这正是我想要的答案。不过还有一个问题。您是如何知道RxAndroidBle能够做到这一点的?我已经阅读了此li的所有可用文档布雷(这里没有太多)没有这样的例子。你有没有深入研究源代码来找到答案?或者我在学习这个库的时候还错过了什么?我真的很感兴趣,因为我经常偶然发现一个问题,即使在翻了很多文档后也无法解决。我是RxAndro的作者之一idBle库。该库试图遵守可观察的契约,其余的则将其与rxJava知识相结合。在上面的示例中,我只使用了两种库方法:
establishConnection(Context,boolean)
readCharacteristic(UUID)
-其余的都是纯rxJava。实际上有一个示例#5,其中类似的流与
readRssi()
一起使用,而不是
readCharacteristic(UUID)
如果我可以问的话-你想要什么信息?在哪里?我将尝试更新可用的文档。实际上我不知道这主要是RxJava。我对RxJava也很陌生(在此之前几乎从未使用过),所以这可能是我无法在更“高级”的版本中使用此库的原因方法。我想我希望从文档中看到并可能解释一些更高级的使用示例,如您的答案中的示例,因为像我这样的许多人第一次遇到RxJava时,他们都想使用像您这样的库。但无论哪种方法,我都喜欢它!这使BLE对我来说非常容易使用:)