-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BleakScanner: Add async iterator scanning capability
Add `observe()` async iterator method to the `BleakScanner` which yields results of the ongoing scan.
- Loading branch information
1 parent
6d603aa
commit f1c8af1
Showing
4 changed files
with
143 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
""" | ||
Scan/Discovery Async Iterator | ||
-------------- | ||
Example showing how to scan for BLE devices using async iterator instead of callback function | ||
Created on 2023-07-07 by bojanpotocnik <info@bojanpotocnik.com> | ||
""" | ||
import asyncio | ||
import contextlib | ||
from typing import AsyncIterator, Tuple | ||
|
||
from bleak import BleakScanner, BLEDevice, AdvertisementData | ||
|
||
|
||
async def normal_usage(): | ||
n = 5 | ||
print(f"Scanning for {n} devices...") | ||
async with BleakScanner() as scanner: | ||
async for bd, ad in scanner.observe(): | ||
print(f"{n}. {bd!r} with {ad!r}") | ||
n -= 1 | ||
if n == 0: | ||
break | ||
|
||
n = 6 | ||
print(f"\nScanning for a device with name longer than {n} characters...") | ||
async with BleakScanner() as scanner: | ||
async for bd, ad in scanner.observe(): | ||
found = len(bd.name or "") > n or len(ad.local_name or "") > n | ||
print(f"Found{' it' if found else ''} {bd!r} with {ad!r}") | ||
if found: | ||
break | ||
|
||
|
||
async def deterministic_cleanup(): | ||
# If you want to use async iterator again immediately after it is done, | ||
# ensure that the previous one is closed and resources released. | ||
# Cleanup of asynchronous generators is a tricky case since they can | ||
# be cleaned up by multiple cases at once (unrolling as the generator | ||
# finishes, cancellation, or closing as the generator is garbage | ||
# collected), the order of which is implementation dependent. | ||
# The proper way to address this is not to rely on implicit cleanup, | ||
# but await aclose() method or use contextlib.aclosing(). | ||
|
||
# Helper method to avoid code duplication, could also be used in `normal_usage()`, | ||
# but that would make the `normal_usage()` example less clear. | ||
async def iterate_devices( | ||
iterator: AsyncIterator[Tuple[BLEDevice, AdvertisementData]], n: int | ||
): | ||
print(f"Scanning for {n} devices...") | ||
async for bd, ad in iterator: | ||
print(f"{n}. {bd!r} with {ad!r}") | ||
n -= 1 | ||
if n == 0: | ||
break | ||
|
||
async with BleakScanner() as scanner: | ||
print("\nUsing the same scanner async iterator immediately after it is done...") | ||
|
||
if hasattr(contextlib, "aclosing"): # New in version 3.10 | ||
print("Using contextlib.aclosing() to ensure cleanup...") | ||
|
||
# You can try removing any context managers below, e.g. change | ||
# async with contextlib.aclosing(scanner.observe()) as observer: | ||
# await iterate_devices(observer, 2) | ||
# to | ||
# await iterate_devices(scanner.observe(), 2) | ||
# to see how the cleanup is affected. Most likely `finally:` block in `observe()` | ||
# will not reset the callback in time and BleakError will be raised on next call. | ||
|
||
async with contextlib.aclosing(scanner.observe()) as observer: | ||
await iterate_devices(observer, 2) | ||
|
||
async with contextlib.aclosing(scanner.observe()) as observer: | ||
await iterate_devices(observer, 3) | ||
await iterate_devices(observer, 4) | ||
|
||
else: | ||
print("Using AsyncIterator.aclose() to ensure cleanup...") | ||
|
||
observer = scanner.observe() | ||
await iterate_devices(observer, 2) | ||
await observer.aclose() # Try commenting this | ||
|
||
observer = scanner.observe() | ||
await iterate_devices(observer, 3) | ||
await observer.aclose() # or this | ||
|
||
await iterate_devices(scanner.observe(), 6) | ||
# This one can be cleaned implicitly, as we do not care about reusing the same scanner anymore | ||
|
||
|
||
async def main(): | ||
await normal_usage() | ||
|
||
# Note about deterministic cleanup when using async | ||
await deterministic_cleanup() | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |