-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathdetection_callback.py
More file actions
76 lines (55 loc) · 1.73 KB
/
Copy pathdetection_callback.py
File metadata and controls
76 lines (55 loc) · 1.73 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Detection callback w/ scanner
--------------
Example showing what is returned using the callback upon detection functionality
Updated on 2020-10-11 by bernstern <bernie@allthenticate.net>
"""
import argparse
import asyncio
import logging
from bleak import BleakScanner
from bleak.backends.device import BLEDevice
from bleak.backends.scanner import AdvertisementData
logger = logging.getLogger(__name__)
class Args(argparse.Namespace):
macos_use_bdaddr: bool
services: list[str]
debug: bool
def simple_callback(device: BLEDevice, advertisement_data: AdvertisementData):
logger.info(
"addr: %s, name: %s, %r", device.address, device.name, advertisement_data
)
async def main(args: Args):
scanner = BleakScanner(
simple_callback, args.services, cb={"use_bdaddr": args.macos_use_bdaddr}
)
while True:
logger.info("(re)starting scanner")
async with scanner:
await asyncio.sleep(5.0)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--macos-use-bdaddr",
action="store_true",
help="when true use Bluetooth address instead of UUID on macOS",
)
parser.add_argument(
"--services",
metavar="<uuid>",
nargs="*",
help="UUIDs of one or more services to filter for",
)
parser.add_argument(
"-d",
"--debug",
action="store_true",
help="sets the logging level to debug",
)
args = parser.parse_args(namespace=Args())
log_level = logging.DEBUG if args.debug else logging.INFO
logging.basicConfig(
level=log_level,
format="%(asctime)-15s %(name)-8s %(levelname)s: %(message)s",
)
asyncio.run(main(args))