在自动化脚本中使用无障碍会有比较大的概率被风控,现在比较流行的是使用hid硬件来实现模拟点击和滑动等操作,蓝牙hid硬件使用esp32-c3成本最低9块左右,usb hid成本会更高些,所以今天我来给大家详细介绍下如何使用esp32-c3蓝牙hid硬件来实现自动化脚本,给出了完整可行的方案,最后还提供了可直接运行的免费源代码。
在 Android 上通过 蓝牙 HID(Human Interface Device) 协议,使用 ESP32-C3 模拟鼠标和触控板的点击、滑动操作,需要结合硬件固件开发和 Android 系统特性。以下是完整的实现方案:
一. 硬件端(ESP32-C3)配置
1.1 蓝牙 HID 设备定义
ESP32-C3 需配置为 HID 设备,向 Android 发送输入报告。以下是关键步骤:
1.1.1 HID 描述符(Report Descriptor)
定义鼠标和触控板的报告格式。例如,鼠标的 HID 描述符(相对坐标):
// 鼠标 HID 描述符(简化版)
static const uint8_t hid_mouse_report_descriptor[] = {
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x02, // Usage (Mouse)
0xA1, 0x01, // Collection (Application)
0x09, 0x01, // Usage (Pointer)
0xA1, 0x00, // Collection (Physical)
0x05, 0x09, // Usage Page (Button)
0x19, 0x01, // Usage Minimum (1)
0x29, 0x03, // Usage Maximum (3)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x95, 0x03, // Report Count (3)
0x75, 0x01, // Report Size (1)
0x81, 0x02, // Input (Data,Var,Abs)
0x95, 0x01, // Report Count (1)
0x75, 0x05, // Report Size (5)
0x81, 0x03, // Input (Const,Var,Abs)
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x30, // Usage (X)
0x09, 0x31, // Usage (Y)
0x15, 0x81, // Logical Minimum (-127)
0x25, 0x7F, // Logical Maximum (127)
0x75, 0x08, // Report Size (8)
0x95, 0x02, // Report Count (2)
0x81, 0x06, // Input (Data,Var,Rel)
0xC0, // End Collection
0xC0 // End Collection
};
1.1.2 触控板 HID 描述符(绝对坐标)
若需模拟触控板(绝对坐标):
static const uint8_t hid_touchpad_report_descriptor[] = {
0x05, 0x0D, // Usage Page (Digitizer)
0x09, 0x05, // Usage (Touch Pad)
0xA1, 0x01, // Collection (Application)
0x09, 0x22, // Usage (Finger)
0xA1, 0x02, // Collection (Logical)
0x09, 0x42, // Usage (Tip Switch)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x01, // Report Count (1)
0x81, 0x02, // Input (Data,Var,Abs)
0x09, 0x32, // Usage (In Range)
0x81, 0x02, // Input (Data,Var,Abs)
0x09, 0x47, // Usage (Touch Valid)
0x81, 0x02, // Input (Data,Var,Abs)
0x95, 0x05, // Report Count (5)
0x81, 0x03, // Input (Const,Var,Abs)
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x30, // Usage (X)
0x09, 0x31, // Usage (Y)
0x15, 0x00, // Logical Minimum (0)
0x26, 0xFF, 0x7F, // Logical Maximum (32767)
0x75, 0x10, // Report Size (16)
0x95, 0x02, // Report Count (2)
0x81, 0x02, // Input (Data,Var,Abs)
0xC0, // End Collection
0xC0 // End Collection
};
1.2 ESP32-C3 固件开发
使用 Arduino 或 ESP-IDF 框架实现蓝牙 HID 服务:
#include <BLEDevice.h>
#include <BLEHIDDevice.h>
BLEHIDDevice* hid;
BLECharacteristic* input;
void setup() {
BLEDevice::init("ESP32-C3 Mouse");
BLEServer* server = BLEDevice::createServer();
hid = new BLEHIDDevice(server);
// 设置 HID 报告描述符
hid->reportMap((uint8_t*)hid_mouse_report_descriptor, sizeof(hid_mouse_report_descriptor));
hid->startServices();
// 定义输入报告特征
input = hid->inputReport(1); // Report ID=1
// 开始广播
BLEAdvertising* advertising = server->getAdvertising();
advertising->start();
}
void loop() {
// 示例:发送鼠标移动指令(相对坐标 X=10, Y=0)
uint8_t report[] = {0x00, 0x00, 0x0A, 0x00};
input->setValue(report, sizeof(report));
input->notify();
delay(1000);
}
二. Android 端适配
2.1 蓝牙 HID 协议兼容性
-
Android 6.0+ 支持蓝牙 HID 设备,但需处理权限和连接逻辑。
-
触控板绝对坐标 需要 Android 设备支持
DIGITIZER
用法页。
2.2 连接 ESP32-C3
Android 端无需额外代码即可识别蓝牙 HID 设备,但若需主动控制连接,可使用 BluetoothHidDevice
API:
// 在 AndroidManifest.xml 中添加权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
// 代码中注册 HID 主机服务
BluetoothHidDevice bluetoothHidDevice = BluetoothAdapter.getDefaultAdapter().getProfileProxy(
context,
new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.HID_DEVICE) {
// 连接成功
BluetoothHidDevice hidDevice = (BluetoothHidDevice) proxy;
}
}
@Override
public void onServiceDisconnected(int profile) {}
},
BluetoothProfile.HID_DEVICE
);
三. 输入模拟逻辑
3.1 鼠标模拟
-
相对移动:发送
REL_X
和REL_Y
报告。 -
点击事件:通过
BUTTON_1
(左键)、BUTTON_2
(右键)标记。
3.2 触控板模拟
-
绝对坐标:将屏幕分辨率映射到触控板坐标范围(如 0-32767)。
-
多点触控:需定义多个触点报告(参考 HID 多点触控协议)。
四. 调试与优化
4.1 常见问题
-
延迟高:减少报告发送间隔(如每 10ms 发送一次)。
-
坐标偏移:校准触控板坐标范围与 Android 屏幕分辨率。
-
连接不稳定:优化 ESP32-C3 蓝牙天线设计或降低发射功率。
4.2 调试工具
-
Android 输入事件监控:
adb shell getevent -l # 查看原始输入事件 adb shell input tap x y # 模拟点击(对比测试)
-
蓝牙嗅探:使用 Ellisys 或 Frontline 工具分析 HID 数据包。
五. 完整代码示例(ESP32-C3 鼠标)
#include <BLEDevice.h>
#include <BLEHIDDevice.h>
BLEHIDDevice* hid;
BLECharacteristic* input;
void setup() {
BLEDevice::init("ESP32-C3 Mouse");
BLEServer* server = BLEDevice::createServer();
hid = new BLEHIDDevice(server);
// 设置 HID 报告描述符(鼠标)
hid->reportMap((uint8_t*)hid_mouse_report_descriptor, sizeof(hid_mouse_report_descriptor));
hid->startServices();
// 定义输入报告特征(Report ID=1)
input = hid->inputReport(1);
// 广播
BLEAdvertising* advertising = server->getAdvertising();
advertising->addServiceUUID(hid->hidService()->getUUID());
advertising->start();
}
void sendMouseMove(int8_t dx, int8_t dy) {
uint8_t report[] = {0x00, 0x00, dx, dy}; // [按钮状态, 保留, X位移, Y位移]
input->setValue(report, sizeof(report));
input->notify();
}
void loop() {
sendMouseMove(10, 0); // 向右移动
delay(1000);
sendMouseMove(-10, 0); // 向左移动
delay(1000);
}
六. 获取免费可执行的源代码
免费源码直接去冰狐智能辅助网站:aznfz.com找下客服索取免费蓝牙hid源代码即可。