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: Add async iterator scanning capability #1361

Merged
merged 3 commits into from
Jul 23, 2023

Conversation

bojanpotocnik
Copy link
Contributor

@bojanpotocnik bojanpotocnik commented Jul 7, 2023

Enable using BleakScanner() as an async iterator (async for), combining detection_callback and filtering functionality. BleakScanner().devices() method has the same functionality with option to provide timeout.

I noticed that almost every time I use bleak, one of the first things is to create scanner method which combines discover() and find_device_by_filter() functionality "in concurrent fashion", without the need of using callbacks and queue. This enables reporting scanned devices immediately while giving all the filtering flexibility, even when scanning for multiple devices with different filters.

bleak/__init__.py Outdated Show resolved Hide resolved
@bojanpotocnik bojanpotocnik force-pushed the scanner_async_iterator branch 2 times, most recently from 7e5b4a1 to 4148a1d Compare July 7, 2023 23:20
bleak/__init__.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@dlech dlech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. This is something that I have been wanting to add for a while.

I have made some suggestions on how we could simplify the implementation and usage a bit.

CHANGELOG.rst Outdated Show resolved Hide resolved
bleak/__init__.py Outdated Show resolved Hide resolved
bleak/__init__.py Outdated Show resolved Hide resolved
bleak/__init__.py Outdated Show resolved Hide resolved
bleak/__init__.py Outdated Show resolved Hide resolved
bleak/backends/scanner.py Outdated Show resolved Hide resolved
bleak/__init__.py Outdated Show resolved Hide resolved
bleak/__init__.py Outdated Show resolved Hide resolved
@bojanpotocnik bojanpotocnik force-pushed the scanner_async_iterator branch 2 times, most recently from d3fc836 to 4646aea Compare July 10, 2023 20:11
bleak/__init__.py Outdated Show resolved Hide resolved
@bojanpotocnik bojanpotocnik requested a review from dlech July 11, 2023 06:05
bleak/__init__.py Outdated Show resolved Hide resolved
examples/scan_iterator.py Outdated Show resolved Hide resolved
bleak/__init__.py Outdated Show resolved Hide resolved
bleak/__init__.py Outdated Show resolved Hide resolved
bleak/backends/scanner.py Outdated Show resolved Hide resolved
@dlech
Copy link
Collaborator

dlech commented Jul 13, 2023

We also need to expand the "Getting discovered devices and advertisement data" section in scanner.rst so that the new method is included in the documentation.

@dlech dlech marked this pull request as draft July 18, 2023 17:05
@dlech dlech marked this pull request as ready for review July 18, 2023 17:05
bleak/__init__.py Outdated Show resolved Hide resolved
@bojanpotocnik
Copy link
Contributor Author

The last two comments are here so you can see the alternative implementations - I can add any of them back if desired.

bleak/__init__.py Show resolved Hide resolved
bleak/__init__.py Show resolved Hide resolved
examples/scan_iterator.py Outdated Show resolved Hide resolved
docs/api/scanner.rst Show resolved Hide resolved
bleak/__init__.py Outdated Show resolved Hide resolved
bleak/__init__.py Outdated Show resolved Hide resolved
@bojanpotocnik bojanpotocnik force-pushed the scanner_async_iterator branch 2 times, most recently from 2cb1bb0 to b8d7a38 Compare July 21, 2023 14:57
Add `advertisement_data()` async iterator method to the `BleakScanner`
which yields results of the ongoing scan.
bleak/__init__.py Outdated Show resolved Hide resolved
CHANGELOG.rst Outdated Show resolved Hide resolved
bleak/__init__.py Outdated Show resolved Hide resolved
@dlech dlech merged commit 8a720ab into hbldh:develop Jul 23, 2023
16 checks passed
@dlech
Copy link
Collaborator

dlech commented Jul 23, 2023

Thanks for all of the work on this! It was more complicated than I expected.

@bojanpotocnik bojanpotocnik deleted the scanner_async_iterator branch July 24, 2023 05:52
@dlech dlech mentioned this pull request Sep 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants