Share

engineering

5min read

Maintaining Open-Source RxAndroidBle Library (Readability)

Maintaining Open-Source RxAndroidBle Library (Readability)

The third part of the series about maintaining RxAndroidBle library deals with descriptive signatures. If you want to come back to the introductory article, you can find it here.

Descriptive signatures

At the beginning, the RxJava framework was in the 1.1.0. version, meaning that the only production-ready interface was the Observable. All good, except for one thing…

Observable is quite vague. It is possible that it will emit events indefinitely:

|-----o-----o-----o-----o-----o----->

It can also complete on its own after several emissions:

|-----o-----o-----o-----o-----|----->

Or emit just a single event:

|-----o----------------------------->

Single event and complete:

|-----o-|--------------------------->

Single event and error:

|-----o-----------------------x----->

Or even more… Either way, there are multiple behaviors that an Observable may demonstrate. The problem is that most of the users coming to use a library API often have little to no domain knowledge about what they are about to use. They usually don’t have any idea how the Observable acts either. The initial API of the library’s RxBleConnection used to look like this:

interface RxBleConnection {
    Observable<RxBleDeviceServices> discoverServices();
    Observable<byte[]> readCharacteristic(UUID characteristicUuid);
    Observable<byte[]> writeCharacteristic(UUID characteristicUuid, byte[] data);
    Observable<Observable<byte[]>> setupNotification(UUID characteristicUuid);
    Observable<Observable<byte[]>> setupIndication(UUID characteristicUuid);
    Observable<byte[]> readDescriptor(UUID serviceUuid, UUID characteristicUuid, UUID descriptorUuid);
    Observable<byte[]> writeDescriptor(UUID serviceUuid, UUID characteristicUuid, UUID descriptorUuid, byte[] data);
    Observable<Integer> readRssi();
}

At some point users asked for the clarification of the behavior, so the following table was added to the documentation:

Interface Function Number of values Completes
RxBleClient scanBleDevices()* Infinite false
RxBleClient observeStateChanges() Infinite** false**
RxBleDevice observeConnectionStateChanges() Infinite false
RxBleDevice establishConnection()* Single false
RxBleConnection discoverServices() Single true
RxBleConnection setupNotification()* Single false
RxBleConnection setupNotification() emitted Observable Infinite false
RxBleConnection setupIndication()* Single false
RxBleConnection setupIndication() emitted Observable Infinite false
RxBleConnection getCharacteristic() Single true
RxBleConnection readCharacteristic() Single true
RxBleConnection writeCharacteristic() Single true
RxBleConnection readDescriptor() Single true
RxBleConnection writeDescriptor() Single true
RxBleConnection readRssi() Single true
RxBleConnection requestMtu() Single true
RxBleConnection queue() User defined User defined
LongWriteOperationBuilder build() Single true

Definitely not the perfect solution. The problem lies in the fact that the table is available only in the documentation and Javadoc but it should be clear just by looking on the API. Fortunately, RxJava framework has matured and now has more interfaces than only the Observable:

  • Single — emits only a single event or an error
  • Completable — does not emit any values but completes or emits an error at some point in time
  • Maybe — may emit a single event or complete without emitting (and of course it can emit an error)

So, the new API that is under development will make the understanding easier by simply looking at the code:

interface Connection {
    Single<RxBleDeviceServices> discoverServices();
    Single<byte[]> readCharacteristic(UUID characteristicUuid);
    Completable writeCharacteristic(UUID characteristicUuid, byte[] data);
    Observable<Observable<byte[]>> setupNotification(UUID characteristicUuid);
    Observable<Observable<byte[]>> setupIndication(UUID characteristicUuid);
    Single<byte[]> readDescriptor(UUID serviceUuid, UUID characteristicUuid, UUID descriptorUuid);
    Completable writeDescriptor(UUID serviceUuid, UUID characteristicUuid, UUID descriptorUuid, byte[] data);
    Single<Integer> readRssi();
}

everywhere.jpg

One could see that there is a peculiar case of Observable<Observable<byte[]>> setupNotification(UUID characteristicUuid). That seems odd! And actually this API has a good explanation:

Making a characteristic to send notifications to the mobile device is (as most actions in BLE) asynchronous so it takes time to be finished. Additionally, the pool for characteristic notifications is fixed (max 4 on Android 4.3 / 7 on 4.4 / 15 on 5.0) and managing them could be done the same way as managing the RxBleConnection. All as long as the user is interested in (subscribed to) .setupNotification(), the notification is active and it is torn down as soon as the user unsubscribes. Additionally, when the notification is established, it is able to transmit an infinite amount of data—that is why the API has Observable<Observable<byte[]>> return type.

Wrap up: the User

It is very important to provide users with clear info. They need to know what they can achieve by using a particular library and what to expect from it. The abstraction they are about to touch should fit into what they already know and shouldn’t require learning new concepts. Concise API also helps users in understanding what they would get without the need of digging through the documentation.

Perspective: the Developer

We are creating abstractions and APIs for the users but we are not omniscent. This means that we sometimes need to iterate over what we have already published. The original API was not perfect for the user and we needed to introduce a better one. But the users are not happy with rewriting their code. It is better to give them time to change—and provide with something that comes in handy and is already well established in the industry.

Stay tuned for the next part about API development!

Share

Darek

Staff Software Engineer

Did you enjoy the read?

If you have any questions, don’t hesitate to ask!

Did you enjoy the read?

If you have any questions, don’t hesitate to ask!