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

cannot receive connection state change after removeBond() sometimes #363

Closed
kkkbird opened this issue Mar 2, 2022 · 6 comments · Fixed by #365 or #571
Closed

cannot receive connection state change after removeBond() sometimes #363

kkkbird opened this issue Mar 2, 2022 · 6 comments · Fixed by #365 or #571
Assignees
Labels

Comments

@kkkbird
Copy link
Contributor

kkkbird commented Mar 2, 2022

removeBond() is called like removeBond().then(callback).enqueue() for checking the bond state after remove bond. and there are two different behaviors after that.

  1. sometimes Connection state changed is logged and onServicesInvalidated is invoked, BOND_STATE_CHANGED is not logged and removeBond callback is not invoked.
V/nordic-lib: Removing bond information...
D/nordic-lib: device.removeBond() (hidden)
D/nordic-lib: [Callback] Connection state changed with status: 0 and new state: 0 (DISCONNECTED)
I/nordic-lib: Disconnected
D/nordic-lib: gatt.close()
D/ble-manager: onServicesInvalidated # self-defined ble manager
  1. sometimes BOND_STATE_CHANGED is received and onServicesInvalidated is not invoked.
V/nordic-lib: Removing bond information...
D/nordic-lib: device.removeBond() (hidden)
D/nordic-lib: [Broadcast] Action received: android.bluetooth.device.action.BOND_STATE_CHANGED, bond state changed to: BOND_NONE (10)
I/nordic-lib: Bond information removed
D/nordic-lib: gatt.close()
D/viewmodel: removeBond callback:F2:0F:0E:FB:CD:DF state:10   # self-defined view model

in the 1st situation, though the removeBond callback is not invoked, we known the device is disconnected and can reconnect the device as designed.
in the 2nd situation, because gatt.close() is called but onServicesInvalidated() is not invoked, we don't known the device is actually disconnected, it leads to some mistake. we have to check connection state again in removeBond callback. I think it should not be a expected result.

after check the source code in nordic ble library, this problem may relate to issue #157. if i remove close() call in BleManagerHandler.java for #157, logs remain same when 1st situation occurs. but for the 2nd one, Connection state changed can be received and onServicesInvalidated is invoked, and the connection state can be well handled.

V/nordic-lib: Removing bond information...
D/nordic-lib: device.removeBond() (hidden)
D/nordic-lib: [Broadcast] Action received: android.bluetooth.device.action.BOND_STATE_CHANGED, bond state changed to: BOND_NONE (10)
I/nordic-lib: Bond information removed
D/viewmodel: removeBond callback:F2:0F:0E:FB:CD:DF state:10
D/nordic-lib: [Callback] Connection state changed with status: 0 and new state: 0 (DISCONNECTED)
I/nordic-lib: Disconnected
D/nordic-lib: gatt.close()
D/ble-manager: onServicesInvalidated
@philips77
Copy link
Member

Thank you for the report. I'll have a look.

@kkkbird
Copy link
Contributor Author

kkkbird commented Mar 3, 2022

Hi @philips77, thank you. btw, there may be other two questions.

  1. if i keep a observer of ObservableBleManager.bondingState and it seems that i never receive NOT_BONDED state in both situation above. can only get correct value by BluetoothDevice.getBondState()
  2. for the 1st situation, i set a then callback but it is not invoked after removeBond request executed, it may lost because of the disconnection but still confusing, as then is defined as will be executed when the request has been processed, no matter the request result.

@philips77
Copy link
Member

Hello, could you try the proposed solution and check if it works? It's a bit difficult to test it, but you may already have a setup for doing that. Could you place logs from 1. and 2. after applying the PR? Is the behaviour as intended?

@philips77
Copy link
Member

Released in 2.4.0-beta03. Please test if it works for you. We're getting ready to release 2.4.0 final next week.

@kkkbird
Copy link
Contributor Author

kkkbird commented Mar 18, 2022

our office has been temporarily closed since last week because of covid-19 :(
hope i can get back to work early next week and i'll test it asap.

@kkkbird
Copy link
Contributor Author

kkkbird commented Jun 7, 2022

hi @philips77

after 2 month lockdown in shanghai, i finally come back to office to test this issue.

  • 2.3.1: unfortunately, after tried same procedure dozens of times, I found that 2nd situation could not be reproduced on my phone (Samsung S21, Android 12). Maybe it was caused by the system upgrading last month.
  • 2.5.0: the behavior is same as 2.3.1 without then() callback being invoked.
I/nordic-lib: Disconnected
D/ble-manager: onServicesInvalidated

Moreover, system BOND broadcast still seemed to not be handled correctly on 2.5.0, If add a log before createBond() and check the log after removeBond() and call createBond() again, bond value looked like below:

ObservableBleManager.bondingState.getValue().getState() : BONDED
ObservableBleManager.getBluetoothDevice().getBondState() : 10 => BOND_NONE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment