0

我正在做一个应用程序,它使用 RxAndroidBle 作为呼吸机的遥控器。我有一个问题,unsubscribe因为当我使用

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

之后我使用subscription.unsubscribe();writeCharacteristics 不起作用,因为unsubscribe总是首先运行,并且在发送数据之前连接断开。

我需要的是:

  • 当我点击按钮时,我想连接到呼吸机
  • 然后发送所有值
  • 然后断开连接。
  • 如果我重复这个过程,它需要一遍又一遍地做同样的事情。

有人可以帮我一些想法吗?我尝试使用.delay(1000, Time.MILISECONDS)并且它有效,但是将信息发送到呼吸机需要很长时间。

这是我的代码:

 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());
}
4

1 回答 1

0

看起来你不需要在这里写长篇大论。您的数据是否超过 20 个字节?

无论如何,当取消订阅时,库会释放连接Observable<RxBleConnection>。如果我是你,我会做的是:

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());
                    }
            );

请确保您没有过度使用长写。它在 1.2.0 中有一个已知错误(不相关),最近在 1.3.0-SNAPSHOT 中修复。

于 2017-04-05T09:17:23.517 回答