Skip to content

fix(ble): resume Limitless recording after network-only reconnect - #7842

Merged
kodjima33 merged 2 commits into
BasedHardware:mainfrom
formed2forge:fix/limitless-network-reconnect
Jun 16, 2026
Merged

kodjima33 merged 2 commits into
BasedHardware:mainfrom
formed2forge:fix/limitless-network-reconnect

Conversation

@formed2forge

Copy link
Copy Markdown
Contributor

Problem

When the Limitless pendant is recording and the network drops entirely (Bluetooth stays connected), the pendant silently stops streaming BLE audio after its internal timeout elapses. When the network comes back, the app's keep-alive timer successfully reconnects the transcription WebSocket, but no audio flows — the pendant is no longer emitting frames. The UI continues to show deviceRecord, giving no indication that transcription has stopped. The only recovery is force-closing the app or disconnecting/reconnecting Bluetooth.

This is the network-side analog of #7725 (BT reconnect fix). That fix added _handleTransportReconnected() to re-send the enable-data-stream command after a BLE drop. This PR does the same for network-only drops.

Fixes #7841

Root Cause

_initiateWebsocket() in CaptureProvider has no way to notify the active device connection that the socket has recovered. After reconnecting the socket, the _bleBytesStream pipeline is live and waiting, but the Limitless pendant stopped producing BLE audio frames while the socket was dead. Nobody re-sent the enable-data-stream command.

CaptureProvider.onConnected() is the natural hook, but it is unreachable from the keep-alive reconnect path: subscribe() is called after start() completes (socket already connected), so the onConnected event fires on an empty listeners map and never reaches CaptureProvider. Even if it did, it only handles the interrupted state (phone mic).

Solution

Add onNetworkSocketReconnected() as a virtual hook on DeviceConnection (no-op default). LimitlessDeviceConnection overrides it to call _reinitializeAfterReconnect() — the same enable-data-stream command used by the BT reconnect fix.

A _socketReconnectPending boolean in CaptureProvider gates the call:

  • Set in onClosed() when recordingState == deviceRecord (socket dropped during active device recording)
  • Consumed and cleared in _initiateWebsocket() after successful reconnect, guarded by recordingState == deviceRecord (skipped if the user paused between the drop and the reconnect)
  • Cleared in _cleanupCurrentState() on explicit stop so it does not carry over into the next session

Explicit socket stops call TranscriptSegmentSocketService.stop() which clears listeners before the async close fires, so CaptureProvider.onClosed() is never reached from those paths — the flag is never set spuriously.

Changes

  • device_connection.dart — onNetworkSocketReconnected() no-op hook on the base class; all existing device types unaffected
  • limitless_connection.dart — override calls _reinitializeAfterReconnect() (re-sends enable-data-stream)
  • capture_provider.dart — _socketReconnectPending flag wiring in onClosed(), _cleanupCurrentState(), and _initiateWebsocket()

Note: the diff also includes formatter-only normalizations in capture_provider.dart and device_connection.dart produced by the pre-commit hook (dart format --line-length 120). No logic is changed in those lines.

Testing

Verified on hardware (Limitless pendant on iOS):

  1. Start recording — confirm transcription is live.
  2. Toggle airplane mode on (Bluetooth stays connected on iOS).
  3. Wait 60+ seconds for the pendant's internal streaming timeout.
  4. Toggle airplane mode off.
  5. Transcription resumes within ~15 seconds (keep-alive interval) without touching the pendant or restarting the app. ✅

Regression verified:

  • BT disconnect/reconnect still works (_handleTransportReconnected() path unchanged) ✅
  • Explicit stop/start cycle works correctly (flag cleared by _cleanupCurrentState()) ✅
  • Pause/resume cycle unaffected (deviceRecord guard prevents reinit while paused) ✅

