Starting fresh from the working pre-refactor code (d123d25), this is a clean
rewrite that preserves all the hard-won BLE reliability fixes while simplifying
the architecture. Key changes:
What's preserved (battle-tested, working):
- Exact same 16-step write sequence (DeviceName, Frame1, Frame2 iBeacon, Save)
- Same DX-Smart packet format (4E 4F CMD LEN DATA XOR)
- Response gating between writes (1s timeout, matches Android)
- Adaptive delays (1s for frame select/type, 0.8s for UUID, 0.5s base)
- FFE2 missing → full disconnect/reconnect (CoreBluetooth stale GATT cache)
- SaveConfig write-error = success (beacon reboots immediately)
- Disconnect recovery with write position resume
- Multi-password auth (555555, dx1234, 000000)
- Skip device info read (0x30 causes disconnects, MAC is optional)
- Skip extra frame disables (frames 3-6 untouched, fewer writes = fewer disconnects)
What's cleaned up:
- Removed dead device info provisioning code (was already skipped)
- Removed processDeviceInfoForProvisioning (dead code)
- Removed awaitingDeviceInfoForProvisioning flag
- Removed skipDeviceInfoRead flag
- Removed deviceInfoRetryCount (no longer needed)
- Consolidated charRediscovery into handleFFE2Missing()
- Renamed state vars for clarity (dxSmartWriteIndex → writeIndex, etc.)
- Extracted scheduleGlobalTimeout (was inline closure)
- Added cancelAllTimers() helper
- Reduced mutable state from ~30 vars to ~22
1652 lines → 1441 lines (-211 lines, -13%)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Two key changes:
1. Remove frames 3-6 disable commands (was steps 16-23, 8 extra BLE writes).
We only configure Frame 1 (device info) and Frame 2 (iBeacon), then save.
Fewer writes = fewer chances for supervision timeout disconnects.
2. When FFE2 characteristic goes missing after a disconnect, do a full
disconnect → reconnect → re-discover services → re-auth → resume cycle
instead of trying to rediscover characteristics on the same (stale GATT)
connection. CoreBluetooth returns cached results on the same connection,
so FFE2 stays missing. Full reconnect forces a fresh GATT discovery.
Write sequence is now 16 steps (down from 24).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When FFE2 goes missing during writes, the rediscovery path had no
timeout — if CoreBluetooth never called back didDiscoverCharacteristics,
the app would hang at "Re-discovering characteristics..." indefinitely.
Adds a 5-second timeout per rediscovery attempt. If it fires, it either
retries (up to MAX_CHAR_REDISCOVERY) or fails with .timeout instead of
hanging forever.
2026-03-22 03:18:43 +00:00
1 changed files with 1441 additions and 0 deletions