English | 中文
This project transforms an ESP32-S3 into a powerful bridge between USB HID devices (keyboards and mice) and Bluetooth Low Energy (BLE) HID, enabling any wired USB keyboard or mouse to function as a wireless BLE HID device. Simply plug your USB device into the ESP32-S3 and connect to your Mac, PC, tablet, or phone via Bluetooth.
Key Highlights:
- ✅ Full macOS Compatibility – Optimized for macOS with Report Protocol support
- ✅ High DPI Mouse Support – 16-bit precision (-32768 to 32767) for smooth high-resolution mouse movement
- ✅ Composite HID Device – Simultaneously supports Keyboard, Mouse, Consumer Control, and Gamepad
- ✅ Advanced Motion Processing – Ring Buffer + Time Window resampling for stable, lossless mouse movement
- ✅ Multi-Button Mouse – Supports up to 5 buttons (left, right, middle, side buttons 4 & 5)
- Detects and reads input from standard USB HID keyboards and mice via TinyUSB
- Supports both Boot Protocol and Report Protocol modes
- Automatic protocol detection and switching
- Sends keystrokes and mouse movements over Bluetooth as a standard BLE HID device
- Compatible with macOS, Windows, Linux, iOS, and Android
- Low latency and stable connection
- Report Protocol Support: Automatically switches USB devices to Report Protocol mode for macOS compatibility
- 16-bit Mouse Precision: Full support for high DPI mice with 16-bit X/Y coordinates
- Stable Polling: Fixed 7.5ms (133Hz) BLE transmission rate for consistent performance
- 16-bit Coordinate Range: Supports mouse movement from -32768 to +32767 pixels per report
- Motion Accumulator: Advanced Ring Buffer + Time Window algorithm ensures no movement loss
- Smooth Movement: Handles high-frequency USB input (up to 1000Hz) and resamples to stable BLE rate
| LED Color | Meaning |
|---|---|
| 🔴 Red | USB & BLE both disconnected |
| 🟢 Green | USB device connected |
| 🔵 Blue | BLE device connected |
| ⚪ White | Both USB and BLE connected |
- Supports modifier keys (Ctrl, Alt, Shift, Cmd) and up to 6 simultaneous keypresses
- Consumer Control support (volume, media keys, etc.)
- Optional Gamepad support (configurable)
- Proper key release events for correct key repetition behavior
- Thread-safe motion accumulator with spinlock protection
This project includes significant improvements over basic USB-to-BLE implementations:
- Problem Solved: USB polling rate (often 1000Hz) doesn't match BLE transmission rate (~133Hz), causing movement loss and jittery behavior
- Solution: Ring Buffer stores USB events with microsecond timestamps, then integrates movement over time windows for stable BLE transmission
- Benefits:
- No movement loss even during high-speed mouse movements
- Stable 133Hz transmission rate regardless of USB input frequency
- Handles USB input jitter and bursts gracefully
- Two-phase commit ensures data integrity even if BLE transmission fails
- Problem Solved: macOS requires Report Protocol mode for USB HID devices, while many devices default to Boot Protocol
- Solution: Automatically detects and switches USB devices to Report Protocol mode
- Benefits: Seamless compatibility with macOS without manual configuration
- Problem Solved: Standard 8-bit mouse coordinates (-127 to +127) are insufficient for high DPI mice, causing pixelation and loss of precision
- Solution: 16-bit HID Report Map with full 16-bit coordinate support
- Benefits: Smooth, precise mouse movement even with high DPI mice (4000+ DPI)
- Extended button support beyond standard 3-button mice
- Supports side buttons (buttons 4 & 5) commonly found on gaming mice
- Single BLE device can act as Keyboard, Mouse, Consumer Control, and Gamepad simultaneously
- Proper HID Report Map with multiple Report IDs
- Problem Solved: USB HID devices use complex report descriptors that need to be parsed to extract input data correctly, especially for Report Protocol mode devices
- Solution: Advanced HID report parser that can handle various report descriptor formats and extract keycodes, mouse movements, and button states accurately
- Benefits:
- Supports both Boot Protocol and Report Protocol modes
- Handles complex report descriptors with multiple Report IDs
- Accurate extraction of all input fields including buttons, axes, and consumer controls
- Compatible with a wide range of USB HID devices
-
USB Initialization
The ESP32-S3 acts as a USB host using TinyUSB to enumerate and communicate with connected HID devices. -
Device Detection & Protocol Switching
Automatically detects device type (keyboard/mouse) and switches to Report Protocol mode for macOS compatibility. -
HID Report Parsing
Incoming USB HID reports are parsed to extract keycodes, mouse movements, and button states. -
Motion Accumulation (Mouse Only)
Mouse movements are accumulated in a Ring Buffer with timestamps, then integrated over time windows for stable BLE transmission. -
BLE HID Emulation
The ESP32-S3 translates parsed data into BLE HID reports and sends them via GATT notifications to the connected Bluetooth device.
- ESP32-S3 board with native USB OTG support
- ESP-IDF v5.x or later
- USB keyboard/mouse + USB OTG adapter/cable
- Serial monitor (for debugging)
idf.py set-target esp32s3
idf.py build
idf.py flash monitor- Power the ESP32-S3 and connect a USB keyboard or mouse.
- On your Mac, PC, or phone, scan for Bluetooth devices.
- Connect to the advertised "BLE HID" device.
- The LED will turn white when both USB and BLE are connected.
- Only supports standard USB HID devices (Boot Protocol and Report Protocol)
- BLE battery service is not implemented
- Some vendor-specific keys or features may not work
- Maximum 6 simultaneous keypresses (HID standard limitation)
- Add BLE battery service
- Support for more multimedia keys
- Configurable polling rates
- Support for additional HID device types
- ESP-IDF – Espressif IoT Development Framework
- TinyUSB – USB Host stack
- ESP BLE HID Profile – For Bluetooth HID emulation
This project is built upon the excellent work of:
- Vengeance110703/USB-to-BLE-Keyboard – Original USB-to-BLE keyboard implementation that served as the foundation for this project
- pasztorpisti/hid-report-parser – USB HID report parser library that provided inspiration and reference for HID descriptor parsing
- Espressif Systems – For ESP-IDF framework and ESP32-S3 hardware
- TinyUSB – For the robust USB Host stack
- ESP-IDF BLE HID Examples – For the BLE HID profile implementation foundation
Special thanks to the open-source community for their contributions and feedback.
本项目将 ESP32-S3 打造成一个强大的 USB HID 设备(键盘和鼠标)与 蓝牙低功耗 (BLE) HID 之间的桥接器,让任何有线 USB 键盘或鼠标都能作为无线 BLE HID 设备使用。只需将 USB 设备插入 ESP32-S3,然后通过蓝牙连接到您的 Mac、PC、平板或手机。
核心亮点:
- ✅ 完整 macOS 兼容性 – 针对 macOS 优化,支持 Report Protocol
- ✅ 高 DPI 鼠标支持 – 16 位精度(-32768 至 32767),支持高分辨率鼠标平滑移动
- ✅ 复合 HID 设备 – 同时支持键盘、鼠标、消费控制(Consumer Control)和游戏手柄
- ✅ 高级运动处理 – Ring Buffer + 时间窗重采样,确保稳定、无丢失的鼠标移动
- ✅ 多按键鼠标 – 支持最多 5 个按键(左、右、中键及侧键 4、5)
- 通过 TinyUSB 检测并读取标准 USB HID 键盘和鼠标输入
- 支持 Boot Protocol 和 Report Protocol 两种模式
- 自动协议检测和切换
- 通过蓝牙以标准 BLE HID 设备形式发送按键和鼠标移动
- 兼容 macOS、Windows、Linux、iOS 和 Android
- 低延迟、稳定连接
- Report Protocol 支持:自动将 USB 设备切换到 Report Protocol 模式以确保 macOS 兼容性
- 16 位鼠标精度:完整支持高 DPI 鼠标,使用 16 位 X/Y 坐标
- 稳定轮询:固定 7.5ms(133Hz)BLE 传输速率,确保一致性能
- 16 位坐标范围:支持每次报告 -32768 至 +32767 像素的鼠标移动
- 运动累加器:先进的 Ring Buffer + 时间窗算法确保不丢失任何移动
- 平滑移动:处理高频 USB 输入(最高 1000Hz)并重采样为稳定的 BLE 速率
| LED 颜色 | 含义 |
|---|---|
| 🔴 红色 | USB 和 BLE 均未连接 |
| 🟢 绿色 | USB 设备已连接 |
| 🔵 蓝色 | BLE 设备已连接 |
| ⚪ 白色 | USB 和 BLE 均已连接 |
- 支持修饰键(Ctrl、Alt、Shift、Cmd)和最多 6 个同时按键
- 消费控制支持(音量、媒体键等)
- 可选游戏手柄支持(可配置)
- 正确的按键释放事件,确保按键重复行为正常
- 线程安全的运动累加器,使用自旋锁保护
相比基础的 USB-to-BLE 实现,本项目包含以下重要改进:
- 解决的问题:USB 轮询速率(通常 1000Hz)与 BLE 传输速率(约 133Hz)不匹配,导致移动丢失和抖动
- 解决方案:Ring Buffer 存储带微秒时间戳的 USB 事件,然后在时间窗内积分运动数据以实现稳定的 BLE 传输
- 优势:
- 即使高速移动鼠标也不会丢失移动数据
- 无论 USB 输入频率如何,都保持稳定的 133Hz 传输速率
- 优雅处理 USB 输入抖动和突发
- 两阶段提交确保即使 BLE 传输失败也能保证数据完整性
- 解决的问题:macOS 要求 USB HID 设备使用 Report Protocol 模式,而许多设备默认使用 Boot Protocol
- 解决方案:自动检测并将 USB 设备切换到 Report Protocol 模式
- 优势:无需手动配置即可与 macOS 无缝兼容
- 解决的问题:标准 8 位鼠标坐标(-127 至 +127)不足以支持高 DPI 鼠标,导致像素化和精度丢失
- 解决方案:16 位 HID Report Map,完整支持 16 位坐标
- 优势:即使使用高 DPI 鼠标(4000+ DPI)也能实现平滑、精确的鼠标移动
- 扩展了标准 3 键鼠标的按键支持
- 支持游戏鼠标常见的侧键(按键 4 和 5)
- 单个 BLE 设备可同时作为键盘、鼠标、消费控制和游戏手柄使用
- 具有多个 Report ID 的完整 HID Report Map
- 解决的问题:USB HID 设备使用复杂的报告描述符,需要正确解析才能提取输入数据,特别是 Report Protocol 模式设备
- 解决方案:先进的 HID 报告解析器,能够处理各种报告描述符格式,准确提取键码、鼠标移动和按键状态
- 优势:
- 支持 Boot Protocol 和 Report Protocol 两种模式
- 处理具有多个 Report ID 的复杂报告描述符
- 准确提取所有输入字段,包括按键、轴和消费控制
- 兼容广泛的 USB HID 设备
-
USB 初始化
ESP32-S3 作为 USB 主机,使用 TinyUSB 枚举并与连接的 HID 设备通信。 -
设备检测和协议切换
自动检测设备类型(键盘/鼠标)并切换到 Report Protocol 模式以确保 macOS 兼容性。 -
HID 报告解析
解析传入的 USB HID 报告,提取键码、鼠标移动和按键状态。 -
运动累加(仅鼠标)
鼠标移动以时间戳形式累加到 Ring Buffer 中,然后在时间窗内积分以实现稳定的 BLE 传输。 -
BLE HID 模拟
ESP32-S3 将解析后的数据转换为 BLE HID 报告,并通过 GATT 通知发送到连接的蓝牙设备。
- 支持原生 USB OTG 的 ESP32-S3 开发板
- ESP-IDF v5.x 或更高版本
- USB 键盘/鼠标 + USB OTG 适配器/线缆
- 串口监视器(用于调试)
idf.py set-target esp32s3
idf.py build
idf.py flash monitor- 给 ESP32-S3 上电并连接 USB 键盘或鼠标。
- 在您的 Mac、PC 或手机上扫描蓝牙设备。
- 连接到名为 "BLE HID" 的设备。
- 当 USB 和 BLE 都连接时,LED 将变为白色。
- 仅支持标准 USB HID 设备(Boot Protocol 和 Report Protocol)
- 未实现 BLE 电池服务
- 某些厂商特定的按键或功能可能无法工作
- 最多 6 个同时按键(HID 标准限制)
- 添加 BLE 电池服务
- 支持更多多媒体按键
- 可配置的轮询速率
- 支持更多 HID 设备类型
- ESP-IDF – Espressif 物联网开发框架
- TinyUSB – USB Host 协议栈
- ESP BLE HID Profile – 用于蓝牙 HID 模拟
本项目基于以下优秀工作构建:
- Vengeance110703/USB-to-BLE-Keyboard – 原始 USB-to-BLE 键盘实现,为本项目提供了基础
- pasztorpisti/hid-report-parser – USB HID 报告解析器库,为 HID 描述符解析提供了参考和灵感
- Espressif Systems – 提供 ESP-IDF 框架和 ESP32-S3 硬件
- TinyUSB – 提供强大的 USB Host 协议栈
- ESP-IDF BLE HID 示例 – 提供 BLE HID 配置文件实现基础
特别感谢开源社区的贡献和反馈。
This project is open source. Please refer to the license file for details.
本项目为开源项目。详情请参阅许可证文件。