Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RxBle is not show disconnected when BluetoothGatt: android.os.DeadObjectException is thrown #275

Closed
hoanglm4 opened this issue Sep 1, 2017 · 16 comments
Assignees
Labels
bug Bug that is caused by the library
Milestone

Comments

@hoanglm4
Copy link

hoanglm4 commented Sep 1, 2017

Summary

CONNECTED state forever when BluetoothGatt: android.os.DeadObjectException is thrown

My code in below:

   private static final UUID characteristicUuid = UUID.fromString("25A80001-6478-11E6-BDF4-AD3BCC77759F");

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example4);
        ButterKnife.bind(this);
        bleDevice = SampleApplication.getRxBleClient(this).getBleDevice(macAddress);
        connectionObservable = prepareConnectionObservable();
        bleDevice.observeConnectionStateChanges().subscribe(state -> {
            Log.i(getClass().getSimpleName(), "" + state.toString());
        });
        connect();
    }

    private Observable<RxBleConnection> prepareConnectionObservable() {
        return bleDevice
                .establishConnection(false)
                .takeUntil(disconnectTriggerSubject)
                .compose(bindUntilEvent(PAUSE))
                .compose(new ConnectionSharingAdapter());
    }

    @OnClick(R.id.connect)
    public void connect() {
        connectionObservable.flatMap(RxBleConnection::discoverServices)
                .flatMap(rxBleDeviceServices -> rxBleDeviceServices.getCharacteristic(characteristicUuid))
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(() -> Log.i(getClass().getSimpleName(), "start connect "))
                .subscribe(
                        characteristic -> {
                            Log.i(getClass().getSimpleName(), "Hey, connection has been established!");
                        },
                        this::onConnectionFailure,
                        this::onConnectionFinished
                );
    }

   @OnClick(R.id.dissconnect)
    public void triggerDisconnect() {
        disconnectTriggerSubject.onNext(null);
    }

     @OnClick(R.id.read_rssi)
    public void readRSSI() {
        if (bleDevice.getConnectionState() != RxBleConnection.RxBleConnectionState.CONNECTED) {
            return;
        }
        connectionObservable.flatMap(rxBleConnection -> rxBleConnection.readRssi())
                .subscribe(rssi -> {
                    Log.i(getClass().getSimpleName(), "RSSI = " + rssi);
                }, throwable -> {
                    Log.i(getClass().getSimpleName(), "Connection error: " + throwable);
                });
    }
  • Sometime, when Bluetooth in phone is crashed => I call readRSSI => BluetoothGatt: android.os.DeadObjectException is thrown. But RxBleDevice#getConnectionState() return RxBleConnectionState{CONNECTED}.
  • When call readRSSI again, BluetoothGatt: android.os.DeadObjectException still thown.
  • After, I call triggerDisconnect() method => connect() method=> try to readRSSI, and it work fine.

Actual result

BluetoothGatt: android.os.DeadObjectException is thown
Please see log:

09-01 11:49:24.135 13204-13304/com.mysoftsource.vuzitech D/RxBle#Radio:   QUEUED RxBleRadioOperationReadRssi(28807445)
09-01 11:49:24.136 13204-13302/com.mysoftsource.vuzitech D/RxBle#Radio:  STARTED RxBleRadioOperationReadRssi(28807445)
09-01 11:49:24.138 13204-13204/com.mysoftsource.vuzitech D/BluetoothGatt: readRssi() - device: F7:A2:68:4B:DA:D3
09-01 11:49:24.139 13204-13204/com.mysoftsource.vuzitech E/BluetoothGatt: android.os.DeadObjectException
                                                                              at android.os.BinderProxy.transactNative(Native Method)
                                                                              at android.os.BinderProxy.transact(Binder.java:615)
                                                                              at android.bluetooth.IBluetoothGatt$Stub$Proxy.readRemoteRssi(IBluetoothGatt.java:1006)
                                                                              at android.bluetooth.BluetoothGatt.readRemoteRssi(BluetoothGatt.java:1121)
                                                                              at com.polidea.rxandroidble.internal.operations.RxBleRadioOperationReadRssi.startOperation(RxBleRadioOperationReadRssi.java:30)
                                                                              at com.polidea.rxandroidble.internal.RxBleSingleGattRadioOperation.protectedRun(RxBleSingleGattRadioOperation.java:56)
                                                                              at com.polidea.rxandroidble.internal.RxBleRadioOperation$1.call(RxBleRadioOperation.java:39)
                                                                              at com.polidea.rxandroidble.internal.RxBleRadioOperation$1.call(RxBleRadioOperation.java:35)
                                                                              at rx.internal.operators.OnSubscribeCreate.call(OnSubscribeCreate.java:72)
                                                                              at rx.internal.operators.OnSubscribeCreate.call(OnSubscribeCreate.java:32)
                                                                              at rx.Observable.unsafeSubscribe(Observable.java:10256)
                                                                              at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(OperatorSubscribeOn.java:100)
                                                                              at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
                                                                              at android.os.Handler.handleCallback(Handler.java:751)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6126)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
