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

BleakScanner service_uuids argument not working in BlueZ backend #1534

Closed
dlech opened this issue Apr 7, 2024 · 6 comments · Fixed by #1559
Closed

BleakScanner service_uuids argument not working in BlueZ backend #1534

dlech opened this issue Apr 7, 2024 · 6 comments · Fixed by #1559
Labels
Backend: BlueZ Issues and PRs relating to the BlueZ backend bug Something isn't working

Comments

@dlech
Copy link
Collaborator

dlech commented Apr 7, 2024

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

  • Bleak: 0.21.1
  • Python version: 3.11.7
  • Operating System (uname -a): Linux raspberrypi 6.1.21-v8+ #1642 SMP PREEMPT Mon Apr 3 17:24:16 BST 2023 aarch64 GNU/Linux
  • BlueZ version (bluetoothctl -v) in case of Linux: bluetoothctl: 5.66

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 #1447 (comment)

@dlech dlech added bug Something isn't working Backend: BlueZ Issues and PRs relating to the BlueZ backend labels Apr 7, 2024
@dheerajluffy
Copy link

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
Copy link
Collaborator Author

dlech commented Apr 28, 2024

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

@dlech dlech added more info required Issues does not have a reproducible test case, has insufficent logs or otherwise needs more feedback and removed bug Something isn't working labels Apr 28, 2024
@Dheeraj-Tachyon
Copy link

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
Copy link

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
Copy link
Collaborator Author

dlech commented Apr 29, 2024

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.

@dlech dlech added bug Something isn't working and removed more info required Issues does not have a reproducible test case, has insufficent logs or otherwise needs more feedback labels Apr 29, 2024
dlech added a commit that referenced this issue Apr 30, 2024
It was noted that on Linux, if another app was scanning at the same
time, BlueZ would trigger RSSI changes for all devices, even if they
they didn't match the service_uuids filter.

This change ensures that we always filter by service_uuids, even if
the OS isn't doing it for us.

On Windows, the OS wasn't filtering for us anyway, so we can just move
that code to the shared call_detection_callbacks() so that all backends
will make use of it.

Fixes: #1534
dlech added a commit that referenced this issue Apr 30, 2024
It was noted that on Linux, if another app was scanning at the same
time, BlueZ would trigger RSSI changes for all devices, even if they
they didn't match the service_uuids filter.

This change ensures that we always filter by service_uuids, even if
the OS isn't doing it for us.

On Windows, the OS wasn't filtering for us anyway, so we can just move
that code to the shared call_detection_callbacks() so that all backends
will make use of it.

Fixes: #1534
@dlech dlech mentioned this issue May 4, 2024
@MarkusPiotrowski
Copy link

MarkusPiotrowski commented May 21, 2024

So, service_uuid filtering does work in Windows?
I try to filter on:
service_uuids = ['0000fef3-0000-1000-8000-00805f9b34fb']
like this:

...
service_uuids = ['0000fef3-0000-1000-8000-00805f9b34fb']
# Without filter
async with Scanner(service_uuids=None) as scanner:
    await asyncio.sleep(10)
    self.show_scan_result(
        scanner.discovered_devices_and_advertisement_data
    )
# With filter
async with Scanner(service_uuids=service_uuids) as scanner:
    await asyncio.sleep(10)
    self.show_scan_result(
        scanner.discovered_devices_and_advertisement_data
    )
...

(where show_scan_results is a method to display the results).
However, I can't see a difference in the devices beeing found, and some of them don't have the service uuid in their AdvertisementData.
Tested with 0.21.1 and 0.22.1 on Windows 10.

Am I doing something wrong or do I misunderstand the concept of service UUID filtering?

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 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants