forked from hbldh/bleak
-
Notifications
You must be signed in to change notification settings - Fork 0
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 (hbldh#1361)
Add `advertisement_data()` async iterator method to the `BleakScanner` which yields results of the ongoing scan.
- Loading branch information
1 parent
05793e1
commit 8a720ab
Showing
4 changed files
with
76 additions
and
9 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,37 @@ | ||
""" | ||
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 <[email protected]> | ||
""" | ||
import asyncio | ||
|
||
from bleak import BleakScanner | ||
|
||
|
||
async def main(): | ||
async with BleakScanner() as scanner: | ||
print("Scanning...") | ||
|
||
n = 5 | ||
print(f"\n{n} advertisement packets:") | ||
async for bd, ad in scanner.advertisement_data(): | ||
print(f" {n}. {bd!r} with {ad!r}") | ||
n -= 1 | ||
if n == 0: | ||
break | ||
|
||
n = 10 | ||
print(f"\nFind device with name longer than {n} characters...") | ||
async for bd, ad in scanner.advertisement_data(): | ||
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()) |