Skip to content

Conversation

@GlassOnTin
Copy link
Contributor

@GlassOnTin GlassOnTin commented May 26, 2025

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

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

GlassOnTin and others added 4 commits August 1, 2024 18:14
- Detect when user is not in bluetooth group
- Provide clear instructions for permanent fix (usermod command)
- Show exact sudo command as temporary workaround
- Enhanced error messages for common Bluetooth failures
- Check permissions before attempting Bluetooth scan
- Improve user experience when running without proper permissions

This makes it much clearer to users why Bluetooth scanning fails
and provides actionable steps to resolve the issue.
- Implement connection state tracking (disconnected/connecting/connected/reconnecting)
- Add exponential backoff for reconnection attempts (1s to 60s max)
- Enhance error handling to detect Bluetooth disconnections
- Add comprehensive logging for connection state changes
- Ensure device continues attempting reconnection until manually stopped

This improves reliability when Bluetooth connections drop due to:
- Device timeouts after prolonged streaming
- Bluetooth interference
- Device power cycling
- Other processes connecting to the device
@GlassOnTin GlassOnTin changed the title Improve Bluetooth permission error handling Improve Bluetooth handling: permission errors and automatic reconnection May 26, 2025
@cdump
Copy link
Owner

cdump commented Jun 2, 2025

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

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