0

Hi i am trying to build my first app with RxJava and BLE and i have a question: I made a wite(byte b) method in singletone class(that i use for connection, notifications, etc..). So now if i want to write data to BLE device i call this method. I need to do this a lot from different parts of code. I am very new to RxJava concept, and i feel like i am doing wrong. Current solution:

public void write(byte[] b) {

        if (isConnected()) {
            connectionObservable
                    .flatMap(rxBleConnection -> rxBleConnection.writeCharacteristic(characteristicUuid, b))
                    .subscribe(bytes -> {
                        onWriteSuccess(bytes);
                    }, this::onWriteFailure);
        }

    }

I feel like i need to do something like that, but i dont know how:

 protected static final BlockingQueue<byte[]> TxQueue = new ArrayBlockingQueue<>(32);

 public void write(byte[] b) {

       TxQueue.add(bytes);

    }

And call this only once during connection to device:

connectionObservable
                    .flatMap(rxBleConnection ->     rxBleConnection.writeCharacteristic(characteristicUuid, TxQueue.take))
                    .subscribe(bytes -> {
                        onWriteSuccess(bytes);
                    }, this::onWriteFailure);

Thanks for an answer!

1 Answers1

0

Your first solution will work just fine.

The RxAndroidBle library is handling queueing of the commands for you so the only thing you need to do is to subscribe() to RxBleConnection.writeCharacteristic().

There is also similar questions already on Stackoverflow: RxAndroidBle keeping a persistant connection + Write/Notification handling

Best Regards

Community
  • 1
  • 1
Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21