-
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
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.
- Loading branch information
1 parent
2abe80d
commit 7e5b4a1
Showing
4 changed files
with
102 additions
and
13 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-27 by bojanpotocnik <info@bojanpotocnik.com> | ||
""" | ||
import asyncio | ||
|
||
from bleak import BleakScanner | ||
|
||
|
||
async def main(): | ||
x = 5 | ||
print(f"Scanning for {x} devices...") | ||
async with BleakScanner() as scanner: | ||
async for bd, ad in scanner: | ||
print(f"{x}. {bd!r} with {ad!r}") | ||
x -= 1 | ||
if x == 0: | ||
break | ||
|
||
x = 6 | ||
print(f"\nScanning for a device with name longer than {x} characters...") | ||
async with BleakScanner() as scanner: | ||
async for bd, ad in scanner.devices(timeout=10, return_adv=True): | ||
print(f"Found{' it' if len(bd.name) > x else ''} {bd!r} with {ad!r}") | ||
if len(bd.name or "") > x or len(ad.local_name or "") > x: | ||
break | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |