0

I'm using RxAndroidBle 2 to watch a characteristic for CSC but I cannot read some characteristics before. It's also the first time I'm using ReactiveX so I have to deal with flatMap, flatMapSingle, etc.

Here's my code to register a handler for watching CSC Measure:

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());
            }
        }
);

And then the code for extracting the value:

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;
    }
}

I have another piece of working code to read ONE characteristic, but how can I process a list of characteristics?

    .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.
        }
)

Constants are defined like this:

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

I tried to integrate the answer provided in here but the code doesn't compile anymore. Moreover, the code should combine up to 12 characteristics (simple map of UUID to Int/String/Boolean). I used to have a working code by creating a subclass of BluetoothGattCallback but my code was getting more and more difficult to maintain with standard Android Bluetooth classes.

Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21
MBach
  • 1,647
  • 16
  • 30

1 Answers1

0

I tried to integrate the answer provided in here but the code doesn't compile anymore.

I have updated the post you mentioned to match RxAndroidBle based on RxJava2. It should compile now.

I have another piece of working code to read ONE characteristic, but how can I process a list of characteristics? ... Moreover, the code should combine up to 12 characteristics (simple map of UUID to Int/String/Boolean).

The mentioned post does solve a case where there are 4 characteristics. In case of 12 (or a variable number) there is a Single#zipArray function.

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)
)
Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21
  • Well unfortunately I had to revert, too much code to rework and still didn't get the expected result. Can't confirm nor deny your answer – MBach Sep 23 '21 at 22:03