Skip to content

Fix mobile BLE scan stream completion and cleanup#8

Open
robekl wants to merge 1 commit intoMeshMapper:devfrom
robekl:fix/mobile-scan-stream-completion
Open

Fix mobile BLE scan stream completion and cleanup#8
robekl wants to merge 1 commit intoMeshMapper:devfrom
robekl:fix/mobile-scan-stream-completion

Conversation

@robekl
Copy link

@robekl robekl commented Mar 4, 2026

Summary

Fixes mobile BLE scan stream lifecycle so scan flows complete reliably and do not hang waiting for stream termination.

Problem

MobileBluetoothService.scanForDevices() yielded a controller stream that was never guaranteed to close.
This could leave callers waiting on onDone indefinitely and keep UI scan state stuck.

Changes

  • Added _scanController to track the active scan stream controller.
  • In scanForDevices():
    • Cancel any previous scan subscription before starting a new one.
    • Guard controller.add(...) with !controller.isClosed.
    • Start a background watcher on FlutterBluePlus.isScanning and close the controller when scanning stops.
    • Ensure controller cleanup in finally.
  • In stopScan():
    • Close _scanController if still open.
    • Clear _scanController reference after cleanup.

Why this works

The scan stream now has explicit termination paths for:

  • natural scan stop/timeout (isScanning == false)
  • manual stop (stopScan())
  • error/teardown (finally)

So the consumer can consistently receive stream completion and unblock scan UI logic.

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