如何在使用 rxandroidble 成功写入特性后取消订阅
How to unsubscribe after a successful writeCharacteristic with rxandroidble
我现在的代码如下图。我想知道在 writeCharacteristic 之后如何取消订阅(断开 ble)?另外,有没有办法在 writeCharacteristic 失败时重新连接?
Subscription subscription = device.establishConnection(false)
.timeout(5000, TimeUnit.MILLISECONDS)
.flatMap(rxBleConnection ->
rxBleConnection.writeCharacteristic(fromString("00005551-0000-1000-8000-008055555fb"), hexToBytes(mData)))
.take(1).retry(2)
.subscribe(
characteristicValue -> {
Log.e(TAG, "write success " + characteristicValue + " " + device.getMacAddress());
// subscribeList.remove(key).unsubscribe();
},
throwable -> {
Log.e(TAG, "write error " + throwable);
// subscribeList.remove(key).unsubscribe();
}
);
只是:
subscription.unsubscribe();
如果我理解正确的话,退订会"bubble up"通过运算符堆栈到顶部,从而断开连接。
I am wondering how do i unsubscribe (disconnect the ble) after connection?
我假设 after connection
是 after the write
,因为如果连接因外部因素(如外围设备关闭)而结束,那么整个流程将以错误结束。
如果您只想写入单个值然后断开连接,那么您需要做的就是在 writeCharacteristic()
之后通过在 .subscribe()
之前使用 .take(1)
从流中获取单个值.
Also, is there a way to reconnect on writeCharacteristic fail?
首先,rxBleConnection.writeCharacteristic()
的失败不会自动关闭连接,自1.3.0版本RxAndroidBle
如果错误与写入本身有关。
您遇到连接被关闭的情况可能只是因为您没有处理写入操作的错误。您可以使用 .retry(2)
运算符使写入重试两次。
请注意您尝试 .retry()
的 Observable
。如果您有兴趣仅在写入失败但连接仍然有效时重试写入,那么您应该在 RxBleConnection.writeCharacteristic()
上应用 .retry()
。另一方面,如果你想在发生任何错误时重试整个流程,那么你应该将 .retry()
放在整个流程上 Observable
.
Subscription subscription = device.establishConnection(false)
.flatMap(rxBleConnection ->
rxBleConnection.writeCharacteristic(
fromString("00005551-0000-1000-8000-008055555fb"),
hexToBytes(mData)
)
.retry(2) // retry here will only retry the write if a write characteristic will fail but the connection will still be intact
)
.take(1) // this will unsubscribe the above part of the Observable on the first valid emission from it—so after the write will complete
// .retry(2) if you will put retry here then for whatever reason the above flow [connecting + writing] will fail then a new connection will be established and write performed
.subscribe(
characteristicValue -> {
Log.e(TAG, "write success " + characteristicValue + " " + device.getMacAddress());
},
throwable -> {
Log.e(TAG, "write error " + throwable);
}
);
我现在的代码如下图。我想知道在 writeCharacteristic 之后如何取消订阅(断开 ble)?另外,有没有办法在 writeCharacteristic 失败时重新连接?
Subscription subscription = device.establishConnection(false)
.timeout(5000, TimeUnit.MILLISECONDS)
.flatMap(rxBleConnection ->
rxBleConnection.writeCharacteristic(fromString("00005551-0000-1000-8000-008055555fb"), hexToBytes(mData)))
.take(1).retry(2)
.subscribe(
characteristicValue -> {
Log.e(TAG, "write success " + characteristicValue + " " + device.getMacAddress());
// subscribeList.remove(key).unsubscribe();
},
throwable -> {
Log.e(TAG, "write error " + throwable);
// subscribeList.remove(key).unsubscribe();
}
);
只是:
subscription.unsubscribe();
如果我理解正确的话,退订会"bubble up"通过运算符堆栈到顶部,从而断开连接。
I am wondering how do i unsubscribe (disconnect the ble) after connection?
我假设 after connection
是 after the write
,因为如果连接因外部因素(如外围设备关闭)而结束,那么整个流程将以错误结束。
如果您只想写入单个值然后断开连接,那么您需要做的就是在 writeCharacteristic()
之后通过在 .subscribe()
之前使用 .take(1)
从流中获取单个值.
Also, is there a way to reconnect on writeCharacteristic fail?
首先,rxBleConnection.writeCharacteristic()
的失败不会自动关闭连接,自1.3.0版本RxAndroidBle
如果错误与写入本身有关。
您遇到连接被关闭的情况可能只是因为您没有处理写入操作的错误。您可以使用 .retry(2)
运算符使写入重试两次。
请注意您尝试 .retry()
的 Observable
。如果您有兴趣仅在写入失败但连接仍然有效时重试写入,那么您应该在 RxBleConnection.writeCharacteristic()
上应用 .retry()
。另一方面,如果你想在发生任何错误时重试整个流程,那么你应该将 .retry()
放在整个流程上 Observable
.
Subscription subscription = device.establishConnection(false)
.flatMap(rxBleConnection ->
rxBleConnection.writeCharacteristic(
fromString("00005551-0000-1000-8000-008055555fb"),
hexToBytes(mData)
)
.retry(2) // retry here will only retry the write if a write characteristic will fail but the connection will still be intact
)
.take(1) // this will unsubscribe the above part of the Observable on the first valid emission from it—so after the write will complete
// .retry(2) if you will put retry here then for whatever reason the above flow [connecting + writing] will fail then a new connection will be established and write performed
.subscribe(
characteristicValue -> {
Log.e(TAG, "write success " + characteristicValue + " " + device.getMacAddress());
},
throwable -> {
Log.e(TAG, "write error " + throwable);
}
);