hbldh / bleak

A cross platform Bluetooth Low Energy Client for Python using asyncio
MIT License
1.56k stars 275 forks source link

BleakScanner service_uuids argument not working in BlueZ backend #1534

Closed dlech closed 2 weeks ago

dlech commented 1 month ago

I found that on bluez the service_uuids argument is ignored and the callback is called on non-matching devices.

Example:

import asyncio

from bleak import BleakScanner, AdvertisementData, BLEDevice

SERVICE_UUID = "0e140000-0af1-4582-a242-773e63054c68"

async def detection_callback(device: BLEDevice, advertisement_data: AdvertisementData):
    print(f"Found device: {device.address} (device.name)")
    print(f"Matches service UUID: {SERVICE_UUID in advertisement_data.service_uuids}")

async def discover():
    scanner = BleakScanner(
        detection_callback=detection_callback,
        use_bdaddr=False,
        service_uuids=[SERVICE_UUID],
    )

    await scanner.start()
    await asyncio.sleep(20)

asyncio.run(discover())

Results:

Found device: 68:27:37:56:DD:C3 - [AV] Samsung Soundbar MS650
Matches service UUID: False
Found device: 0A:9F:E3:B6:74:C9 - 0A-9F-E3-B6-74-C9
Matches service UUID: False
Found device: 00:1B:66:E0:80:F0 - MOMENTUM 3
Matches service UUID: False

This doesn't happen on Windows in my tests.

Originally posted by @filmkorn in https://github.com/hbldh/bleak/issues/1447#issuecomment-2041572075

dheerajluffy commented 3 weeks ago

is this anyway related to the issue, i.e not able to list UUIDS of some LE devices? I have been facing this issue from a week.

dlech commented 2 weeks ago

I am not able to reproduce this issue. I tried:

python examples/detection_callback.py --services 0e140000-0af1-4582-a242-773e63054c68

And I get no callbacks.

BlueZ version: 5.64

Dheeraj-Tachyon commented 2 weeks ago

I am not able to reproduce this issue. I tried:

python examples/detection_callback.py --services 0e140000-0af1-4582-a242-773e63054c68

And I get no callbacks.

BlueZ version: 5.64

Could it be a bug in bluez user space?

filmkorn commented 2 weeks ago

python examples/detection_callback.py --services 0e140000-0af1-4582-a242-773e63054c68

I just gave that command a try. Found that the simple callback is only triggered for other service ids if home assistant is running (has a bleak based bluetooth integration). My guess is that home assistant somehow removes the service id filter.

dlech commented 2 weeks ago

If another application is scanning, then yes the filter could be removed. I suppose that we could add an additional filter in Bleak to get the expected results even when this happens.