Search code examples
androidbluetooth-lowenergyrxandroidble

RxAndroidBle receive notifications faster


I'm quite new in RxJava. I try now RxAndroidBle and compare it with Android API implementation for BLE. My peripheral sends a lot of notifications at once (about 30kB in 512b chunks). In Rx it takes about 10-12 seconds to receive them all. When I try with Android API it is about 2-3 second.

In my RxAndroidBle implementation I do as it is proposed in the example app:

connectionObservable
    .flatMap(rxBleConnection -> rxBleConnection.setupNotification(UUID.fromString(mCharacteristicUUID)))
    .doOnNext(notificationObservable -> runOnUiThread(this::notificationHasBeenSetUp))
    .flatMap(notificationObservable -> notificationObservable)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(
        this::onNotificationReceived,
        this::onNotificationSetupFailure
    );

Is there any way to make it faster?


Solution

  • In your code snipped the notifications are consumed on the main thread which is also used for refreshing the UI. If you are not updating UI and want to achieve highest possible performance you can get rid of .observeOn(AndroidSchedulers.mainThread()) line.

    Samples are just samples — they are rarely optimised for a specific use-case (performance in this case).

    Having in mind your other question and that the OS or your peripheral seem to work differently when used by vanilla Android API and RxAndroidBle it may be also worth to ask a question whether both implementations work under the same conditions.