File: scan_iterator.py

package info (click to toggle)
bleak 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,828 kB
  • sloc: python: 10,660; makefile: 165; java: 105
file content (38 lines) | stat: -rw-r--r-- 974 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
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:
        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())