When the Limitless pendant is recording and the server WebSocket drops
(network gone, BLE stays up), the device stops emitting BLE audio after
its internal timeout.  The app's keep-alive timer reconnects the socket
but never re-sends the enable-data-stream command, so no audio flows to
the new socket.  The UI stays on deviceRecord giving no hint, and the
only recovery is force-closing the app or disconnecting BT.

This is the network-side counterpart of the BT reconnect fix (BasedHardware#7725).
That fix added _handleTransportReconnected() which re-sends the command
after a BLE drop.  We apply the same treatment for network-only drops.

Add onNetworkSocketReconnected() as a no-op virtual hook on DeviceConnection.
LimitlessDeviceConnection overrides it to call _reinitializeAfterReconnect()
(re-sends enable-data-stream, same as the BT path).

In CaptureProvider, _socketReconnectPending is set in onClosed() when
the socket drops during deviceRecord state, and consumed in
_initiateWebsocket() after a successful reconnect — guarded by
recordingState == deviceRecord so it is skipped when the user has paused
(no point waking the device when _bleBytesStream is cancelled).
_cleanupCurrentState() clears the flag on explicit stop so it does not
carry over into the next session.

Co-authored-by: Claude Sonnet 4.6 <[email protected]>
@greptile-apps

greptile-apps Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a network-only Limitless pendant recording outage by adding a _socketReconnectPending boolean in CaptureProvider that gates a new onNetworkSocketReconnected() virtual hook on DeviceConnection. The hook lets LimitlessDeviceConnection re-send the enable-data-stream BLE command after the transcription WebSocket recovers from a network drop, mirroring the existing BT-reconnect path added in #7725.

  • capture_provider.dart: _socketReconnectPending is set in onClosed() when recordingState == deviceRecord, consumed and cleared in _initiateWebsocket() after a successful socket reconnect (guarded by the same state), and wiped in _cleanupCurrentState() on explicit stop; the rest of the diff is formatter-only whitespace normalization.
  • device_connection.dart: Adds a no-op onNetworkSocketReconnected() async method to the base class; all existing device types are unaffected.
  • limitless_connection.dart: Overrides onNetworkSocketReconnected() to call _reinitializeAfterReconnect(), which writes the enable-data-stream characteristic — the same command the BT-reconnect path uses.

Confidence Score: 4/5

Safe to merge for the primary fix. The two observations (concurrent reinit bypass and no-retry after failed BLE write) represent edge-case hardening gaps rather than broken behavior in the intended network-only outage scenario.

The core flag wiring and state-guard logic in CaptureProvider is sound — set on socket close during device recording, cleared on cleanup, consumed only when both the flag and the deviceRecord state agree. The LimitlessDeviceConnection override is simple and delegates to the well-tested _reinitializeAfterReconnect(). The two observations are narrow edge cases (simultaneous BT+network recovery, and a BLE write failure during reinit) that don't affect the main fix path and match the existing BT-reconnect behavior.

app/lib/services/devices/limitless_connection.dart — the new onNetworkSocketReconnected() override bypasses the _isReinitializing serialization guard used by the BT-reconnect path.

Important Files Changed

Filename Overview
app/lib/providers/capture_provider.dart Adds _socketReconnectPending flag with correct set/clear/guard logic; the ensureConnection call in _initiateWebsocket returns null-safely if BLE isn't connected, silently skipping reinit with no retry. Most changes are formatter whitespace only.
app/lib/services/devices/device_connection.dart Adds a no-op onNetworkSocketReconnected() hook to the abstract base class; existing subclasses are unaffected. Remaining changes are formatter whitespace normalization.
app/lib/services/devices/limitless_connection.dart Overrides onNetworkSocketReconnected() to call _reinitializeAfterReconnect(); the override doesn't consult the _isReinitializing / _pendingReinit serialization guard that _handleTransportReconnected() uses, opening a narrow concurrent-write window when BT and network both recover simultaneously.

Sequence Diagram

sequenceDiagram
    participant App as CaptureProvider
    participant KA as KeepAlive Timer
    participant Sock as TranscriptSocket
    participant Dev as DeviceService
    participant LC as LimitlessDeviceConnection

    Note over App,LC: Normal recording (recordingState = deviceRecord)

    Sock-->>App: onClosed() [network drop]
    App->>App: "_socketReconnectPending = true"

    loop Every 15s
        KA->>App: keep-alive tick
        App->>Sock: _initiateWebsocket()
        Sock-->>App: socket connected
        App->>App: "check _socketReconnectPending && deviceRecord"
        App->>App: "_socketReconnectPending = false"
        App->>Dev: ensureConnection(_recordingDevice.id)
        Dev-->>App: conn (BLE still connected)
        App->>LC: conn.onNetworkSocketReconnected()
        LC->>LC: _reinitializeAfterReconnect()
        LC->>LC: writeCharacteristic(enable-data-stream)
        Note over LC: Pendant resumes BLE audio stream
    end

    Note over App,LC: Explicit stop
    App->>App: _cleanupCurrentState()
    App->>App: "_socketReconnectPending = false"
Loading

Reviews (1): Last reviewed commit: "fix(ble): resume Limitless recording aft..." | Re-trigger Greptile

Comment on lines +109 to +115
@override
Future<void> onNetworkSocketReconnected() async {
// The Limitless pendant stops streaming BLE audio after an extended period
// without a live server connection. Re-send the enable-data-stream command
// so it resumes, just as _reinitializeAfterReconnect() does after BT reconnect.
await _reinitializeAfterReconnect();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Missing _isReinitializing guard — duplicate concurrent writes possible

_handleTransportReconnected() serializes BT-reconnect reinit calls via _isReinitializing / _pendingReinit to prevent two concurrent writeCharacteristic calls from racing. onNetworkSocketReconnected() calls _reinitializeAfterReconnect() directly, bypassing that guard entirely.

If a BT transport reconnect fires while onNetworkSocketReconnected() is already awaiting the BLE write (e.g., network recovers just before BLE finishes cycling), both paths call _reinitializeAfterReconnect() simultaneously. The pendant receives two back-to-back enable-data-stream commands; whether this causes a stutter or is silently idempotent depends on firmware behavior. Routing through the same serialization used by _handleTransportReconnected() would prevent this ambiguity.

Comment on lines +572 to +576
if (_socketReconnectPending && _recordingDevice != null && recordingState == RecordingState.deviceRecord) {
_socketReconnectPending = false;
final conn = await ServiceManager.instance().device.ensureConnection(_recordingDevice!.id);
await conn?.onNetworkSocketReconnected();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 _socketReconnectPending cleared before reinit completes — silent no-retry on BLE write failure

_socketReconnectPending is set to false before ensureConnection and onNetworkSocketReconnected() are awaited. If the BLE write inside _reinitializeAfterReconnect() throws (transport error, pendant busy), the error is swallowed and the flag is already gone, so no further keep-alive cycle will retry. The pendant stays silent without any indication. The BT-reconnect path has the same no-retry characteristic, so this is consistent, but it's worth confirming the pendant's error-recovery on failed enable-data-stream writes is robust enough.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good BLE reconnect fix with clear root cause; lint check failing — fix to land

@formed2forge

Copy link
Copy Markdown
Contributor Author

Good BLE reconnect fix with clear root cause; lint check failing — fix to land

Formatting fix pushed. Good to go.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mobile BLE bug fix: resume Limitless recording after network-only reconnect; clear root cause, mirrors existing reconnect path.

@kodjima33
kodjima33 merged commit 672ee6b into BasedHardware:main Jun 16, 2026
1 check passed
@cursor
cursor Bot deleted the fix/limitless-network-reconnect branch September 2, 2026 02:34
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.

Limitless pendant does not resume recording after network-only reconnect

2 participants