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

bluez: Implement unpair() method #1067

Merged
merged 1 commit into from
Oct 11, 2022

Conversation

projectgus
Copy link
Contributor

Operates by calling RemoveDevice on the Adapter object, which can be done regardless of the device's connection status.

If the device is connected then calling this method will cause Bluez to disconnect. Calling this method will also cause Bluez to delete any bonding information and GATT attribute cache.

As noted in the previous version of this function, there doesn't seem to be a more direct way to unpair a device in Bluez. It's possible to set the Trusted parameter (readwrite) on a Device object, but not the Paired parameter.

@dlech
Copy link
Collaborator

dlech commented Oct 6, 2022

Nice one! It would be helpful to have some comments explaining the preconditions that lead to the various cases of having/not having self._device_info, etc. How many of these cases have you tested? (unpair while connected, unpair before connected with address arg, unpair before connected with BLEDevice, with/without adapter arg, did I miss any combos?)

@projectgus projectgus force-pushed the feature/bluez_unpair branch 2 times, most recently from 2eebda0 to c9a5d62 Compare October 10, 2022 04:21
@projectgus
Copy link
Contributor Author

projectgus commented Oct 10, 2022

@dlech Thanks for quick review!

Have fixed the issue you found with setting adapter in the constructor, and refactored a little.

I've tested with permutations of the following:

  • BleakClient(str) and BleakClient(BLEDevice)
  • Device previously paired vs device paired as part of the client session
  • unpair() while connected vs unpair() before initiating any connection
  • Passing adapter kwarg to BleakClient() vs not passing (default adapter)

Also added some code to explicitly reset the internal state of the connection after unpairing. Although Bluez sends property changes to update some of this, it seems like calling unpair() and then connect() in the same client object can still fail as the client thinks it doesn't need to re-run discovery but Bluez requires this.

bleak/backends/bluezdbus/client.py Outdated Show resolved Hide resolved
bleak/backends/bluezdbus/client.py Outdated Show resolved Hide resolved
bleak/backends/bluezdbus/client.py Outdated Show resolved Hide resolved
bleak/backends/bluezdbus/client.py Outdated Show resolved Hide resolved
bleak/backends/bluezdbus/client.py Outdated Show resolved Hide resolved
bleak/backends/bluezdbus/client.py Outdated Show resolved Hide resolved
Operates by calling RemoveDevice on the Adapter object, which can be done
regardless of the device's connection status.

If the device is connected then calling this method will cause Bluez to
disconnect. Calling this method will also cause Bluez to delete any bonding
information and GATT attribute cache.

This seems like the only applicable way to unpair a device in Bluez. It's
possible to set the Trusted parameter (readwrite) on a Device object, but not
the Paired parameter.
@projectgus
Copy link
Contributor Author

@dlech Thanks for the careful review and explanatory comments, much appreciated!

I think this should be good to go now. I noticed another quirk while re-testing, which is that even if the RemoveDevice attempt fails (i.e. nothing to remove), any existing BlueZ object still needs to be re-Discovered - so I've reordered the unpair() coroutine a little.

@dlech dlech merged commit 2fba800 into hbldh:develop Oct 11, 2022
@dlech
Copy link
Collaborator

dlech commented Oct 11, 2022

merged, thanks!

@projectgus projectgus deleted the feature/bluez_unpair branch October 11, 2022 19:13
@dlech dlech mentioned this pull request Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants