0

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()
rjr-apps
  • 352
  • 4
  • 13
  • 1
    Hey! 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. I modified your post with a proposed test scenario. – pawel.urban Jun 30 '17 at 20:28
  • That did it! You can post that as the answer and I'll mark it correct, if you like. Thanks! :) – rjr-apps Jun 30 '17 at 20:53
  • 1
    Cool, I posted the code sample along with the answer :) – pawel.urban Jul 01 '17 at 05:24

2 Answers2

1

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()
pawel.urban
  • 1,031
  • 8
  • 10
1

I think this answer was for RxJava, for RxJava2 this is the updated lambda syntax:

.setWriteOperationAckStrategy(booleanObservable -> {
        Log.d("TEST", "batch written");
        return booleanObservable
            .doOnNext(aBoolean -> {
              Log.d("TEST", "batch written");
            });
      }

Full:

.setWriteOperationAckStrategy(new RxBleConnection.WriteOperationAckStrategy() {
  @Override
  public Observable<Boolean> apply(Observable<Boolean> booleanObservable) {
    Log.d("TEST", "batch written");
    return booleanObservable
        .doOnNext(new Consumer<Boolean>() {
           @Override
           public void accept(Boolean aBoolean) {
             Log.d("TEST", "batch written");
           }
         });
  }
RandomIO
  • 93
  • 7