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

Device returned by detection callback is not properly initialized on corebluetooth #448

Closed
nonameforgood opened this issue Feb 17, 2021 · 1 comment · Fixed by #534
Closed
Labels
Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend bug Something isn't working

Comments

@nonameforgood
Copy link

nonameforgood commented Feb 17, 2021

  • bleak version:0.10.0
  • Python version:3.7.2
  • Operating System:MacOS Catalina 10.15.2
  • BlueZ version (bluetoothctl -v) in case of Linux:

Description

The device returned by the detection callback is missing "delegate" which is needed by the ctor of BleakClientCoreBluetooth

What I Did

I work around the issue by not using the callback and repeatedly calling get_discovered_devices instead.

Sample (modified from bleak/examples/detection_callback.py):

import asyncio
from bleak import BleakScanner
from bleak import BleakClient
from bleak.backends.device import BLEDevice
from bleak.backends.scanner import AdvertisementData
import logging

logging.basicConfig()

dev = None

def simple_callback(device: BLEDevice, advertisement_data: AdvertisementData):
    global dev
    dev = device
    print(device.address, "RSSI:", device.rssi, advertisement_data)


async def run():
    scanner = BleakScanner()
    scanner.register_detection_callback(simple_callback)

    while True:
        await scanner.start()
        await asyncio.sleep(5.0)
        await scanner.stop()

        global dev
        if dev is not None:
            async with BleakClient(dev) as client:
                print("Connected") 


loop = asyncio.get_event_loop()
loop.run_until_complete(run())

Command line:

python3 detect_sample.py

Output:

Traceback (most recent call last):
  File "detect_sample.py", line 35, in <module>
    loop.run_until_complete(run())
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "detect_sample.py", line 30, in run
    async with BleakClient(dev) as client:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/bleak/backends/corebluetooth/client.py", line 51, in __init__
    self._central_manager_delegate = address_or_ble_device.metadata["delegate"]
KeyError: 'delegate'
@dlech dlech added Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend bug Something isn't working labels Feb 17, 2021
dlech added a commit to dlech/bleak that referenced this issue May 12, 2021
This fixes a KeyError due to a missing 'delegate' key in the metadata
dictionary in the BLEDevice returned in the advertisement callback.

This also adds other legacy metadata entries to the dictionary.

Fixes: hbldh#448
dlech added a commit that referenced this issue May 13, 2021
Fix missing delegate in BLEDevice metadata

Fixes #448
Fixes #437
@nonameforgood
Copy link
Author

Awesome thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants