gpt4 book ai didi

android - 如何同时有效地读取两个 BLE 设备的温度?

转载 作者:太空狗 更新时间:2023-10-29 13:12:18 26 4
gpt4 key购买 nike

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

我有两个 SensorTag devices我想同时读取两者的温度。例如,我想每 500 毫秒从两个设备读取一次温度,并在两个 TextView 中将其显示给用户。

我的应用目前成功连接到两个 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);
}
});
}

现在我正在寻找每 500 毫秒同时从两者读取温度的最佳方法。

现在,我正在做这样的事情:

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 毫秒调用一次的可运行对象中。

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

最佳答案

首先,您无法在 BLE 上进行真正的并行读取或任何其他操作,因为您只有一个 radio ,并且操作需要按顺序进行。你能做的最好的事情就是尽快一个接一个地解雇他们。使用 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
);

希望对你有帮助。

最好的问候。

关于android - 如何同时有效地读取两个 BLE 设备的温度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38940943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com