A robust Python library for handling Bluetooth and USB remote controls with automatic reconnection
Project description
Bluetooth Remote Control Library
A robust Python library for handling Bluetooth remote controls that go to sleep, combining udev device monitoring with evdev event handling for seamless reconnection and event processing.
Features
- 🔄 Automatic Reconnection: Handles Bluetooth remotes that go to sleep and wake up
- 📱 Multiple Device Support: Manage multiple remotes simultaneously with individual configurations
- ⚡ Real-time Detection: Uses udev for instant device discovery (no polling)
- 🎯 Flexible Filtering: Filter devices by name, vendor ID, product ID, or bus type
- 🔧 Per-Device Handlers: Different event handlers and settings for each remote type
- 🏗️ Async/Await: Built with modern Python async patterns
- 🧵 Thread-Safe: Proper threading and cleanup
- 🔍 Device Discovery: Built-in tools to find your remote's details
Installation
Using uv (recommended)
uv add bluetooth-remote-lib
Using pip
pip install bluetooth-remote-lib
Quick Start
import asyncio
from bluetooth_remote_lib import RemoteManager, RemoteEventHandler, KeyEvent
class MyHandler(RemoteEventHandler):
async def on_key_event(self, event: KeyEvent):
print(f"Key pressed: {event.keycode}")
async def main():
handler = MyHandler()
manager = RemoteManager(
event_handler=handler,
device_filters={'name': 'remote'} # Match devices with 'remote' in name
)
async with manager.managed_context():
await asyncio.sleep(60) # Run for 60 seconds
asyncio.run(main())
Advanced Usage - Multiple Remotes
import asyncio
from bluetooth_remote_lib import (
RemoteManager, RemoteEventHandler, RemoteConfig,
KeyEvent, KeyEventType, DeviceInfo
)
# Define handlers for specific remotes
async def handle_fire_tv(event: KeyEvent):
if event.event_type == KeyEventType.KEY_DOWN:
print(f"🔥 Fire TV: {event.keycode}")
async def handle_roku(event: KeyEvent):
if event.event_type == KeyEventType.KEY_DOWN:
print(f"📺 Roku: {event.keycode}")
class MainHandler(RemoteEventHandler):
async def on_key_event(self, event: KeyEvent):
print(f"Generic remote: {event.keycode}")
async def on_device_connected(self, device_info: DeviceInfo):
print(f"Connected: {device_info.name}")
# Configure multiple remotes with individual settings
remote_configs = [
RemoteConfig(
name="Fire TV Remote",
filters={
'name': 'fire tv',
'vendor_id': 0x1949, # Amazon
},
handler=handle_fire_tv,
reconnect_delay=1.5, # Custom timing
scan_interval=3.0
),
RemoteConfig(
name="Roku Remote",
filters={
'name': 'roku',
'vendor_id': 0x0B05, # Roku
},
handler=handle_roku,
reconnect_delay=2.0
)
]
async def main():
manager = RemoteManager(
event_handler=MainHandler(),
remote_configs=remote_configs
)
async with manager.managed_context():
while True:
await asyncio.sleep(1)
asyncio.run(main())
Device Discovery
Find your remote's vendor/product IDs:
# Discover all input devices
bt-remote-discover
# Or use the module directly
python -m bluetooth_remote_lib discover
# Find specific devices
bt-remote find --name "fire tv"
bt-remote find --vendor-id 0x1949
Example output:
Device: Amazon Fire TV Remote
Path: /dev/input/event5
Vendor ID: 0x1949 (6473)
Product ID: 0x0404 (1028)
Key capabilities: 15 keys
Keys: KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_ENTER...
Configuration Options
RemoteConfig Parameters
- name: Friendly name for the remote configuration
- filters: Dictionary of device matching criteria:
name: Substring to match in device name (case insensitive)vendor_id: Vendor ID (integer)product_id: Product ID (integer)bus_type: Bus type (integer)
- handler: Optional async function to handle events from this remote
- reconnect_delay: Custom delay before reconnection attempts (seconds)
- scan_interval: Custom interval between reconnection scans (seconds)
Manager Parameters
- event_handler: Main event handler (RemoteEventHandler subclass)
- remote_configs: List of RemoteConfig objects
- device_filters: Legacy single filter dict (use remote_configs instead)
- reconnect_delay: Default reconnection delay (2.0 seconds)
- scan_interval: Default scan interval (5.0 seconds)
- log_level: Logging level (logging.INFO)
Event Handling
Key Events
class MyHandler(RemoteEventHandler):
async def on_key_event(self, event: KeyEvent):
# event.keycode: 'KEY_UP', 'KEY_HOME', etc.
# event.event_type: KEY_DOWN, KEY_UP, KEY_HOLD
# event.timestamp: Event timestamp
# event.device_name: Name of the device
# event.raw_event: Original evdev event
if event.event_type == KeyEventType.KEY_DOWN:
if event.keycode == 'KEY_HOME':
print("Home button pressed!")
Device Events
class MyHandler(RemoteEventHandler):
async def on_device_connected(self, device_info: DeviceInfo):
print(f"Device connected: {device_info.name}")
print(f"Vendor: 0x{device_info.vendor_id:04x}")
async def on_device_disconnected(self, device_info: DeviceInfo):
print(f"Device disconnected: {device_info.name}")
async def on_device_reconnecting(self, device_info: DeviceInfo):
print(f"Reconnecting: {device_info.name}")
Requirements
- Linux: This library uses Linux-specific evdev and udev systems
- Python 3.8+: Modern async/await support
- Permissions: May require user to be in
inputgroup or run with appropriate permissions
System Setup
# Add user to input group (recommended)
sudo usermod -a -G input $USER
# Then log out and back in
# Or install udev rule for your specific devices
echo 'SUBSYSTEM=="input", GROUP="input", MODE="0664"' | sudo tee /etc/udev/rules.d/99-input.rules
sudo udevadm control --reload-rules
Examples
The examples/ directory contains:
basic_usage.py: Simple single remote exampleadvanced_usage.py: Multiple remotes with individual configurations
Run examples:
python examples/basic_usage.py
python examples/advanced_usage.py
Contributing
Contributions welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
See CHANGELOG.md for version history and changes.
Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Related Projects
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ev_remote_lib-0.7.0.tar.gz.
File metadata
- Download URL: ev_remote_lib-0.7.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc2d3f2ba745fdf7d3f103f43466f6784bd33e8dce0ad36a18809c6566f01c4d
|
|
| MD5 |
7f8a5a7fcc12563a134481840acc9803
|
|
| BLAKE2b-256 |
31a28cdb85a3aa40b944a98e639bc261ad5aa4ce74ce58e3059a6755bac7c57e
|
File details
Details for the file ev_remote_lib-0.7.0-py3-none-any.whl.
File metadata
- Download URL: ev_remote_lib-0.7.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e0e45890c0f0b09146f10b88ec519005debb0f7e3464c6beedf83a028f4a46c
|
|
| MD5 |
a7fdfb6fcc3fc022ebb9eb0f315ead65
|
|
| BLAKE2b-256 |
bd2f694e48f65025108352e3e249541040a8548d280c7c04020b1ae9000216ac
|