Skip to content

fix(hal): re-resolve a BLE device by name when its address goes stale - #141

Merged
gbradham merged 1 commit into
mainfrom
fix/ble-reresolve-by-name
Aug 21, 2026
Merged

fix(hal): re-resolve a BLE device by name when its address goes stale#141
gbradham merged 1 commit into
mainfrom
fix/ble-reresolve-by-name

Conversation

@gbradham

Copy link
Copy Markdown
Member

Straight from the bench. A Maimu scanned fine, was added at the address the scan reported, and then refused to initialize:

Failed to initialize device device_0: Device with address 20:79:13:0C:7D:30 was not found.

Which reads like the device is switched off. It wasn't — a phone app could see it the whole time.

Why

Many BLE peripherals advertise a resolvable private address that rotates every few minutes. The address the Scan button captured named nothing by the time Connect was pressed. Corroborating detail: the same unit had a different address the previous day (68:7E:E9:…20:79:13:…).

_resolve_address returned the configured address and never consulted the advertised name — not even after a failure. Since the Scan button always fills the address, the fragile path was the ordinary one, and the escape hatch (leave address blank, set name) was the path nobody would find.

The fix

A connect failure now rescans for the configured name and retries once at whatever address the peripheral is advertising now. A name doesn't rotate.

  • No name configured → nothing changes, original error raised.
  • Name resolves to the address that just failed → not a rotation, so the original error is reported rather than the same failure twice.

A better message for the most common BLE symptom

"Not found" is both the most frequent BLE failure and the least informative. When a name resolves to nothing, the error now names the two ordinary causes:

It may be connected to something else — a peripheral with a central attached usually stops advertising — or out of range.

That second clause explains something else from the same session: the scan stopped listing the device while a phone app still showed it, because the phone was connected to it.

Tests — 4 new

A stale address re-resolved by name (asserting both connect attempts and exactly one rescan); no name means the original failure, unchanged, with no scan; a name resolving to the same dead address is not retried; and a name that resolves to nothing produces the explanatory message.

Verification

PYTHONPATH=src QT_QPA_PLATFORM=offscreen pytest tests/3641 passed, 4 skipped, ruff and black clean.

From the bench: a Maimu scanned fine, was added at the address the scan
reported, and then refused to initialize with bleak's

    Device with address 20:79:13:0C:7D:30 was not found.

which reads like the device is switched off. It was not. Many BLE peripherals
advertise a resolvable private address that rotates every few minutes, so the
address the Scan button captured named nothing by the time anyone pressed
Connect. The same unit had a different address the day before.

_resolve_address returned the configured address and never consulted the
advertised name -- not even after a failure. Since the Scan button always fills
the address, the fragile path was the ordinary one, and the escape hatch (leave
the address blank, set the name) was the path nobody would find.

A connect failure now rescans for the configured name and retries once at
whatever address the peripheral is advertising now. A name does not rotate.
When the name resolves to the address that just failed this is not a rotation,
so the original error is reported rather than the same failure twice, and with
no name configured nothing changes.

The message for a name that resolves to nothing now names the two ordinary
causes, because "not found" is the most common BLE symptom and the least
informative: the peripheral is connected to something else -- one with a
central attached usually stops advertising, which is also why it vanishes from
scans -- or it is out of range.
@gbradham
gbradham merged commit 43260aa into main Aug 21, 2026
4 checks passed
@gbradham
gbradham deleted the fix/ble-reresolve-by-name branch August 21, 2026 15:26
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.

1 participant