首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在设备超时(使用RxAndroidBle )之后,RxAndroidBle不再连接

在设备超时(使用RxAndroidBle )之后,RxAndroidBle不再连接
EN

Stack Overflow用户
提问于 2017-04-21 11:02:51
回答 1查看 1.7K关注 0票数 0

我正在使用RxAndroidBle库开发RxAndroidBle。如果没有交互,BLE设备的超时时间为5分钟。也就是说,如果没有读写操作5分钟,设备将自动断开连接。当超时断开发生时,我将得到BleGattException。

代码语言:javascript
复制
     W/System.err: BleGattException{macAddress=24:71:89:FE:A0:88, status=19 (0x13 -> https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.1.0_r1/stack/include/gatt_api.h), bleGattOperationType=BleGattOperation{description='CONNECTION_STATE'}}
 W/System.err:     at com.polidea.rxandroidble.internal.connection.RxBleGattCallback.propagateStatusErrorIfGattErrorOccurred(RxBleGattCallback.java:233)
 W/System.err:     at com.polidea.rxandroidble.internal.connection.RxBleGattCallback.access$200(RxBleGattCallback.java:35)
 W/System.err:     at com.polidea.rxandroidble.internal.connection.RxBleGattCallback$3.onConnectionStateChange(RxBleGattCallback.java:86)
 W/System.err:     at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:228)
 W/System.err:     at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70)
 W/System.err:     at android.os.Binder.execTransact(Binder.java:453)

 W/BluetoothGatt: Unhandled exception in callback
                                                                  rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError
                                                                      at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:194)
                                                                      at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:115)
                                                                      at rx.observers.Subscribers$5.onError(Subscribers.java:230)
                                                                      at rx.observers.Subscribers$5.onError(Subscribers.java:230)
                                                                      at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onError(OnSubscribeDoOnEach.java:87)
                                                                      at rx.observers.Subscribers$5.onError(Subscribers.java:230)
                                                                      at rx.observers.Subscribers$5.onError(Subscribers.java:230)
                                                                      at rx.observers.Subscribers$5.onError(Subscribers.java:230)
                                                                      at rx.internal.operators.OperatorMerge$MergeSubscriber.reportError(OperatorMerge.java:266)
                                                                      at rx.internal.operators.OperatorMerge$MergeSubscriber.checkTerminate(OperatorMerge.java:818)
                                                                      at rx.internal.operators.OperatorMerge$MergeSubscriber.emitLoop(OperatorMerge.java:579)
                                                                      at rx.internal.operators.OperatorMerge$MergeSubscriber.emit(OperatorMerge.java:568)
                                                                      at rx.internal.operators.OperatorMerge$InnerSubscriber.onError(OperatorMerge.java:852)
                                                                      at rx.internal.operators.OperatorMerge$MergeSubscriber.reportError(OperatorMerge.java:266)
                                                                      at rx.internal.operators.OperatorMerge$MergeSubscriber.checkTerminate(OperatorMerge.java:818)
                                                                      at rx.internal.operators.OperatorMerge$MergeSubscriber.emitLoop(OperatorMerge.java:579)
                                                                      at rx.internal.operators.OperatorMerge$MergeSubscriber.emit(OperatorMerge.java:568)
                                                                      at rx.internal.operators.OperatorMerge$InnerSubscriber.onError(OperatorMerge.java:852)
                                                                      at rx.internal.operators.OperatorMerge$MergeSubscriber.reportError(OperatorMerge.java:266)
                                                                      at rx.internal.operators.OperatorMerge$MergeSubscriber.checkTerminate(OperatorMerge.java:818)
                                                                      at rx.internal.operators.OperatorMerge$MergeSubscriber.emitLoop(OperatorMerge.java:579)
                                                                      at rx.internal.operators.OperatorMerge$MergeSubscriber.emit(OperatorMerge.java:568)
                                                                      at rx.internal.operators.OperatorMerge$InnerSubscriber.onError(OperatorMerge.java:852)
                                                                      at rx.internal.operators.NotificationLite.accept(NotificationLite.java:132)
                                                                      at rx.subjects.SubjectSubscriptionManager$SubjectObserver.emitNext(SubjectSubscriptionManager.java:253)
                                                                      at rx.subjects.BehaviorSubject.onError(BehaviorSubject.java:141)
                                                                      at com.polidea.rxandroidble.internal.connection.RxBleGattCallback.propagateStatusError(RxBleGattCallback.java:241)
                                                                      at com.polidea.rxandroidble.internal.connection.RxBleGattCallback.propagateStatusErrorIfGattErrorOccurred(RxBleGattCallback.java:233)
                                                                      at com.polidea.rxandroidble.internal.connection.RxBleGattCallback.access$200(RxBleGattCallback.java:35)
                                                                      at com.polidea.rxandroidble.internal.connection.RxBleGattCallback$3.onConnectionStateChange(RxBleGattCallback.java:86)
                                                                      at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:228)
                                                                      at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70)
                                                                      at android.os.Binder.execTransact(Binder.java:453)
                                                                   Caused by: rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError
                                                                      at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:187)
                                                                      at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:115)
                                                                      at rx.exceptions.Exceptions.throwOrReport(Exceptions.java:216)
                                                                      at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139)
                                                                      at rx.internal.operators.OperatorDistinctUntilChanged$1.onNext(OperatorDistinctUntilChanged.java:96)
                                                                      at rx.internal.operators.NotificationLite.accept(NotificationLite.java:135)
                                                                      at rx.subjects.SubjectSubscriptionManager$SubjectObserver.emitNext(SubjectSubscriptionManager.java:253)
                                                                      at rx.subjects.BehaviorSubject.onNext(BehaviorSubject.java:160)
                                                                      at com.polidea.rxandroidble.internal.RxBleDeviceImpl$1$1.call(RxBleDeviceImpl.java:77)
                                                                      at rx.subscriptions.BooleanSubscription.unsubscribe(BooleanSubscription.java:71)
                                                                      at rx.internal.util.SubscriptionList.unsubscribeFromAll(SubscriptionList.java:136)
                                                                      at rx.internal.util.SubscriptionList.unsubscribe(SubscriptionList.java:125)

