bleak icon indicating copy to clipboard operation
bleak copied to clipboard

Pattern being ignored?

Open Odud opened this issue 2 years ago • 3 comments

  • Bleak: 0.21.1
  • Python version: 3.12.0
  • Operating System: Debian GNU/Linux 12 (bookworm) (Raspberry Pi)
  • BlueZ version (bluetoothctl -v) in case of Linux: bluetoothctl: 5.66

Description

Trying to only scan for devices from a particular manufacturer, so I used what I thought was the correct argument to the scanner: async with BleakScanner(callback, bluez={"Pattern": "A4:C1:38"}) as scanner:

A4:C1:38 is the start of the MAC addresses that I am iterested in. However all devices seem to be passed back to the callback

Odud avatar Nov 07 '23 10:11 Odud

It sounds like what you want is BleakScanner.find_device_by_filter().

FYI, filtering by address doesn't work on macOS, so if you were trying to make something that works cross platform, you should consider filters on something from the advertisement data instead.

dlech avatar Nov 08 '23 00:11 dlech

That's equivalent to what I do at the moment - I was hoping to offload the filtering to BlueZ

Pete Barlow

On Wed, 8 Nov 2023, 00:02 David Lechner, @.***> wrote:

It sounds like what you want is BleakScanner.find_device_by_filter() https://bleak.readthedocs.io/en/latest/api/scanner.html#bleak.BleakScanner.find_device_by_filter .

FYI, filtering by address doesn't work on macOS, so if you were trying to make something that works cross platform, you should consider filters on something from the advertisement data instead.

— Reply to this email directly, view it on GitHub https://github.com/hbldh/bleak/issues/1447#issuecomment-1800537719, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADXMVZYOYRKWFP6BJJ3FCKDYDLDXRAVCNFSM6AAAAAA7A6SMUWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMBQGUZTONZRHE . You are receiving this because you authored the thread.Message ID: @.***>

Odud avatar Nov 08 '23 07:11 Odud

If the device in question has a service UUID in the advertising data, you ca use the service_uuids keyword arg to BleakScanner() to offload filtering. Filtering by address (and pretty much anything else) can't be offloaded.

dlech avatar Nov 08 '23 13:11 dlech

Unsure if this is relevant to this issue - but 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.

filmkorn avatar Apr 07 '24 19:04 filmkorn

Unsure if this is relevant to this issue -

Not relevant, so i moved it to a new issue.

dlech avatar Apr 07 '24 19:04 dlech