Skip to content

fix: pass BLEDevice to BleakClient for Bluetooth proxy support#153

Merged
broglep merged 3 commits intomeshtastic:mainfrom
dereckhall:fix/bluetooth-ble-device
Mar 18, 2026
Merged

fix: pass BLEDevice to BleakClient for Bluetooth proxy support#153
broglep merged 3 commits intomeshtastic:mainfrom
dereckhall:fix/bluetooth-ble-device

Conversation

@dereckhall
Copy link
Copy Markdown
Contributor

@dereckhall dereckhall commented Mar 2, 2026

Problem

When connecting to a Meshtastic device via Bluetooth, the integration passes a raw address string to BleakClient. This causes two issues:

  1. habluetooth cannot determine which proxy backend can route the connection, resulting in:

    BleakError: No backend with an available connection slot that can reach address XX:XX:XX:XX:XX:XX was found
    
  2. habluetooth logs a warning on every connection:

    BleakClient.connect() called without bleak-retry-connector. For reliable connection
    establishment, use bleak_retry_connector.establish_connection().
    

Fix

  • Look up the BLEDevice via async_ble_device_from_address() before creating the BluetoothConnection
  • Pass the BLEDevice through to BluetoothConnection so habluetooth can route through the correct proxy
  • Use establish_connection() from bleak-retry-connector when a BLEDevice is available, which handles connection slot management and automatic retries
  • Falls back to direct BleakClient.connect() with the raw address string when no BLEDevice is available

Testing

Tested with a Meshtastic device connected via Bluetooth and via an ESPHome Bluetooth proxy. Both the BleakError and the habluetooth warning no longer appear after this fix.

When connecting via an ESPHome Bluetooth proxy, habluetooth requires
a BLEDevice object (from HA's scanner cache) to route the connection
through the correct proxy backend. Passing a raw address string causes
BleakClient.connect() to fail with "No backend with an available
connection slot that can reach address" because habluetooth cannot
determine which proxy discovered the device.

Look up the BLEDevice via async_ble_device_from_address before creating
the BluetoothConnection, and pass it through to BleakClient. Falls back
to the address string when no BLEDevice is available.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 2, 2026

CLA assistant check
All committers have signed the CLA.

@broglep
Copy link
Copy Markdown
Collaborator

broglep commented Mar 2, 2026

LGTM, lint needs small fix, then it can be merged

@dereckhall dereckhall force-pushed the fix/bluetooth-ble-device branch from 990ef30 to d4a03ef Compare March 2, 2026 22:59
@dereckhall
Copy link
Copy Markdown
Contributor Author

Fixed the lint issue, ready to merge

Use establish_connection() from bleak-retry-connector instead of calling
BleakClient.connect() directly when a BLEDevice is available. This
integrates with HA's bluetooth connection slot management and provides
automatic retry logic for flaky BLE connections.
@dereckhall
Copy link
Copy Markdown
Contributor Author

I pushed an additional commit that adds bleak-retry-connector support. When a BLEDevice is available, _connect() now uses establish_connection() instead of calling BleakClient.connect() directly. This resolves the habluetooth warning about unreliable connection establishment. Updated the PR description to cover both changes.

@broglep broglep merged commit 41354b5 into meshtastic:main Mar 18, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants