最近在研究自动化脚本,发现以前常用的无障碍技术在一些app上会被风控,所以研究了hid来实现点击和滑动,蓝牙hid成本比较低(9块左右)但是蓝牙多了会相互产生干扰,usb hid是直接插入手机作为otg设备使用再多也不会干扰,所以我详细介绍下如何使用usb hid硬件来模拟点击、滑动等操作。

在 Android 上通过 USB HID 协议,使用 ESP32-S3 模拟鼠标和触控板实现点击、滑动操作,需要结合硬件固件开发和 Android 系统的 USB Host 模式支持。以下是完整的实现方案:

一. 硬件端(ESP32-S3)配置

1.1 硬件要求
  • ESP32-S3:支持 USB OTG(On-The-Go),需连接 USB D+/D- 引脚到手机。

  • USB 连接方式:通过 USB-C 或 Micro-USB 线连接 Android 手机(需支持 OTG 模式)。

1.2 HID 描述符设计

需定义 复合设备描述符,同时包含鼠标和触控板的输入报告。以下是关键代码:

1.2.1 鼠标(相对坐标)
// 鼠标 HID 报告描述符(相对坐标)
static const uint8_t hid_mouse_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.2.2 触控板(绝对坐标)
// 触控板 HID 报告描述符(绝对坐标)
static const uint8_t hid_touchpad_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.3 ESP32-S3 固件开发

使用 TinyUSB 库 实现 USB HID 复合设备:

#include <Arduino.h>
#include <Adafruit_TinyUSB.h>

// 定义 HID 报告描述符
uint8_t const desc_hid_report[] = {
  TUD_HID_REPORT_DESC_KEYBOARD(),
  TUD_HID_REPORT_DESC_MOUSE(),
  TUD_HID_REPORT_DESC_GAMEPAD()
};

Adafruit_USBD_HID usb_hid;
uint8_t report_id = 1;

void setup() {
  Serial.begin(115200);
  
  // 初始化 USB HID
  usb_hid.setPollInterval(2);
  usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
  usb_hid.begin();

  // 等待 USB 连接
  while (!TinyUSBDevice.mounted()) delay(10);
}

void loop() {
  // 发送鼠标移动指令(相对坐标 X=10, Y=0)
  uint8_t mouse_report[] = {0x00, 0x00, 0x0A, 0x00};
  usb_hid.sendReport(report_id, mouse_report, sizeof(mouse_report));
  delay(1000);
}

二. Android 端适配

2.1 Android USB Host 模式
  • USB 权限:需动态申请 USB_PERMISSION

  • HID 协议支持:Android 4.1+ 支持标准 HID 设备,但需处理 USB API。

2.2 代码示例:检测并连接 HID 设备
// 在 AndroidManifest.xml 中添加权限
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION" />

// 检测连接的 USB 设备
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();

for (UsbDevice device : deviceList.values()) {
  // 检查设备是否为 HID
  if (device.getInterface(0).getInterfaceClass() == UsbConstants.USB_CLASS_HID) {
    // 申请权限
    PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    usbManager.requestPermission(device, permissionIntent);
  }
}

// 处理权限回调
BroadcastReceiver usbReceiver = new BroadcastReceiver() {
  public void onReceive(Context context, Intent intent) {
    if (ACTION_USB_PERMISSION.equals(intent.getAction())) {
      UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
      if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
        // 连接设备并读取输入
        UsbInterface usbInterface = device.getInterface(0);
        UsbEndpoint endpointIn = usbInterface.getEndpoint(0); // 输入端点
        UsbDeviceConnection connection = usbManager.openDevice(device);
        connection.claimInterface(usbInterface, true);
        
        // 读取 HID 输入报告
        byte[] buffer = new byte[endpointIn.getMaxPacketSize()];
        connection.bulkTransfer(endpointIn, buffer, buffer.length, 0);
      }
    }
  }
};

三. 输入模拟逻辑

3.1 鼠标操作
  • 点击:发送 BUTTON_1 按下和释放报告。

  • 滑动:持续发送 REL_X 和 REL_Y 位移值。

3.2 触控板操作
  • 绝对坐标:将屏幕分辨率映射到 0x0000-0x7FFF 范围。

  • 多点触控:需定义多个触点报告(参考 HID 触控板协议)。


四. 调试与优化

4.1 常见问题
  • 设备未识别:检查 USB OTG 模式是否启用,或更换数据线。

  • 输入延迟:减少报告发送间隔(如每 10ms 发送一次)。

  • 坐标偏移:校准触控板绝对坐标范围。

4.2 调试工具
  • USB 监控工具:使用 Wireshark 或 USBlyzer 分析 HID 数据包。

  • Android 输入事件

    adb shell getevent -l  # 查看原始输入事件
    adb shell input swipe x1 y1 x2 y2  # 对比触控板滑动效果

五. 完整代码示例(ESP32-S3 触控板)

#include <Adafruit_TinyUSB.h>

// 定义触控板 HID 报告
typedef struct {
  uint8_t contact;  // 触点状态(Bit 0: 触控有效, Bit 1-7: 保留)
  uint16_t x;       // X 坐标 (0-32767)
  uint16_t y;       // Y 坐标 (0-32767)
} touchpad_report_t;

Adafruit_USBD_HID usb_hid;

void setup() {
  // 初始化 USB HID
  usb_hid.setPollInterval(2);
  usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
  usb_hid.begin();
  while (!TinyUSBDevice.mounted()) delay(10);
}

void sendTouchpadReport(uint16_t x, uint16_t y, bool is_touching) {
  touchpad_report_t report = {
    .contact = is_touching ? 0x01 : 0x00,
    .x = x,
    .y = y
  };
  usb_hid.sendReport(REPORT_ID_TOUCHPAD, &report, sizeof(report));
}

void loop() {
  // 模拟从 (1000, 1000) 滑动到 (5000, 5000)
  for (int i = 0; i < 100; i++) {
    sendTouchpadReport(1000 + i*40, 1000 + i*40, true);
    delay(10);
  }
  sendTouchpadReport(0, 0, false); // 释放触点
  delay(1000);
}

六. 获取免费可执行的源代码

免费源码直接去冰狐智能辅助网站:aznfz.com找下客服索取免费usb hid源代码即可。

Logo

智能硬件社区聚焦AI智能硬件技术生态,汇聚嵌入式AI、物联网硬件开发者,打造交流分享平台,同步全国赛事资讯、开展 OPC 核心人才招募,助力技术落地与开发者成长。

更多推荐