09-01 11:49:24.141 13204-13302/com.mysoftsource.vuzitech D/RxBle#Radio: FINISHED RxBleRadioOperationReadRssi(28807445)
09-01 11:49:25.134 13204-13304/com.mysoftsource.vuzitech D/RxBle#Radio:   QUEUED RxBleRadioOperationReadRssi(189325052)
09-01 11:49:25.136 13204-13302/com.mysoftsource.vuzitech D/RxBle#Radio:  STARTED RxBleRadioOperationReadRssi(189325052)
09-01 11:49:25.138 13204-13204/com.mysoftsource.vuzitech D/BluetoothGatt: readRssi() - device: F7:A2:68:4B:DA:D3
09-01 11:49:25.140 13204-13204/com.mysoftsource.vuzitech E/BluetoothGatt: android.os.DeadObjectException
                                                                              at android.os.BinderProxy.transactNative(Native Method)
                                                                              at android.os.BinderProxy.transact(Binder.java:615)
                                                                              at android.bluetooth.IBluetoothGatt$Stub$Proxy.readRemoteRssi(IBluetoothGatt.java:1006)
                                                                              at android.bluetooth.BluetoothGatt.readRemoteRssi(BluetoothGatt.java:1121)
                                                                              at com.polidea.rxandroidble.internal.operations.RxBleRadioOperationReadRssi.startOperation(RxBleRadioOperationReadRssi.java:30)
                                                                              at com.polidea.rxandroidble.internal.RxBleSingleGattRadioOperation.protectedRun(RxBleSingleGattRadioOperation.java:56)
                                                                              at com.polidea.rxandroidble.internal.RxBleRadioOperation$1.call(RxBleRadioOperation.java:39)
                                                                              at com.polidea.rxandroidble.internal.RxBleRadioOperation$1.call(RxBleRadioOperation.java:35)
                                                                              at rx.internal.operators.OnSubscribeCreate.call(OnSubscribeCreate.java:72)
                                                                              at rx.internal.operators.OnSubscribeCreate.call(OnSubscribeCreate.java:32)
                                                                              at rx.Observable.unsafeSubscribe(Observable.java:10256)
                                                                              at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(OperatorSubscribeOn.java:100)
                                                                              at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
                                                                              at android.os.Handler.handleCallback(Handler.java:751)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6126)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
09-01 11:49:25.141 13204-13302/com.mysoftsource.vuzitech D/RxBle#Radio: FINISHED RxBleRadioOperationReadRssi(189325052)

Expected result

When BluetoothGatt: android.os.DeadObjectException is thrown => call disconnect and shown DISCONNECTED state

Library version

1.3.3

@hoanglm4 hoanglm4 changed the title Ble is not disconnected when BluetoothGatt: android.os.DeadObjectException is thrown RxBle is not show disconnected when BluetoothGatt: android.os.DeadObjectException is thrown Sep 1, 2017
@dariuszseweryn
Copy link
Owner

Could you check against 1.4.0-SNAPSHOT version? The code related to reporting state has changed there.

@hoanglm4
Copy link
Author

hoanglm4 commented Sep 1, 2017

Dear @dariuszseweryn,

I have checked this issue in 1.4.0-SNAPSHOT, this bug is occur too. However, Bluetooth is crashed on Samsung Note II (rooted). I can not make bluetooth on Samsung J7(or HTC U11 because they are not rooted) is crashed.

Thanks

