fix: Resume BLE writes on disconnect instead of restarting #12

Closed
schwifty wants to merge 0 commits from schwifty/fix-disconnect-resume into main
Collaborator

Problem

Beacon disconnects 4 times during provisioning — it connects, writes some commands, disconnects, then reconnects and starts all 24 commands over from scratch. This wastes time, confuses the beacon firmware with duplicate writes, and burns through retries fast.

Root Cause

The disconnect handler was calling dxSmartCommandQueue.removeAll() and dxSmartWriteIndex = 0 — nuking the saved progress on every reconnect.

Fix

  • Resume from saved position: On disconnect during writing, preserve the command queue and write index. After reconnect + re-auth, skip straight to dxSmartSendNextCommand() at the saved index.
  • More retries: Increase MAX_DISCONNECT_RETRIES from 3→5 (resume is cheap)
  • Slower writes: Increase inter-command delay from 150ms→300ms (gives firmware time to process flash writes)
  • Longer timeout: 45s→90s to accommodate the extra retries

Test Plan

  • Provision a DX-Smart beacon — should succeed even with 1-2 disconnects
  • Check debug logs for "Resuming write from command X/24" messages
  • Verify SaveConfig still triggers success on disconnect/error
## Problem Beacon disconnects 4 times during provisioning — it connects, writes some commands, disconnects, then reconnects and **starts all 24 commands over from scratch**. This wastes time, confuses the beacon firmware with duplicate writes, and burns through retries fast. ## Root Cause The disconnect handler was calling `dxSmartCommandQueue.removeAll()` and `dxSmartWriteIndex = 0` — nuking the saved progress on every reconnect. ## Fix - **Resume from saved position**: On disconnect during writing, preserve the command queue and write index. After reconnect + re-auth, skip straight to `dxSmartSendNextCommand()` at the saved index. - **More retries**: Increase MAX_DISCONNECT_RETRIES from 3→5 (resume is cheap) - **Slower writes**: Increase inter-command delay from 150ms→300ms (gives firmware time to process flash writes) - **Longer timeout**: 45s→90s to accommodate the extra retries ## Test Plan - [ ] Provision a DX-Smart beacon — should succeed even with 1-2 disconnects - [ ] Check debug logs for "Resuming write from command X/24" messages - [ ] Verify SaveConfig still triggers success on disconnect/error
schwifty added 1 commit 2026-03-22 02:48:58 +00:00
Previously, when the beacon disconnected mid-write, the reconnect handler
cleared the entire command queue and reset writeIndex to 0, causing all 24
commands to be re-sent from scratch on every reconnect. This could confuse
the beacon firmware with duplicate config writes and wasted reconnect cycles.

Changes:
- On disconnect during writing, PRESERVE command queue and write index
- After reconnect + re-auth, resume from the last command instead of rebuilding
- Increase MAX_DISCONNECT_RETRIES from 3 to 5 (resume is lightweight)
- Increase inter-command delay from 150ms to 300ms for firmware breathing room
- Increase global timeout from 45s to 90s to accommodate more retries
- Add resumeWriteAfterDisconnect flag to control post-auth flow

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
schwifty closed this pull request 2026-03-22 03:13:08 +00:00

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: payfrit/payfrit-beacon-ios#12
No description provided.