-
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 `devices()` async iterator method to the `BleakScanner` which yields results of the ongoing scan, together with its class-method counterpart `scan()`. Note about cleaning the `devices` queue in BleakScanner.devices(): It is safe to use `devices.get()` instead of `.get_nowait()` and catching the QueueEmpty, as yield was the only consumer of the queue. This means that a race condition between `.empty()` returning `False` but queue being empty when `.get()` is called, consequently hanging the program, is not possible.
- Loading branch information
1 parent
2abe80d
commit caa2484
Showing
4 changed files
with
98 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,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 <info@bojanpotocnik.com> | ||
""" | ||
import asyncio | ||
|
||
from bleak import BleakScanner | ||
|
||
|
||
async def main(): | ||
# The two methods below (using class method .scan() or instance method .devices()) are equivalent | ||
|
||
x = 5 | ||
print(f"Scanning for {x} devices...") | ||
async with BleakScanner() as scanner: | ||
async for bd, ad in scanner.devices(): | ||
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 for bd, ad in BleakScanner.scan(): | ||
found = len(bd.name or "") > x or len(ad.local_name or "") > x | ||
print(f"Found{' it' if found else ''} {bd!r} with {ad!r}") | ||
if found: | ||
break | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |