android - android 手机可以充当 usb hid 鼠标吗?

标签 android usb mouse hid

在 Android 开放访问协议(protocol) 2.0 中,标题为 HID 支持的部分:

AOA 2.0 协议(protocol)添加了四个新的 USB 控制请求,以允许配件充当 Android 设备的一个或多个 HID 输入设备。由于 HID 支持完全通过端点零上的控制请求完成,因此不需要新的 USB 接口(interface)来提供此支持。

这是否意味着,Android 设备可以充当 usb hid 鼠标?

我得到了一个 Arduino 的例子 https://github.com/YuuichiAkagawa/Arduino-AOA2/blob/master/AOA2/examples/AOA2HID/AOA2HID.ino .

并编写如下演示程序。但它不起作用。 android设备是否可以像Arduino示例那样成为usb hid鼠标?谢谢!

private void initIdentity(UsbDeviceConnection connection) {
    Log.i("initAccessory", "initStringControlTransfer");

    initStringControlTransfer(connection, 2,
            "showcasing android2android USB communication"); // DESCRIPTION
    initStringControlTransfer(connection, 3, "0.1"); // VERSION
    initStringControlTransfer(connection, 4, "http://minji.cn"); // URI
    initStringControlTransfer(connection, 5, "42"); // SERIAL
}

private void initStringControlTransfer(
        final UsbDeviceConnection deviceConnection, final int index,
        final String string) {
    deviceConnection.controlTransfer(0x40, 52, 0, index, string.getBytes(),
            string.length(), USB_TIMEOUT_IN_MS);
}

private int HID_ID = 0;

private int GET_PROTOCOL = 51;
private int ACCESSORY_REGISTER_HID = 54;
private int ACCESSORY_UNREGISTER_HID = 55;
private int ACCESSORY_SET_HID_REPORT_DESC = 56;
private int ACCESSORY_SEND_HID_EVENT = 57;

char[] _hidReportDescriptor = {
        // Mouse
        0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01, 0xA1,
        0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x08, 0x15, 0x00, 0x25, 0x01,
        0x95, 0x08, 0x75, 0x01, 0x81, 0x02, 0x95,
        0x00, 0x81, 0x03, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09,
        0x38, 0x15, 0x81, 0x25, 0x7F, 0x75, 0x08, 0x95, 0x03,
        0x81, 0x06, 0x05, 0x0C, 0x0A, 0x38, 0x02, 0x95, 0x01,
        0x81, 0x06, 0xC0, 0xC0, };

private void initUsbHid(UsbDeviceConnection connection) {
    connection.controlTransfer(REQUEST_OUT_TYEP, ACCESSORY_REGISTER_HID,
            HID_ID, _hidReportDescriptor.length, new byte[] {}, 0,
            USB_TIMEOUT_IN_MS);

    connection.controlTransfer(REQUEST_OUT_TYEP,
            ACCESSORY_SET_HID_REPORT_DESC, HID_ID, 0, _hidReportDescriptor.toString().getBytes(),
            0, USB_TIMEOUT_IN_MS);
}

private void unRegisteHid() {
    UsbDeviceConnection connection = mUsbManager.openDevice(mUsbDevice);
    connection.controlTransfer(REQUEST_OUT_TYEP, ACCESSORY_UNREGISTER_HID,
            HID_ID, 0, new byte[] {}, 0, USB_TIMEOUT_IN_MS);
}

public void send(String msg) {
    Log.i(this.getClass().getName(), msg);
    UsbDeviceConnection connection = mUsbManager.openDevice(mUsbDevice);

    byte[] m = new byte[5];
    m[2] = 0;
    m[1] = 0;
    m[0] = 1;

    connection.controlTransfer(REQUEST_OUT_TYEP, ACCESSORY_SEND_HID_EVENT,
            HID_ID, 0, m, m.length, USB_TIMEOUT_IN_MS);
}

private void startAccessory(UsbDeviceConnection connection) {
    connection.controlTransfer(REQUEST_OUT_TYEP, // request type for this
                                                    // transaction
            53, // request ID for this transaction
            0, // value field for this transaction
            0, // index field for this transaction
            new byte[] {}, // buffer for data portion of transaction, or
                            // null if no data needs to be sent or received
            0, // the index of the first byte in the buffer to send or
                // receive
            USB_TIMEOUT_IN_MS);
}

private boolean initAccessory(final UsbDevice device) {
    Log.i("initAccessory", "initAccessory start");

    final UsbDeviceConnection connection = mUsbManager.openDevice(device);

    if (connection == null) {
        Log.i("initAccessory", "connection == null");
        return false;
    }

    int protocol = getProtocol(connection);
    if (protocol <= 1) {
        Log.i("initAccessory", "Protocol: " + protocol
                + " not support USB HID");
        return false;
    }

    //initIdentity(connection);

    initUsbHid(connection);

    //startAccessory(connection);

    connection.close();

    return true;
}

最佳答案

AOA 2.0 HID 类实现允许附加到 Android 手机的配件充当 HID 设备。但反之则不然。

查看 Android Keyboard Key Assignments here .我不确定他们是否支持鼠标的 HID 页面。我已经测试了 Keyboard Page 和 Consumer Page,发现它们可以正常工作。

我发现你的附件 ID 是 0 (private int HID_ID = 0;),也许使用其他东西。我不确定他们是否枚举了 ID 为 0 的设备。

也尝试在几毫秒后发送释放 key ,其中所有数据均为零。我不确定鼠标的释放键是什么。

关于android - android 手机可以充当 usb hid 鼠标吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26828715/

相关文章:

c# - 释放未插入的虚拟串行端口

android - 删除 USB 附件权限对话框

c - C 中的简单 NCURSES 鼠标处理

Android 应用程序在崩溃后自动重启

android - 单击时如何将按钮的效果赋予 ImageView?

javascript - 使用 CSS 的绝对大小

c++ - 在 Windows/C++ 上控制 USB TMC 设备的最简单方法

c - OpenGL 鼠标 "lock"

Qt鼠标光标透明度

android - 要存储在缓存中的图像数组?