0

I'm doing an app that works as a remote control to a ventilator using RxAndroidBle. I have a problem with the unsubscribe because when I use

.flatMap(rxBleConnection -> rxBleConnection.writeCharacteristic(Uuids.UUID_RX, flaktCommandConcat.getBytes()))

and after that I use subscription.unsubscribe(); the writeCharacteristics doesn´t work because the unsubscribe runs always first and the connection disconect before the data was sent.

What I need is:

  • When I click the button I want to connect to the ventilator
  • Then send all values
  • And then disconnect.
  • If I repeat the procedure, it will need to do the same thing over and over again.

Can some one help me with some idea? I tried to use .delay(1000, Time.MILISECONDS) and it worked but it took a long time to send the information to the ventilator.

This is my code:

 public void writeRxCharacteristics(String flaktCommandConcat){

    rxBleDevice = rxBleClient.getBleDevice(Uuids.DEVICE_ADDRESS);


    subscription = rxBleDevice.establishConnection(true) //false
            .observeOn(AndroidSchedulers.mainThread())
            .flatMap(rxBleConnection -> rxBleConnection.createNewLongWriteBuilder()
            .setCharacteristicUuid(Uuids.UUID_RX)
            .setBytes(flaktCommandConcat.getBytes())
            .build())

            .subscribe(
                    byteArray -> {
                        Log.d("CharacteristicValue","WRITE: " + Arrays.toString(byteArray));
                    },

                    throwable -> {
                        Log.d("CharacteristicValue","Throwable: " + throwable.toString());
                        rxBleActivity.onScanFailure(throwable, getContext());
                    }
            );


    rxBleDevice.observeConnectionStateChanges()
            .observeOn(AndroidSchedulers.mainThread())
            .delay(1000, TimeUnit.MILLISECONDS)
            .subscribe(
                    rxBleConnectionState -> {
                        Log.d("RxBleConnectionState", " CON_STATUS: " + rxBleConnectionState);
                        disconnect();

                    },
                    throwable -> {
                        Log.d("ConnectionStateChanges","Throwable: " + throwable.toString());

                    }
            );
}
public void disconnect() {
if (subscription != null && !subscription.isUnsubscribed()) {
subscription.unsubscribe();
subscription = null;
}
Log.d("CONNECTION2", " CON_STATUS: " + rxBleDevice.getConnectionState().toString());
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • You mentioned `MILISECONDS` in your description but have used `MILLISECONDS` in your code - if one of these is misspelled, please fix it. – halfer Apr 05 '17 at 09:06

1 Answers1

0

it looks that you don't need a long write here. Is your data longer than 20 bytes?

Anyway, the library releases the connection when the Observable<RxBleConnection> is unsubscribed. What I'd do if I were you is to:

public void writeRxCharacteristics(String flaktCommandConcat){
    rxBleDevice = rxBleClient.getBleDevice(Uuids.DEVICE_ADDRESS);

    rxBleDevice.establishConnection(true) //false
            .flatMap(rxBleConnection -> rxBleConnection.createNewLongWriteBuilder()
                .setCharacteristicUuid(Uuids.UUID_RX)
                .setBytes(flaktCommandConcat.getBytes())
                .build()
            )
            .take(1)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    byteArray -> {
                        Log.d("CharacteristicValue","WRITE: " + Arrays.toString(byteArray));
                    },

                    throwable -> {
                        Log.d("CharacteristicValue","Throwable: " + throwable.toString());
                        rxBleActivity.onScanFailure(throwable, getContext());
                    }
            );

Please make sure you're not overusing the long write. It has a known bug (unrelated) in 1.2.0 which was recently fixed in 1.3.0-SNAPSHOT.

pawel.urban
  • 1,031
  • 8
  • 10
  • The `.take(1)` needs to be put below the `.flatMap()` or the connection will be closed before the Long Write Operation will start. – Dariusz Seweryn Apr 05 '17 at 09:19
  • Hi! Thank you for the feedback! I put the take(1) below the .flatMap() and it works!!! But my question now is, Why it take so long to connect/write and disconect?? Is it a problem with android latency that have no real time execution?? – Paulo Nova Apr 05 '17 at 13:42
  • Is there any reason why you've chosen to set autoconnect to true? It delays connection heavily due to how it works, in order to save battery. – pawel.urban Apr 05 '17 at 17:45