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

Dynamically select default BlueZ adapter if not provided #524

Merged

Conversation

bojanpotocnik
Copy link
Contributor

If specific adapter is not provided as a parameter, default adapter is
selected automatically as a first adapter out of all powered adapter in
the system, instead of using hardcoded "hci0".

Additional logic to only choose powered adapters is added for two
convenience reasons:

  • If adapter is powered down but selected for operation, Bleak will not
    power it on. Consequently, org.bluez.Error.NotReady error will occur
    anyway when trying to use such adapter.
  • If user has multiple adapters present on the system but currently uses
    only some while others are powered off, it most probably wants to use
    one of the active adapters.

Solves #513

@bojanpotocnik bojanpotocnik force-pushed the develop-513-bluez_auto_choose_adapter branch 2 times, most recently from 87b9afb to 0b6d1fd Compare April 25, 2021 00:01
Message(
destination=defs.BLUEZ_SERVICE,
path="/",
member="GetManagedObjects",
Copy link
Collaborator

@dlech dlech Apr 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a very expensive operation. Instead of opening a new D-Bus connection and calling this method, could we make use of the existing copy of this information in the BleakScanner?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added 2 commits (those will be squashed later) which solve this. What do you think? In the afternoon I will also modify client.py, which has 50 lines of duplicate code (this and adding signal handlers).

If specific adapter is not provided as a parameter, default adapter is
selected automatically as a first adapter out of all powered adapter in
the system, instead of using hardcoded "hci0".

Additional logic to only choose powered adapters is added for two
convenience reasons:
- If adapter is powered down but selected for operation, Bleak will not
  power it on. Consequently, `org.bluez.Error.NotReady` error will occur
  anyway when trying to use such adapter.
- If user has multiple adapters present on the system but currently uses
  only some while others are powered off, it most probably wants to use
  one of the active adapters.

Signed-off-by: Bojan Potočnik <info@bojanpotocnik.com>
Function get_default_adapter now also accepts return of a previous
invocation of GetManagedObjects.

Signed-off-by: Bojan Potočnik <info@bojanpotocnik.com>
Signed-off-by: Bojan Potočnik <info@bojanpotocnik.com>
@bojanpotocnik bojanpotocnik force-pushed the develop-513-bluez_auto_choose_adapter branch from 0b6d1fd to fb08a9e Compare April 29, 2021 11:46
@@ -77,8 +78,31 @@ def __init__(self, **kwargs):
async def start(self):
self._bus = await MessageBus(bus_type=BusType.SYSTEM).connect()

# Find the HCI device to use for scanning and get cached device properties
reply = await self._bus.call(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this before attaching the signal listeners will cause a race condition where signals can be missed, so unfortunately this is not going to work.

@dlech
Copy link
Collaborator

dlech commented Jul 25, 2022

We have made some changes recently that should help unblock this PR. There is now a BlueZManager with a global instance that we can use to get the current BlueZ D-Bus properties at any time (well almost any time - it must be in an async function). We should be able to add the proposed get_default_adapter as a method to this class and use it in BleackScanner.start() and BleakClient.connect() as needed.

@dlech dlech merged commit 31d7962 into hbldh:develop Sep 12, 2022
@dlech
Copy link
Collaborator

dlech commented Sep 12, 2022

I made the required changes to get this merged. Thanks for the contribution!

@bojanpotocnik bojanpotocnik deleted the develop-513-bluez_auto_choose_adapter branch November 15, 2022 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: BlueZ Issues and PRs relating to the BlueZ backend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dynamically select default adapter
2 participants