一旦设备因超时而断开连接,当我再次扫描时,可以按预期的方式在附近找到可用的设备。但是,当我试图连接到同一个设备时,它会抛出一个已连接的异常通知设备。

代码语言:javascript
复制
 W/System.err: BleAlreadyConnectedException{macAddress=24:71:89:FE:99:E3}
 W/System.err:     at com.polidea.rxandroidble.internal.RxBleDeviceImpl.lambda$establishConnection$3(RxBleDeviceImpl.java:54)
 W/System.err:     at com.polidea.rxandroidble.internal.RxBleDeviceImpl$$Lambda$1.call(Unknown Source)
 W/System.err:     at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)
 W/System.err:     at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)
 W/System.err:     at rx.Observable.subscribe(Observable.java:10236)
 W/System.err:     at rx.Observable.subscribe(Observable.java:10203)
 W/System.err:     at rx.Observable.subscribe(Observable.java:10043)

但是,如果我在手机中关闭和打开蓝牙,或者关闭应用程序并重新启动,我就能够连接到设备上。

当我手动断开设备并再次连接时,和重新连接正在工作.只有当超时断开时才会出现问题。

有人能告诉我为什么会发生这种事吗?

EN

回答 1

Stack Overflow用户

发布于 2017-04-21 11:39:48

从错误和.establishConnection()函数的实现来看,您没有从返回的前一个Observable取消订阅。

代码语言:javascript
复制
@Override
public Observable<RxBleConnection> establishConnection(final boolean autoConnect) {
    return Observable.defer(new Func0<Observable<RxBleConnection>>() {
        @Override
        public Observable<RxBleConnection> call() {

            if (isConnected.compareAndSet(false, true)) {
                return connector.prepareConnection(autoConnect)
                        .doOnSubscribe(new Action0() {
                            @Override
                            public void call() {
                                connectionStateSubject.onNext(CONNECTING);
                            }
                        })
                        .doOnNext(new Action1<RxBleConnection>() {
                            @Override
                            public void call(RxBleConnection rxBleConnection) {
                                connectionStateSubject.onNext(CONNECTED);
                            }
                        })
                        .doOnUnsubscribe(new Action0() {
                            @Override
                            public void call() {
                                connectionStateSubject.onNext(DISCONNECTED);
                                isConnected.set(false);
                            }
                        });
            } else {
                return Observable.error(new BleAlreadyConnectedException(bluetoothDevice.getAddress()));
            }
        }
    });
}

一旦您从上一个取消订阅,您应该能够建立一个新的连接。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43541050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档