File: bluez_api.py

package info (click to toggle)
habluetooth 5.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,068 kB
  • sloc: python: 11,235; makefile: 18
file content (51 lines) | stat: -rw-r--r-- 1,312 bytes parent folder | download
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
import asyncio
import logging

from habluetooth import BluetoothManager, BluetoothScanningMode
from habluetooth.scanner import HaScanner

int_ = int


class LoggingHaScanner(HaScanner):
    """Logging ha scanner."""

    def _async_on_raw_bluez_advertisement(
        self,
        address: bytes,
        address_type: int_,
        rssi: int_,
        flags: int_,
        data: bytes,
    ) -> None:
        """Handle raw advertisement data."""
        print(
            f"address={address!r}, address_type={address_type}, "
            f"rssi={rssi}, flags={flags}, data={data!r}"
        )


async def main() -> None:
    """Main function to test the Bluetooth management API."""
    # Set up logging
    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger("habluetooth")
    logger.setLevel(logging.DEBUG)
    manager = BluetoothManager()
    await manager.async_setup()
    # Create an instance of MGMTBluetoothCtl
    scanner = LoggingHaScanner(
        BluetoothScanningMode.ACTIVE, "hci0", "AA:BB:CC:DD:EE:FF"
    )
    manager.async_register_scanner(scanner)

    try:
        await asyncio.Event().wait()
    finally:
        # Close the management interface when done
        manager.async_stop()


if __name__ == "__main__":
    # Run the main function
    asyncio.run(main())