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

Handle a device already being connected with the BlueZ Dbus backend #994

Merged
merged 5 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Changed
To use the cache, call ``connect`` and ``get_services`` with the ``dangerous_use_bleak_cache``
argument to avoid services being resolved again.
* The BlueZ D-Bus backend now uses ``dbus-fast`` instead of ``dbus-next`` which significantly improves performance.
* The BlueZ D-Bus backend will not avoid trying to connect to devices that are already connected.
dlech marked this conversation as resolved.
Show resolved Hide resolved

Fixed
-----
Expand Down
27 changes: 18 additions & 9 deletions bleak/backends/bluezdbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,25 @@ def on_value_changed(char_path: str, value: bytes) -> None:

try:
try:
async with async_timeout.timeout(timeout):
reply = await self._bus.call(
Message(
destination=defs.BLUEZ_SERVICE,
interface=defs.DEVICE_INTERFACE,
path=self._device_path,
member="Connect",
#
# The BlueZ backend does not disconnect devices when the
# application closes or crashes. This can cause problems
# when trying to reconnect to the same device. To work
# around this, we check if the device is already connected.
#
# For additional details see https://github.com/bluez/bluez/issues/89
#
if not manager.is_connected(self._device_path):
bdraco marked this conversation as resolved.
Show resolved Hide resolved
async with async_timeout.timeout(timeout):
reply = await self._bus.call(
Message(
destination=defs.BLUEZ_SERVICE,
interface=defs.DEVICE_INTERFACE,
path=self._device_path,
member="Connect",
)
)
)
assert_reply(reply)
assert_reply(reply)

self._is_connected = True

Expand Down
15 changes: 15 additions & 0 deletions bleak/backends/bluezdbus/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,21 @@ def get_device_name(self, device_path: str) -> str:
"""
return self._properties[device_path][defs.DEVICE_INTERFACE]["Name"]

def is_connected(self, device_path: str) -> bool:
"""
Gets the value of the "Connected" property for a device.

Args:
device_path: The D-Bus object path of the device.

Returns:
The current property value.
"""
try:
return self._properties[device_path][defs.DEVICE_INTERFACE]["Connected"]
except KeyError:
return False

async def _wait_condition(
self, device_path: str, property_name: str, property_value: Any
) -> None:
Expand Down