Debian Codegraph @

examples/bluez_api.py

raw
1 import asyncio
2 import logging
3
4 from habluetooth import BluetoothManager, BluetoothScanningMode, HaScanner
5
6 int_ = int
7
8
9 class LoggingHaScanner(HaScanner):
10 """Logging ha scanner."""
11
12 def _async_on_raw_bluez_advertisement(
13 self,
14 address: bytes,
15 address_type: int_,
16 rssi: int_,
17 flags: int_,
18 data: bytes,
19 ) -> None:
20 """Handle raw advertisement data."""
21 print(
22 f"address={address!r}, address_type={address_type}, "
23 f"rssi={rssi}, flags={flags}, data={data!r}"
24 )
25
26
27 async def main() -> None:
28 """Main function to test the Bluetooth management API."""
29 # Set up logging
30 logging.basicConfig(level=logging.DEBUG)
31 logger = logging.getLogger("habluetooth")
32 logger.setLevel(logging.DEBUG)
33 manager = BluetoothManager()
34 await manager.async_setup()
35 # Create an instance of MGMTBluetoothCtl
36 scanner = LoggingHaScanner(
37 BluetoothScanningMode.ACTIVE, "hci0", "AA:BB:CC:DD:EE:FF"
38 )
39 manager.async_register_scanner(scanner)
40
41 try:
42 await asyncio.Event().wait()
43 finally:
44 # Close the management interface when done
45 manager.async_stop()
46
47
48 if __name__ == "__main__":
49 # Run the main function
50 asyncio.run(main())