@dariuszseweryn dariuszseweryn added the bug Bug that is caused by the library label Sep 1, 2017
dariuszseweryn added a commit that referenced this issue Sep 5, 2017
`DeadObjectException` is usually thrown when interacting with `BluetoothAdapter` or `BluetoothGatt` instance that was obtained before bluetooth being turned off. Prior to library version `1.3.0` all errors raised when interacting with `BluetoothGatt` or recieved by `BluetoothGattCallback` were closing the connection (were emitted by `RxBleDevice.establishConnection()`). After `1.3.0` only errors raised in `RxBleRadioOperationConnect` and recieved by `BluetoothGattCallback.onConnectionStateChange()` were closing the connection so it was possible that `DeadObjectException`s raised repetedly by subscribing to i.e. `RxBleConnection.readRssi()` would not close the connection. Added monitoring of BluetoothAdapter’s state to prevent `DeadObjectException`s and inform the user about the connection loss as soon as possible.
@dariuszseweryn dariuszseweryn added this to the 1.3.4 milestone Sep 5, 2017
dariuszseweryn added a commit that referenced this issue Sep 11, 2017
`DeadObjectException` is usually thrown when interacting with `BluetoothAdapter` or `BluetoothGatt` instance that was obtained before bluetooth being turned off. Prior to library version `1.3.0` all errors raised when interacting with `BluetoothGatt` or recieved by `BluetoothGattCallback` were closing the connection (were emitted by `RxBleDevice.establishConnection()`). After `1.3.0` only errors raised in `RxBleRadioOperationConnect` and recieved by `BluetoothGattCallback.onConnectionStateChange()` were closing the connection so it was possible that `DeadObjectException`s raised repetedly by subscribing to i.e. `RxBleConnection.readRssi()` would not close the connection. Added monitoring of BluetoothAdapter’s state to prevent `DeadObjectException`s and inform the user about the connection loss as soon as possible.
Moved responsibility of checking the BluetoothAdapter’s state from Connector to DisconnectionRouter to have it in one place.
@dariuszseweryn
Copy link
Owner

dariuszseweryn commented Sep 11, 2017

@hoanglm4 I have just pushed a new version of 1.3.4-SNAPSHOT — it should now properly handle situation when BluetoothAdapter is turned off. Could you check it out?

Edit: it is a new 1.3.4-SNAPSHOT version

@hoanglm4
Copy link
Author

Dear @dariuszseweryn , thank you so much. I will check your fixed in tomorrow (currently, my country in midnight, so I don't have device for check it)

@hoanglm4
Copy link
Author

hoanglm4 commented Sep 12, 2017

Dear @dariuszseweryn,
I can not connect to ble device. "java.lang.NullPointerException: Cannot return null from a non-@nullable @provides method" is thown when connect() method called

Version: 1.3.4-SNAPSHOT

Note: I using 1.3.3, this issue is not occur
Thanks

dariuszseweryn added a commit that referenced this issue Sep 12, 2017
…278)

`RxBleConnection` can be instantiated only after `BluetoothGatt` is available. Changed the Connector code from `Observable.just(connectionComponent.rxBleConnection()` to `Observable.fromCallable(connectionComponent::rxBleConnection)` equivalent.
@dariuszseweryn
Copy link
Owner

@hoanglm4 my bad — just pushed a fix. There should be a new version of 1.3.4-SNAPSHOT available in around 10 minutes. Feel free to re-check.

@hoanglm4
Copy link
Author

Dear @dariuszseweryn,

I have tested this issue. "DISCONNECTED" state is shown when bluetooth is crashed. I think your fixed is OK.

Thanks

@dariuszseweryn
Copy link
Owner

I am happy to hear that. :) Thank you for your report.

@dariuszseweryn
Copy link
Owner

@hoanglm4 I have just released an official 1.3.4 version :)
Best Regards

dariuszseweryn added a commit that referenced this issue Sep 12, 2017
…ConnectOperation` will finish (#275) (#278)

The `RxBleConnection` needs `BluetoothGatt` to be created. The `BluetoothGatt` is available only after the `ConnectOperation` will call `BluetoothDevice.connectGatt()` or `BluetoothGattCallback.onConnectionStateChange()` will be called whichever comes first.
@hoanglm4
Copy link
Author

Dear @dariuszseweryn,
Great! When you release 1.4.0?

Thanks

@dariuszseweryn
Copy link
Owner

I will probably try to release it tomorrow.

@dariuszseweryn
Copy link
Owner

FYI—it is released.

@hoanglm4
Copy link
Author

Great! Thank you so much :)

@dariuszseweryn
Copy link
Owner

I am afraid that this has nothing to do with the library judging by the stacktrace.

@omeryounus
Copy link

Okay I am going to remove it

@omeryounus
Copy link

I have seen this exception happening on some low end Android phones when calling gatt.disconnect method
i:e - E/BluetoothGatt: android.os.DeadObjectException
I am sure if there is any properly handling in library that would prevent it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug that is caused by the library
Projects
None yet
Development

No branches or pull requests

3 participants