如何在Android上使用UsbDeviceConnection从HID设备请求USB HID报告描述符 [英] How to request USB HID report descriptor from HID devices using UsbDeviceConnection on Android

查看:355
本文介绍了如何在Android上使用UsbDeviceConnection从HID设备请求USB HID报告描述符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UsbDeviceConnection.controlTransfer 获取USB设备的HID报告描述符,因此我可以看到USB HID设备具有哪些按钮.

I'm trying to use UsbDeviceConnection.controlTransfer to get the HID report descriptor for USB devices, so I can see what buttons the USB HID device has.

我已经能够通过使用 bulkTransfer 从HID设备获取实际的输入数据.

I've been able to get the actual input data from the HID devices by using a bulkTransfer.

我已经查找了它,只能找到用于创建和解析HID报告描述符的教程.我尝试在不同的范围内输入 controlTransfer ,但是我一直无法弄清楚.

I've looked it up and can only find tutorials for creating and parsing a HID report descriptor. I've tried entering in different perimeters into controlTransfer, but I've been unable to figure it out.

我应该将哪些值传递到 controlTransfer 中以获得USB HID报告描述符,以便可以开始解析设备具有的按钮以及它们分配给的字节?还是不应该使用 controlTransfer 来获取HID报告描述符?我刚接触USB.

What values should I pass into controlTransfer to get the USB HID report descriptor, so I can start parsing what buttons the devices have and what bytes they are assigned to? Or are you not supposed to use controlTransfer to get the HID report descriptor? I'm new to working with USB.

推荐答案

这确实很晚,但是如果您还没有提出解决方案或其他人正在寻找解决方案,这就是我的解决方案.我应该提到,我对这些都不是很有经验,所以一些细节可能会漏掉,但是总的来说是存在的.

This is really late, but in case you haven't come up with a solution or someone else is looking to do this, this was my solution. I should mention, I am not very experienced with any of this, so some details may be off, but the general picture is there.

UsbDeviceConnection.controlTransfer 需要7个变量:

  • 请求类型
  • 请求
  • 请求值
  • 请求索引
  • 输出缓冲区
  • 缓冲区大小
  • 超时

请求类型描述了转移的方向,类型和接收者.在这种情况下,我们要使用标准(00)传输读取(1),并且要查询接口(00001).我们要查询接口,因为HID是设备的接口.因此, 0b10000001 0x81 .

Request type describes the direction, type, and recipient of the transfer. In this case, we want to read (1) using a standard (00) transmission and we want to query an interface (00001). We want to query an interface because HID is an interface of the device. So, 0b10000001 or 0x81.

请求描述了我们的特定请求.我们正在寻找HID报告 Descriptor ,并且规范中将 GET_DESCRIPTOR 定义为 0x06 .

Request describes our specific request. We are looking for the HID Report Descriptor and GET_DESCRIPTOR is defined as 0x06 by the spec.

请求值由描述符类型(高字节)和接口索引(低字节)组成.在我们的例子中,描述符类型是HID报告或 0x22 (这是从HID规范中获得的,特别是类描述符).接口的索引与下面的请求索引相同.以我为例,它是 0x00 ,但是您的可能有所不同.结合高位和低位,我们得到 0x2200 作为我们的请求值.

Request value consists of the descriptor type as the high byte and the index of the interface as the low byte. Descriptor type in our case is HID Report or 0x22 (this is from the HID spec, specifically class descriptors). Index of the interface is the same as request index below. In my case it was 0x00, but yours may be different. Combining the high and low bit, we get 0x2200 for our request value.

请求索引指定要查询的界面.如果HID是设备的唯一接口,则它将为 0x00 .否则,您将必须在配置描述符中检查可用接口的类型及其索引.

Request index specifies which interface you are querying. If HID is the only interface of the device, then this will be 0x00. Otherwise, you will have to check the configuration descriptor for the types of interfaces available and their indices.

输出缓冲区是为事务返回分配的空间,其大小应由缓冲区的大小指定,如下所述.

Output buffer is the space allocated for the return of the transaction and should be of the size specified by size of buffer, explained below.

缓冲区大小描述了报告描述符中的字节数.此值在配置描述符中,在HID类特定的描述符( bDescriptorType = 0x21 )中指定,并且是第8个字节的值( wDescriptorLength ),共9个.在我的情况下,该值为104.

Size of buffer describes the number of bytes in the report descriptor. This value is specified in the configuration descriptor, in the HID class-specific descriptor (bDescriptorType = 0x21) and is the value of the 8th byte (wDescriptorLength) out of the 9 total. In my case this value was 104.

超时是要放弃的毫秒数.我用的是 2000 .

Timeout is the number of milliseconds after which to give up. I used 2000.

将它们放在一起, UsbDeviceConnection.controlTranfer(0x81、0x06、0x2200、0x00,字节[]缓冲区,104、2000).

https://www.beyondlogic.org/usbnutshell/usb6.shtml 很好地概述了 controlTransfer 中使用的位字段,请查看是否需要更多上下文.

https://www.beyondlogic.org/usbnutshell/usb6.shtml gives an excellent overview of the bit fields used in controlTransfer, check it out if you want more context.

https://eleccelerator.com/usbdescreqparser/是了解USB和HID的非常有用的工具描述符,可能比仅阅读规格表和API更具有洞察力.

https://eleccelerator.com/usbdescreqparser/ is an extremely useful tool for understanding USB and HID descriptors, and may be more insightful than reading the spec sheets and the API alone.

这篇关于如何在Android上使用UsbDeviceConnection从HID设备请求USB HID报告描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