首页 文章

Android手机可以作为USB隐藏鼠标吗?

提问于
浏览
1

在Android Open Access Protocol 2.0中, Headers 为HID支持的部分:

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

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

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

并编写如下演示节目 . 但它不起作用 . 一个Android设备是否可以像Arduino一样成为一个usb隐藏鼠标呢?谢谢!

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;
}

1 回答

  • 1

    AOA 2.0 HID类实现允许连接到Android手机的附件充当HID设备 . 但不是相反 .

    查看Android Keyboard Key Assignments here . 我不确定他们是否支持鼠标的HID页面 . 我测试了键盘页面和消费者页面,发现它们正常工作 .

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

    还尝试在几毫秒后发送一个释放密钥,其中所有数据都为零 . 我不确定鼠标的释放键是什么 .

相关问题