How to implement synchronous task queue for RxAndroidBle
How to implement synchronous task queue for RxAndroidBle
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;
);
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?
1 Answer
1
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.
requestCharacteristic()
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
.
RxJava1
RxJava2
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks , this way can make logical serial
– Zachary
Sep 13 '18 at 8:46