androidrxandroidble

Adding a delay in between each batch write using long write builder


I'm trying to add a delay in between each batch write and I managed to get it working by modifying this example but I'm not sure this is the correct way to achieve this?

rxBleConnection.createNewLongWriteBuilder()
              .setCharacteristicUuid(characteristic)
              .setBytes(data)
              .setWriteOperationAckStrategy(booleanObservable -> {
                  return Observable.zip(
                      Observable.timer(delayInMillis, MILLISECONDS).repeat()
                      ,booleanObservable, (callback0, aBoolean) -> aBoolean);
              })
              .build()

Solution

  • Your approach would delay next (sub)writes if Observable.timer().repeat() would emit after the booleanObservable. Unfortunately that would work only for the second (sub)write as after that .repeat() would start to emit very quickly as it does not resubscribe to the upstream Observable. From .repeat() Javadoc:

    Returns an Observable that repeats the sequence of items emitted by the source ObservableSource indefinitely.

    If you would use Observable.timer(delayInMillis, MILLISECONDS).repeatWhen(completions -> completions) or Observable.interval(delayInMillis, MILLISECONDS) then these would make writes happen not more frequent than delayInMillis, MILLISECONDS apart.

    If you would like to give the peripheral delayInMillis, MILLISECONDS time before issuing next write then there seems to be a simpler approach:

    rxBleConnection.createNewLongWriteBuilder()
        .setCharacteristicUuid(characteristic)
        .setBytes(data)
        .setWriteOperationAckStrategy(booleanObservable -> booleanObservable.delay(delayInMillis, MILLISECONDS))
        .build()