First of all, I am using RxAndroidBLE library to manage my BLE connections.
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.
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);
}
});
}
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());
});
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?
statusTextView1/2
by the constructor)