Skip to content

Improve Bluetooth handling: permission errors and automatic reconnection - #52

Open
GlassOnTin wants to merge 4 commits into
cdump:masterfrom
GlassOnTin:improve-bluetooth-permission-errors
Open

Improve Bluetooth handling: permission errors and automatic reconnection#52
GlassOnTin wants to merge 4 commits into
cdump:masterfrom
GlassOnTin:improve-bluetooth-permission-errors

Conversation

@GlassOnTin

@GlassOnTin GlassOnTin commented May 26, 2025

Copy link
Copy Markdown
Contributor

Two changes from running a RadiaCode as a long-lived monitor, where Bluetooth turned out to be fragile in two distinct ways.

The first is permissions. When scanning fails because the user isn't in the bluetooth group, the BTLEException surfaces as a generic failure with no hint of the cause. radiacode/discovery.py now recognises the permission case, and device_reader.py checks group membership before attempting to scan, so the message can say exactly what to do:

⚠️  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

The second is drops during streaming, which happen in normal use — the device times out after a minute or two of prolonged streaming, other processes grab it, interference, power cycling. Previously the reader just stopped. device_reader.py now tracks a connection state (disconnected/connecting/connected/reconnecting), logs transitions through the standard logging module, and reconnects with exponential backoff from 1 s doubling to a 60 s cap, retrying until stopped:

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

Tested without sudo and outside the bluetooth group (gets the instructions above), with sudo, and with proper group membership; the reconnection path and backoff sequence verified across forced connection drops.

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

cdump commented Jun 2, 2025

Copy link
Copy Markdown
Owner

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