Search code examples
rx-javarx-androidrxandroidble

How to implement synchronous task queue for RxAndroidBle


Summary

I use RxAndroidBle as the BLE communication framework in my project. I receive the data from the notification after writing in the following way:

public Observable<byte[]> requestCharacteristic(UUID notificationUuid, UUID writeUuid, byte[] writeData) {
    return Observable.zip(
             connectionObservable.concatMap(rxConnection -> rxConnection.setupNotification(notificationUuid))
                 .concatMap(rxConnection -> rxConnection).first(),
             connectionObservable.concatMap(rxConnection -> rxConnection.writeCharacteristic(writeUuid, writeData)),
             (responseBytes, writeBytes) -> { return responseBytes; }
    );
}

Question

Because the BLE device needs to guarantee the synchronization request by the mobile phone, the request must be completed after the request execution is completed. I am not familiar with RX. How can I do the FIFO way to execute the above code?


Solution

  • As you have stated your code will work for a single execution but when there would be multiple parallel requests they will not get serialised. What you would need to add is an external synchronisation for your requestCharacteristic() function.

    There is an already answered question for keeping a persistant connection with serialised write/notification handling which may be exactly what you are looking for. The answer was created for the library version that was based on RxJava1 but it should not be too hard to adjust it to RxJava2.