1

是否有一些方法可以检查RxBleConnection实例是否处于活动状态或已断开连接?或者也许是一些通知来跟踪断开连接的时刻?

4

2 回答 2

0

当连接成功时,您在方法 onConnectionReceived 中收到的 RxBleConnection 实例,如果 ble 断开连接,RxBleConnection 将无法执行任何操作,如读/写,可以在回调中捕获以确定连接失败

但是最好的方法是检查连接失败回调它自己确定连接不活跃

谢谢斯威亚姆

于 2016-09-16T18:06:34.573 回答
0

我假设您正试图以RxBleConnection非反应性的方式保持开放。在这种情况下,您需要自己跟踪连接状态。你可以用一个辅助类来做到这一点:

static class ConnectionWithState {
    RxBleConnection connection;
    boolean isActive;
}

建立连接时,只需执行以下操作:

final ConnectionWithState connectionWithState = new ConnectionWithState();
final Subscription connectionSubscription = bleDevice
        .establishConnection(this, false)
        .subscribe(
                connection -> {
                    connectionWithState.connection = connection;
                    connectionWithState.isActive = true;
                },
                throwable -> connectionWithState.isActive = false,
                () -> connectionWithState.isActive = false
        );

断开连接需要您取消订阅connectionSubscription.

此致

于 2016-10-03T10:26:51.703 回答