Skip to content Skip to sidebar Skip to footer

Rxandroidble: Setup Notification, Write On Characteristic And Wait For Notification To Proceed

I am using Polidea's RxAndroidBle library to communicate with a Device in my Android application. I am very new to Reactive Programming so I can't figure out exactly how to do the

Solution 1:

I ended figuring it out by myself. Here is a method that setup the indication or notification in a characteristic, then writes some bytes to another characteristic and returns an Observable<String> that emits the byte[] parsed to a hex String that were read on the notification/indication.

Hope it helps someone else looking for this solution in RxJava2.

privateObservable<String> writeAndReadOnNotification(UUID writeTo, UUID readOn,
                                                      String hexString,
                                                      boolean isIndication,
                                                      RxBleConnection rxBleConnection) {
    Observable<Observable<byte[]>> notifObservable =
            isIndication ?
                    rxBleConnection.setupIndication(readOn) :
                    rxBleConnection.setupNotification(readOn);
    return notifObservable.flatMap(
            (notificationObservable) -> Observable.combineLatest(
                    rxBleConnection.writeCharacteristic(writeTo, hexToBytes(hexString)).toObservable(),
                    notificationObservable.take(1),
                    (writtenBytes, responseBytes) -> bytesToHex(responseBytes)
            )
    ).take(1)
            .observeOn(AndroidSchedulers.mainThread())
            .doOnError(this::throwException);
}

Post a Comment for "Rxandroidble: Setup Notification, Write On Characteristic And Wait For Notification To Proceed"