radiacode icon indicating copy to clipboard operation
radiacode copied to clipboard

Improve Bluetooth handling: permission errors and automatic reconnection

Open GlassOnTin opened this issue 8 months ago • 1 comments

Summary

  • Enhanced error messages when Bluetooth scanning fails due to missing permissions
  • Added automatic reconnection with exponential backoff when Bluetooth connections drop
  • Implemented connection state tracking for better visibility

Features

1. Enhanced Permission Error Handling

  • Detect when user is not in the bluetooth group and provide specific instructions
  • Show exact commands for both permanent fix and temporary sudo workaround
  • Check permissions proactively before attempting to scan

2. Automatic Reconnection Logic

  • Added ConnectionState enum to track connection states (disconnected/connecting/connected/reconnecting)
  • Implemented exponential backoff for reconnection attempts (1s → 2s → 4s → ... → 60s max)
  • Added comprehensive logging using Python's logging module
  • Enhanced error handling to detect specific Bluetooth disconnection errors
  • Device continues attempting reconnection until manually stopped

Benefits

This improves reliability when:

  • Setting up RadiaCode monitoring for the first time (permission issues)
  • Bluetooth connections drop due to:
    • Device timeouts after prolonged streaming (~1-2 minutes)
    • Bluetooth interference
    • Device power cycling
    • Other processes connecting to the device

Changes

  • device_reader.py: Added permission checks and automatic reconnection logic
  • radiacode/discovery.py: Enhanced BTLEException handling to identify permission-related failures

Example Output

Permission Error:

⚠️  Permission issue detected:
  You are not in the 'bluetooth' group

  To fix this permanently:
    sudo usermod -a -G bluetooth username
    Then logout and login again

  Or run with sudo:
    sudo python device_reader.py

Reconnection:

2025-05-27 00:14:44 - INFO - Connection state: disconnected → connecting
2025-05-27 00:14:44 - INFO - Connecting to Bluetooth 52:43:06:60:13:F2...
2025-05-27 00:14:45 - INFO - Connection state: connecting → connected
2025-05-27 00:14:45 - INFO - ✓ Connected to RC-102-00001234
...
2025-05-27 00:15:12 - ERROR - Bluetooth connection lost
2025-05-27 00:15:12 - INFO - Connection state: connected → disconnected
2025-05-27 00:15:12 - INFO - Connection state: disconnected → reconnecting
2025-05-27 00:15:12 - INFO - Reconnection attempt in 1.0 seconds...
2025-05-27 00:15:13 - INFO - Connection state: reconnecting → connecting
2025-05-27 00:15:14 - INFO - Connection state: connecting → connected
2025-05-27 00:15:14 - INFO - ✓ Connected to RC-102-00001234

Test plan

  • [x] Test running without sudo and not in bluetooth group - shows helpful error
  • [x] Test running with sudo - works correctly
  • [x] Test with user in bluetooth group - provides appropriate guidance
  • [x] Test automatic reconnection on connection loss
  • [x] Verify exponential backoff working correctly
  • [x] Verify connection state logging provides clear visibility

GlassOnTin avatar May 26 '25 16:05 GlassOnTin

Please rebase your branch to the current master and include only commits related to the description in the PR, you can submit another PR for other changes

cdump avatar Jun 02 '25 07:06 cdump