Skip to content

fix: improve BLE reliability across macOS, Windows, and Linux#1

Merged
SeanathanVT merged 6 commits into
mainfrom
feature/macos-bluetooth-support
Apr 30, 2026
Merged

fix: improve BLE reliability across macOS, Windows, and Linux#1
SeanathanVT merged 6 commits into
mainfrom
feature/macos-bluetooth-support

Conversation

@SeanathanVT
Copy link
Copy Markdown
Owner

Summary

Addresses unreliable Bluetooth Low Energy device discovery and connection stability caused by Bleak's static BleakScanner methods on macOS CoreBluetooth, as well as metrics not updating correctly after pause/resume cycles. Fixes are cross-platform compatible.

Changes

Device Discovery & Connection

  • Replaced static BleakScanner methods with async with BleakScanner() context manager — more stable on macOS CoreBluetooth and consistent across all platforms
  • Added 3-retry mechanism with exponential backoff (up to 10s wait) for transient BLE connectivity issues; searches by cached MAC address and device name
  • Configurable device name via BLE_DEVICE_NAME constant at top of app.py — no scanner logic changes needed

Event Loop & Task Management

  • Proper event loop cleanup — cancels all pending tasks before closing the loop, preventing resource leaks and crashes
  • Stats monitor lifecycle fix — global _stats_monitor_task tracks the active monitor; old tasks are cancelled before new ones on resume, cleaned up on disconnect. Fixes metrics-not-updating-after-pause/resume bug
  • asyncio.wait_for() with 2s timeout on stats polling prevents hanging

Error Handling

  • Try-catch on all async sequences (start_belt(), resume_session()) — failures properly update app state and trigger disconnect handling instead of leaving the app in a hung state
  • Thread-safe coroutine execution — error handling on run_coroutine_threadsafe() calls ensures proper state management if queueing fails

API Compatibility

  • Bleak version fallback — supports both set_disconn_callback() (newer) and set_disconnected_callback() (older), ensuring compatibility across Bleak 0.19+

Compatibility

Platform Status
macOS 12+
Windows
Linux
Bleak 0.19+
Python 3.8+

Testing Recommendations

  1. Test device discovery with the WalkingPad nearby
  2. Test reconnection after device goes out of range
  3. Test pause/resume cycles — verify metrics continue updating correctly
  4. Test rapid start/pause/resume sequences — verify no duplicate stats tasks
  5. Test on multiple platforms (macOS, Windows, Linux) if available

@SeanathanVT SeanathanVT merged commit 3f77e4d into main Apr 30, 2026
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.

2 participants