我正在做一個應用程序,用作使用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