hbldh / bleak

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

Pattern being ignored? #1447

Closed Odud closed 1 month ago

Odud commented 7 months ago

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

dlech commented 7 months ago

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.

Odud commented 7 months ago

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: @.***>

dlech commented 7 months ago

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.

filmkorn commented 2 months ago

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.

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.

dlech commented 2 months ago

Unsure if this is relevant to this issue -

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