【问题标题】:Can a android phone act as a usb hid mouse?安卓手机可以当usb hid鼠标吗?
【发布时间】:2014-11-09 13:30:53
【问题描述】:

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

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

这是否意味着,Android 设备可以充当 USB 隐藏鼠标?

我得到了一个 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;
}

【问题讨论】:

    标签: android usb mouse hid


    【解决方案1】:

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

    查看Android Keyboard Key Assignments here。我不确定他们是否支持 HID Page for Mouse。我已经测试了键盘页面和消费者页面,发现它们可以正常工作。

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

    同样尝试在几毫秒后发送一个释放密钥,此时所有数据都为零。我不确定鼠标的释放键是什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-18
      • 2018-02-02
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多