Skip to content

Commit cefe9d8

Browse files
committed
bleak: use backend-specific key for seen_devices
Replace use of address with backend-specific key for the seen_devices dictionary. As we found out, in the BlueZ backend, the address may not match the D-Bus path in the case of private resolvable addresses. So to avoid problems with converting the address to a D-Bus path, we need to use the D-Bus path as the key in the seen_devices dictionary. For good measure, we also use the macOS UUID for the device even when using the use_bdaddr hack to get the actual address. Other backends continue to use the address as the key, as there doesn't appear to be any better alternative.
1 parent fa784a1 commit cefe9d8

7 files changed

Lines changed: 17 additions & 21 deletions

File tree

bleak/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def discovered_devices_and_advertisement_data(
292292
293293
.. versionadded:: 0.19
294294
"""
295-
return self._backend.seen_devices
295+
return {d[0].address: d for d in self._backend.seen_devices.values()}
296296

297297
@classmethod
298298
async def find_device_by_address(

bleak/backends/bluezdbus/scanner.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from bleak.args.bluez import BlueZScannerArgs
2020
from bleak.backends.bluezdbus.defs import Device1
2121
from bleak.backends.bluezdbus.manager import get_global_bluez_manager
22-
from bleak.backends.bluezdbus.utils import bdaddr_from_device_path
2322
from bleak.backends.scanner import (
2423
AdvertisementData,
2524
AdvertisementDataCallback,
@@ -199,6 +198,7 @@ def _handle_advertising_data(self, path: str, props: Device1) -> None:
199198
)
200199

201200
device = self.create_or_update_device(
201+
path,
202202
props["Address"],
203203
# BlueZ generates a name based on the address if no name is available.
204204
# To match other backends, we replace this with None.
@@ -218,8 +218,7 @@ def _handle_device_removed(self, device_path: str) -> None:
218218
Handles a device being removed from BlueZ.
219219
"""
220220
try:
221-
bdaddr = bdaddr_from_device_path(device_path)
222-
del self.seen_devices[bdaddr]
221+
del self.seen_devices[device_path]
223222
except KeyError:
224223
# The device will not have been added to self.seen_devices if no
225224
# advertising data was received, so this is expected to happen

bleak/backends/bluezdbus/utils.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,6 @@ def extract_service_handle_from_path(path: str) -> int:
3535
raise BleakError(f"Could not parse service handle from path: {path}") from e
3636

3737

38-
def bdaddr_from_device_path(device_path: str) -> str:
39-
"""
40-
Scrape the Bluetooth address from a D-Bus device path.
41-
42-
Args:
43-
device_path: The D-Bus object path of the device.
44-
45-
Returns:
46-
A Bluetooth address as a string.
47-
"""
48-
return ":".join(device_path[-17:].split("_"))
49-
50-
5138
def device_path_from_characteristic_path(characteristic_path: str) -> str:
5239
"""
5340
Scrape the device path from a D-Bus characteristic path.

bleak/backends/corebluetooth/scanner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def callback(p: CBPeripheral, a: NSDictionary, r: int) -> None:
148148
address = p.identifier().UUIDString()
149149

150150
device = self.create_or_update_device(
151+
p.identifier().UUIDString(),
151152
address,
152153
p.name(),
153154
(p, self._manager.central_manager.delegate()),

bleak/backends/p4android/scanner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ def _handle_scan_result(self, result) -> None:
267267
)
268268

269269
device = self.create_or_update_device(
270+
native_device.getAddress(),
270271
native_device.getAddress(),
271272
native_device.getName(),
272273
native_device,

bleak/backends/scanner.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ class BaseBleakScanner(abc.ABC):
115115
"""
116116
Map of device identifier to BLEDevice and most recent advertisement data.
117117
118+
The key is a backend-specific identifier for the device.
119+
118120
This map must be cleared when scanning starts.
119121
"""
120122

@@ -236,12 +238,18 @@ def call_detection_callbacks(
236238
callback(device, advertisement_data)
237239

238240
def create_or_update_device(
239-
self, address: str, name: Optional[str], details: Any, adv: AdvertisementData
241+
self,
242+
key: str,
243+
address: str,
244+
name: Optional[str],
245+
details: Any,
246+
adv: AdvertisementData,
240247
) -> BLEDevice:
241248
"""
242249
Creates or updates a device in :attr:`seen_devices`.
243250
244251
Args:
252+
key: A backend-specific identifier for the device.
245253
address: The Bluetooth address of the device (UUID on macOS).
246254
name: The OS display name for the device.
247255
details: The platform-specific handle for the device.
@@ -252,13 +260,13 @@ def create_or_update_device(
252260
"""
253261

254262
try:
255-
device, _ = self.seen_devices[address]
263+
device, _ = self.seen_devices[key]
256264

257265
device.name = name
258266
except KeyError:
259267
device = BLEDevice(address, name, details)
260268

261-
self.seen_devices[address] = (device, adv)
269+
self.seen_devices[key] = (device, adv)
262270

263271
return device
264272

bleak/backends/winrt/scanner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def _received_handler(
212212
)
213213

214214
device = self.create_or_update_device(
215-
bdaddr, local_name, raw_data, advertisement_data
215+
bdaddr, bdaddr, local_name, raw_data, advertisement_data
216216
)
217217

218218
self.call_detection_callbacks(device, advertisement_data)

0 commit comments

Comments
 (0)