| 1 |
# Bleak Retry Connector |
| 2 |
|
| 3 |
<p align="center"> |
| 4 |
<a href="https://github.com/bluetooth-devices/bleak-retry-connector/actions?query=workflow%3ACI"> |
| 5 |
<img src="https://img.shields.io/github/actions/workflow/status/bluetooth-devices/bleak-retry-connector/ci.yml?branch=main&label=CI&logo=github&style=flat-square" alt="CI Status" > |
| 6 |
</a> |
| 7 |
<a href="https://bleak-retry-connector.readthedocs.io"> |
| 8 |
<img src="https://img.shields.io/readthedocs/bleak-retry-connector.svg?logo=read-the-docs&logoColor=fff&style=flat-square" alt="Documentation Status"> |
| 9 |
</a> |
| 10 |
<a href="https://codecov.io/gh/bluetooth-devices/bleak-retry-connector"> |
| 11 |
<img src="https://img.shields.io/codecov/c/github/bluetooth-devices/bleak-retry-connector.svg?logo=codecov&logoColor=fff&style=flat-square" alt="Test coverage percentage"> |
| 12 |
</a> |
| 13 |
</p> |
| 14 |
<p align="center"> |
| 15 |
<a href="https://python-poetry.org/"> |
| 16 |
<img src="https://img.shields.io/badge/packaging-poetry-299bd7?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAASCAYAAABrXO8xAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAJJSURBVHgBfZLPa1NBEMe/s7tNXoxW1KJQKaUHkXhQvHgW6UHQQ09CBS/6V3hKc/AP8CqCrUcpmop3Cx48eDB4yEECjVQrlZb80CRN8t6OM/teagVxYZi38+Yz853dJbzoMV3MM8cJUcLMSUKIE8AzQ2PieZzFxEJOHMOgMQQ+dUgSAckNXhapU/NMhDSWLs1B24A8sO1xrN4NECkcAC9ASkiIJc6k5TRiUDPhnyMMdhKc+Zx19l6SgyeW76BEONY9exVQMzKExGKwwPsCzza7KGSSWRWEQhyEaDXp6ZHEr416ygbiKYOd7TEWvvcQIeusHYMJGhTwF9y7sGnSwaWyFAiyoxzqW0PM/RjghPxF2pWReAowTEXnDh0xgcLs8l2YQmOrj3N7ByiqEoH0cARs4u78WgAVkoEDIDoOi3AkcLOHU60RIg5wC4ZuTC7FaHKQm8Hq1fQuSOBvX/sodmNJSB5geaF5CPIkUeecdMxieoRO5jz9bheL6/tXjrwCyX/UYBUcjCaWHljx1xiX6z9xEjkYAzbGVnB8pvLmyXm9ep+W8CmsSHQQY77Zx1zboxAV0w7ybMhQmfqdmmw3nEp1I0Z+FGO6M8LZdoyZnuzzBdjISicKRnpxzI9fPb+0oYXsNdyi+d3h9bm9MWYHFtPeIZfLwzmFDKy1ai3p+PDls1Llz4yyFpferxjnyjJDSEy9CaCx5m2cJPerq6Xm34eTrZt3PqxYO1XOwDYZrFlH1fWnpU38Y9HRze3lj0vOujZcXKuuXm3jP+s3KbZVra7y2EAAAAAASUVORK5CYII=" alt="Poetry"> |
| 17 |
</a> |
| 18 |
<a href="https://github.com/astral-sh/ruff"> |
| 19 |
<img src="https://img.shields.io/badge/code%20style-ruff-000000.svg?style=flat-square" alt="ruff"> |
| 20 |
</a> |
| 21 |
<a href="https://github.com/pre-commit/pre-commit"> |
| 22 |
<img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=flat-square" alt="pre-commit"> |
| 23 |
</a> |
| 24 |
</p> |
| 25 |
<p align="center"> |
| 26 |
<a href="https://pypi.org/project/bleak-retry-connector/"> |
| 27 |
<img src="https://img.shields.io/pypi/v/bleak-retry-connector.svg?logo=python&logoColor=fff&style=flat-square" alt="PyPI Version"> |
| 28 |
</a> |
| 29 |
<img src="https://img.shields.io/pypi/pyversions/bleak-retry-connector.svg?style=flat-square&logo=python&logoColor=fff" alt="Supported Python versions"> |
| 30 |
<img src="https://img.shields.io/pypi/l/bleak-retry-connector.svg?style=flat-square" alt="License"> |
| 31 |
</p> |
| 32 |
|
| 33 |
A connector for Bleak Clients that handles transient connection failures |
| 34 |
|
| 35 |
## Installation |
| 36 |
|
| 37 |
Install this via pip (or your favourite package manager): |
| 38 |
|
| 39 |
`pip install bleak-retry-connector` |
| 40 |
|
| 41 |
## Usage |
| 42 |
|
| 43 |
### Quick Start |
| 44 |
|
| 45 |
Replace your direct `BleakClient.connect()` calls with `establish_connection()` for automatic retry logic and better error handling: |
| 46 |
|
| 47 |
```python |
| 48 |
import asyncio |
| 49 |
from bleak import BleakScanner |
| 50 |
from bleak_retry_connector import establish_connection, BleakClientWithServiceCache |
| 51 |
|
| 52 |
|
| 53 |
async def connect_to_device(): |
| 54 |
# Find your device |
| 55 |
device = await BleakScanner.find_device_by_address("AA:BB:CC:DD:EE:FF") |
| 56 |
|
| 57 |
if device is None: |
| 58 |
print("Device not found!") |
| 59 |
return |
| 60 |
|
| 61 |
# Establish connection with automatic retry logic |
| 62 |
client = await establish_connection( |
| 63 |
BleakClientWithServiceCache, # Use BleakClientWithServiceCache for service caching |
| 64 |
device, |
| 65 |
device.name or "Unknown Device", |
| 66 |
max_attempts=3, # Will retry up to 3 times with backoff |
| 67 |
) |
| 68 |
|
| 69 |
try: |
| 70 |
# Use the connected client normally |
| 71 |
services = client.services |
| 72 |
print(f"Connected! Found {len(list(services))} services") |
| 73 |
|
| 74 |
# Read a characteristic |
| 75 |
value = await client.read_gatt_char("00002a00-0000-1000-8000-00805f9b34fb") |
| 76 |
print(f"Read value: {value}") |
| 77 |
|
| 78 |
finally: |
| 79 |
await client.disconnect() |
| 80 |
|
| 81 |
|
| 82 |
# Run the example |
| 83 |
asyncio.run(connect_to_device()) |
| 84 |
``` |
| 85 |
|
| 86 |
### Why Use bleak-retry-connector? |
| 87 |
|
| 88 |
- **Automatic Retry Logic**: Handles transient connection failures automatically |
| 89 |
- **Intelligent Backoff**: Uses appropriate delays between retry attempts |
| 90 |
- **Service Caching**: `BleakClientWithServiceCache` caches services for faster reconnections |
| 91 |
- **Better Error Messages**: Provides clear, actionable error messages |
| 92 |
- **Platform-Specific Handling**: Manages quirks across different operating systems |
| 93 |
- **Connection Slot Management**: Handles limited connection slots on some devices |
| 94 |
|
| 95 |
### Common Connection Issues This Solves |
| 96 |
|
| 97 |
- Device not found errors that resolve on retry |
| 98 |
- Connection timeouts on first attempt |
| 99 |
- "Out of connection slots" errors on ESP32 devices |
| 100 |
- Interference from other Bluetooth operations |
| 101 |
- Platform-specific connection quirks |
| 102 |
|
| 103 |
For detailed documentation and advanced usage, see the [full documentation](https://bleak-retry-connector.readthedocs.io/en/latest/usage.html). |
| 104 |
|
| 105 |
## Contributors ✨ |
| 106 |
|
| 107 |
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)): |
| 108 |
|
| 109 |
<!-- prettier-ignore-start --> |
| 110 |
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> |
| 111 |
<!-- markdownlint-disable --> |
| 112 |
<!-- markdownlint-enable --> |
| 113 |
<!-- ALL-CONTRIBUTORS-LIST:END --> |
| 114 |
<!-- prettier-ignore-end --> |
| 115 |
|
| 116 |
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! |
| 117 |
|
| 118 |
## Credits |
| 119 |
|
| 120 |
This package was created with |
| 121 |
[Cookiecutter](https://github.com/audreyr/cookiecutter) and the |
| 122 |
[browniebroke/cookiecutter-pypackage](https://github.com/browniebroke/cookiecutter-pypackage) |
| 123 |
project template. |