rxandroidble

RxAndroidBle long write - callback for each batch written


I'm doing a long write operation, sending 16 byte batches at a time. I'd like to have a progress bar show the progress of the long write to the user, so I need some sort of callback for each time a batch has been written.

From the documentation, it looked like setWriteOperationAckStrategy does this. However, when running the following code, I only end up seeing one message output to the log. What am I doing wrong here?

subscription = connection.flatMap(rxBleConnection -> rxBleConnection.createNewLongWriteBuilder()
    .setCharacteristicUuid(uuid)
    .setBytes(bytes)
    .setMaxBatchSize(16)
    .setWriteOperationAckStrategy(new RxBleConnection.WriteOperationAckStrategy() {
        @Override
        public Observable<Boolean> call(Observable<Boolean> booleanObservable) {
            Log.d("TEST", "batch written");
            return booleanObservable;
        }
    })
    .build()

Solution

  • The setWriteOperationAckStrategy is similar to standard RxJava's Observable transformers. In order to keep the allocation low, we tend to modify source observable rather than to create a new one when each batch is completed.

    subscription = connection.flatMap(rxBleConnection -> rxBleConnection.createNewLongWriteBuilder()
        .setCharacteristicUuid(uuid)
        .setBytes(bytes)
        .setMaxBatchSize(16)
        .setWriteOperationAckStrategy(new RxBleConnection.WriteOperationAckStrategy() {
            @Override
            public Observable<Boolean> call(Observable<Boolean> booleanObservable) {
                Log.d("TEST", "batch written");
                return booleanObservable
                    .doOnNext(new Action1<Boolean>() {
                        @Override
                        public void call(Boolean aBoolean) {
                            Log.d("TEST", "batch written");
                        }
                    });
            }
        })
        .build()