RxAndroidBle连接超时

时间:2017-08-22 07:31:26

标签: android bluetooth-lowenergy rxandroidble

我正在连接到已发现的设备并定期读取RSSI。我需要知道连接何时丢失。有没有办法指定连接超时,之后我会收到RxBleConnectionState.DISCONNECTED?现在我仍然从rxBleConnection.readRssi()收到rssi,即使连接的设备已关机。

我应该使用Proximity Bluetooth配置文件中的Link Loss服务而不是连接超时吗?

我的代码:

device1ConnectionSubscription = device.establishConnection(false)
        .doOnUnsubscribe(this::clearDevice1Subscription)
        .observeOn(AndroidSchedulers.mainThread())
        .flatMap(rxBleConnection ->
                Observable.interval(500, TimeUnit.MILLISECONDS)
                        .flatMap(sequence -> rxBleConnection.readRssi())
                        .observeOn(AndroidSchedulers.mainThread())
        )
        .subscribe(this::updateDevice1Rssi, this::onConnectionFailure);


device1ConnectionStateChangesSubscription = device.observeConnectionStateChanges()
        .doOnUnsubscribe(() -> device1ConnectionStateChangesSubscription = null)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
            connectionState -> {
                switch(connectionState)
                {
                    case DISCONNECTING:
                    case DISCONNECTED:
                        if(device1ConnectionSubscription != null) {
                            device1ConnectionSubscription.unsubscribe();
                        }
                        device1ConnectionStateChangesSubscription.unsubscribe();
                        break;
                }
            },
            throwable -> { }
        );

1 个答案:

答案 0 :(得分:3)

您要查找的参数是Connection supervision timeout。此参数由设备控制,我想,无法在手机上设置。