-
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 32b30e8
Showing
4 changed files
with
81 additions
and
12 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,35 @@ | ||
""" | ||
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 | ||
|
||
from bleak import BleakScanner | ||
|
||
|
||
async def main(): | ||
async with BleakScanner() as scanner: | ||
n = 5 | ||
print(f"Scanning for {n} devices...") | ||
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 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 | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |