Registry / http-networking / bleak-retry-connector

bleak-retry-connector

JSON →
library4.6.3pypypi✓ verified 22d ago

bleak-retry-connector is a Python library that provides a robust wrapper around Bleak clients, adding automatic reconnection, service caching, and improved error handling for transient Bluetooth LE connection failures. It's built on top of the `bleak` library and is currently at version 4.6.0, with a fairly active release cadence, often updating dependencies and patching bugs.

pip install bleak-retry-connector
INSTALL
IMPORT
SIG · BLEAK-RETRY-CONNEC
B
bleak-retry-connector
http-networkingpythonv4.6.3
Install
2.7s avg
Import
354ms
Disk
21MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.6.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.364s · 23.6MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.7s · import 0.344s · 24MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

BleakClientWithServiceCache
from bleak_retry_connector import BleakClientWithServiceCache
This is the recommended class as it includes both retry logic and service caching, wrapping `BleakClientWithRetry`.
BleakClientWithRetry
from bleak_retry_connector import BleakClientWithRetry
Use this if you only need retry logic without service caching. `BleakClientWithServiceCache` internally uses this.

This quickstart demonstrates how to connect to a Bluetooth LE device using `BleakClientWithServiceCache`. It attempts to connect, and if successful, lists its services and tries to read from available characteristics. Remember to replace `DEVICE_ADDRESS` with an actual MAC address or UUID of a nearby BLE device. The retry mechanism is handled automatically by the client.

import asyncio from bleak_retry_connector import BleakClientWithServiceCache async def connect_and_read(device_address: str): try: # Ensure you replace 'DEVICE_MAC_ADDRESS_OR_UUID' with an actual device address # For example: 'XX:XX:XX:XX:XX:XX' (MAC) or 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' (UUID) # A real device with a known characteristic UUID is required to run this. async with BleakClientWithServiceCache(device_address) as client: if client.is_connected: print(f"Connected to {device_address}") # Example: List services for service in client.services: print(f" Service: {service.uuid}") for char in service.characteristics: print(f" Characteristic: {char.uuid}") if "read" in char.properties: try: value = await client.read_gatt_char(char.uuid) print(f" Value: {value.hex()}") except Exception as e: print(f" Could not read characteristic {char.uuid}: {e}") else: print(f"Failed to connect to {device_address} after retries.") except Exception as e: print(f"An error occurred: {e}") # Replace with a real Bluetooth LE device address # This example is illustrative and requires a discoverable device. DEVICE_ADDRESS = "XX:XX:XX:XX:XX:XX" # Placeholder, replace with actual MAC or UUID if __name__ == "__main__": print("Attempting to connect to a BLE device. This requires an actual device and may take time.") print("Please replace 'XX:XX:XX:XX:XX:XX' with your device's MAC address or UUID.") asyncio.run(connect_and_read(DEVICE_ADDRESS))
Debug
Known issues
gotchaThis library requires Python 3.10 or newer. Using older Python versions will result in installation or runtime errors.
fix
Ensure your Python environment is 3.10 or higher. Upgrade Python if necessary.
affects: <4.0.0
gotchaThe `device_address` parameter expects a valid Bluetooth MAC address (e.g., 'XX:XX:XX:XX:XX:XX') or a UUID. Providing an incorrect format or an address for a device that is not discoverable will lead to connection failures.
fix
Verify the correct MAC address or UUID of your target BLE device. Ensure the device is powered on, advertising, and within range.
affects: All
gotchaAs a wrapper around `bleak`, `bleak-retry-connector` inherits platform-specific quirks and requirements, such as D-Bus availability on Linux or specific macOS/Windows OS versions. Connection issues can often stem from underlying OS Bluetooth service problems.
fix
Consult the `bleak` documentation for platform-specific setup and troubleshooting. Ensure Bluetooth services are running correctly on your operating system.
affects: All
breakingStrict dependency ranges for `bleak` and `bluetooth-adapters` are enforced. If you have other packages that require different major versions of these dependencies, you might encounter version conflicts.
fix
Use a dedicated virtual environment for your project to isolate dependencies. Check the `pyproject.toml` or `setup.cfg` of `bleak-retry-connector` for exact dependency ranges and try to resolve conflicts manually or report to maintainers.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'bleak_retry_connector'
The 'bleak-retry-connector' package has not been installed in your Python environment or is not accessible.
fix
Install the library using pip: `pip install bleak-retry-connector`
BleakClient.connect() called without bleak-retry-connector
This is a warning, often seen in Home Assistant contexts, indicating that `BleakClient` is being used directly without the retry logic and improved error handling provided by `bleak-retry-connector`, leading to potentially less reliable connections.
fix
Replace direct `BleakClient` usage with `establish_connection` or `BleakClientWithServiceCache` from `bleak_retry_connector` for robust connection handling.

```python
from bleak import BleakScanner
from bleak_retry_connector import establish_connection, BleakClientWithServiceCache

async def connect_to_device(address: str):
    device = await BleakScanner.find_device_by_address(address)
    if device:
        client = await establish_connection(
            BleakClientWithServiceCache,
            device,
            name=device.name or "Unknown Device",
            max_attempts=5 # Example: retry 5 times
        )
        # Use the client
        await client.disconnect()
    else:
        print(f"Device {address} not found")
```
bleak_retry_connector.exceptions.BleakDeviceNotFoundError: Device was not found or disappeared after all retries.
The specified Bluetooth LE device could not be discovered or maintained connection with, even after `bleak-retry-connector`'s retry attempts.
fix
Verify the device's MAC address/UUID, ensure the device is powered on, advertising, and within range. Check for Bluetooth service issues on the host operating system.
bleak_retry_connector.exceptions.BleakOutOfConnectionSlotsError: Adapter/proxy has no available connection slots.
The local Bluetooth adapter or an ESP32 Bluetooth proxy has exhausted its available connection slots, preventing new connections. This is common with ESPHome Bluetooth proxies that have limited slots.
fix
Disconnect unused Bluetooth devices from the adapter/proxy, reduce the number of simultaneous connections, or add more ESP32 proxies if applicable. This error includes strategies like increasing backoff when slots are exhausted.
OSError: [WinError -2147483629] The object has been closed
This low-level Windows error often occurs when the underlying Bluetooth WinRT object is unexpectedly closed during a connection attempt or operation, indicating a transient communication issue with the Windows Bluetooth stack, or a race condition.
fix
While `bleak-retry-connector` is designed to handle transient connection failures, if this error persists, ensure your Python environment is up-to-date, especially `bleak` and `pywinrt` dependencies. For Windows console applications, ensure the threading model is Multi-Threaded Apartment (MTA) by setting `sys.coinit_flags = 0` before importing packages that might initialize COM to STA (like `pythoncom`).
Upgrade
Version history
4.6.3latest on PyPI · released Jul 22, 2026
Audit
Dependencies
bleakrequiredCore Bluetooth LE communication library that bleak-retry-connector wraps and enhances.
bluetooth-adaptersrequiredUsed for managing Bluetooth adapters and their states.
Agent activity
22 hits · last 30 days
node
20
OpenAI (training)
1
Resources
bleak-retry-connector — pip install bleak-retry-connector · libregistry