RxAndroidBle 2:多个Read然后通知另一个特性



我正在使用RxAndroidBle 2来观看CSC的特性,但我之前无法读取一些特性。这也是我第一次使用ReactiveX,所以我必须处理flatMap, flatMapSingle等。

下面是我的代码来注册一个处理程序来观看CSC度量:

connectionSubscription = device.establishConnection(false)
// Register for notifications on CSC Measure
.flatMap(rxBleConnection -> rxBleConnection.setupNotification(Constants.CSC_MEASURE))
.doOnNext(notificationObservable -> {
// Notification has been set up
Log.d(TAG, "doOnNext = " + notificationObservable.toString());
})
.flatMap(notificationObservable -> {
Log.d(TAG, "flatMap = " + notificationObservable);
return notificationObservable;
}) // <-- Notification has been set up, now observe value changes.
.subscribe(
this::onCharacteristicChanged,
throwable -> {
if (throwable instanceof BleDisconnectedException) {
Log.e(TAG, "getCanonicalName = " + throwable.getClass().getCanonicalName());
}
}
);

然后是提取值的代码:

private void onCharacteristicChanged(byte[] bytes) {
Integer f = ValueInterpreter.getIntValue(bytes, ValueInterpreter.FORMAT_UINT8, 0);
Log.d(TAG, "flags " + f);
switch (f) {
case 0: // 0x00000000 is not authorized
Log.w(TAG, "flags cannot be properly detected for this CSC device");
break;
case 1: 
// sensor is in speed mode (mounted on the rear wheel)
// Cumulative Wheel Revolutions
Integer sumWheelRevs = ValueInterpreter.getIntValue(bytes, ValueInterpreter.FORMAT_UINT32, 1);
// Last Wheel event time
Integer lastWheelEvent = ValueInterpreter.getIntValue(bytes, ValueInterpreter.FORMAT_UINT16, 5);
Log.d(TAG, "Last wheel event detected  at " + lastWheelEvent + ", wheel revs = " + sumWheelRevs);
break;
case 2: 
// sensor is in cadence mode (mounted on the crank)
// Last Crank Event Time
// Cumulative Crank Revolutions
break;
}
}

我有另一段工作代码来读取一个特征,但我怎么能处理一个特征列表?

.flatMapSingle(rxBleConnection -> rxBleConnection.readCharacteristic(Constants.DEVICE_HARDWARE))
.subscribe(
characteristicValue -> {
String deviceHardware = ValueInterpreter.getStringValue(characteristicValue, 0);
Log.d(TAG, "characteristicValue deviceHardware = " + deviceHardware);
},
throwable -> {
// Handle an error here.
}
)

常量定义如下:

public static final UUID CSC_MEASURE = UUID.fromString("00002a5b-0000-1000-8000-00805f9b34fb");

我试图整合这里提供的答案,但代码不再编译了。此外,代码应该组合多达12个特征(UUID到Int/String/Boolean的简单映射)。我曾经通过创建BluetoothGattCallback的子类有一个工作代码,但我的代码越来越难以维护标准的Android蓝牙类。

我试图整合这里提供的答案,但代码不再编译了。

我已经更新了你提到的基于RxJava2匹配RxAndroidBle的帖子。现在应该可以编译了

我有另一段工作代码来读取一个特征,但我怎么能处理一个特征列表?…此外,代码应该组合多达12个特征(UUID到Int/String/Boolean的简单映射)。

上述文章确实解决了一个有4个特征的情况。在12(或一个变量数)的情况下,有一个Single#zipArray函数。

Single.zipArray(
/* Object[] */ results -> YourWrapperObject(results),
rxBleConnection.readCharacteristic(Constants.DEVICE_HARDWARE),
rxBleConnection.readCharacteristic(Constants.DEVICE_HARDWARE1),
rxBleConnection.readCharacteristic(Constants.DEVICE_HARDWARE2),
// ...
rxBleConnection.readCharacteristic(Constants.DEVICE_HARDWARE11)
)

最新更新