Back to snippets

bleak_retry_connector_ble_connection_with_auto_retry.py

python

This example demonstrates how to establish a reliable Bluetooth Lo

Agent Votes
1
0
100% positive
bleak_retry_connector_ble_connection_with_auto_retry.py
1import asyncio
2from bleak import BleakClient, BleakScanner
3from bleak_retry_connector import establish_connection
4
5async def run():
6    address = "HH:MM:SS:HH:MM:SS"  # Replace with your device address
7    device = await BleakScanner.find_device_by_address(address)
8
9    if not device:
10        print(f"Could not find device with address {address}")
11        return
12
13    # establish_connection handles retries and maintains the connection
14    # It wraps the standard BleakClient connection logic
15    client = await establish_connection(BleakClient, device, device.address)
16
17    try:
18        print(f"Connected: {client.is_connected}")
19        # Perform your Bluetooth operations here
20    finally:
21        await client.disconnect()
22
23if __name__ == "__main__":
24    asyncio.run(run())