我正在做一个应用程序,用作使用RxAndroidBle的呼吸机的遥控器。我与unsubscribe
一个问题,因为当我使用取消订阅的问题
.flatMap(rxBleConnection -> rxBleConnection.writeCharacteristic(Uuids.UUID_RX, flaktCommandConcat.getBytes()))
,之后我用subscription.unsubscribe();
doesn't工作writeCharacteristics因为unsubscribe
运行总是先和数据之前的连接disconect发送。
我需要的是:
- 当我点击我要连接呼吸机
- 的按钮,然后将所有值
- 然后断开连接。
- 如果我重复这个过程,它将需要一遍又一遍地做同样的事情。
有人能帮我一些想法吗?我试图使用.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());
}
您在描述中提到了'MILISECONDS',但在代码中使用了'MILLISECONDS' - 如果其中一个拼写错误,请修复它。 – halfer