-
Notifications
You must be signed in to change notification settings - Fork 304
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
Dynamically select default BlueZ adapter if not provided #524
Conversation
87b9afb
to
0b6d1fd
Compare
bleak/backends/bluezdbus/utils.py
Outdated
Message( | ||
destination=defs.BLUEZ_SERVICE, | ||
path="/", | ||
member="GetManagedObjects", |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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>
0b6d1fd
to
fb08a9e
Compare
bleak/backends/bluezdbus/scanner.py
Outdated
@@ -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( |
There was a problem hiding this comment.
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.
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 |
also fix up debug messages that referenced self._adapter
I made the required changes to get this merged. Thanks for the contribution! |
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:
power it on. Consequently,
org.bluez.Error.NotReady
error will occuranyway when trying to use such adapter.
only some while others are powered off, it most probably wants to use
one of the active adapters.
Solves #513