diff options
| author | Alex Schofield <git@ajschof.me> | 2025-02-07 21:38:41 +0000 |
|---|---|---|
| committer | Alex Schofield <git@ajschof.me> | 2025-02-07 21:38:41 +0000 |
| commit | bf2ae303bdd3b69be69eba4962f154baaed98cfd (patch) | |
| tree | e408ea0aa1c5541f2be4bfac4d23cf5dc1ae22fd | |
| parent | a77ced9f72eedcee6b1b0cdae655937dd9bcf1ae (diff) | |
| download | tp-logger-bf2ae303bdd3b69be69eba4962f154baaed98cfd.tar.gz tp-logger-bf2ae303bdd3b69be69eba4962f154baaed98cfd.zip | |
add functionality to connect to device and find characteristics and properties
| -rw-r--r-- | src/connect.py | 22 | ||||
| -rw-r--r-- | src/main.py | 5 |
2 files changed, 27 insertions, 0 deletions
diff --git a/src/connect.py b/src/connect.py new file mode 100644 index 0000000..dadf147 --- /dev/null +++ b/src/connect.py @@ -0,0 +1,22 @@ +from bleak import BleakClient, BleakScanner +from logger import get_logger + +logger = get_logger("Connection") + +async def connect(address): + try: + async with BleakClient(address) as client: + if client.is_connected: + logger.info("Connection successful!") + + services = await client.get_services() + for service in services: + logger.info(f"Service: {service.uuid}") + for char in service.characteristics: + logger.info(f" └── Characteristic: {char.uuid}, Properties: {char.properties}") + return client + else: + logger.error("Failed to connect!") + except Exception as e: + logger.error(f"Connection error: {e}") + raise
\ No newline at end of file diff --git a/src/main.py b/src/main.py index ecd075a..94c0559 100644 --- a/src/main.py +++ b/src/main.py @@ -1,4 +1,5 @@ from discover import discover +from connect import connect import os from logger import get_logger import asyncio @@ -42,6 +43,10 @@ async def main(debug_flag=None): else: device = devices[0] logger.info(f"Single device selected: {device['name']} - {device['address']}") + + conn = await connect(device['address']) + + return conn if __name__ == "__main__": asyncio.run(main()) |
