Android USB HID bulkTransfer()参数解析

来源:互联网 发布:心动网络校招面试 编辑:程序博客网 时间:2024/06/06 03:25

一、概述

1、上一篇文章,记录的是Android UsbDeviceConnection.controlTransfer() 参数解析,那个也是相对于较难的,这文章是记录调试公司另一种产品,使用USB HID 类中的“中断传输”方式。大笑

2、在Android USB Hos通讯开发中,中断传输是使用bulkTransfer() 方法的,这也是百度上资料最多的,用的最多最广范的一种usb通讯方式。

二、参数解析

1、中断传输:
(1)在USB HID 中,中断传输是HID设备使用中断管道与主机通信。

(2)在USB HID 设备类协议中,中断传输分为: A、中断输入 -- 传输从设备到主机的输入数据    B、 中断输出 -- 传输从主机到设备的输出数据
其中,中断输入是必须的,而中断输出是可选的。

(3)下面的代码是谷歌官方的介绍

    /**     * Performs a bulk transaction on the given endpoint.     * The direction of the transfer is determined by the direction of the endpoint.     * <p>     * This method transfers data starting from index 0 in the buffer.     * To specify a different offset, use     * {@link #bulkTransfer(UsbEndpoint, byte[], int, int, int)}.     * </p>     *     * @param endpoint the endpoint for this transaction     * @param buffer buffer for data to send or receive     * @param length the length of the data to send or receive     * @param timeout in milliseconds     * @return length of data transferred (or zero) for success,     * or negative value for failure     */    public int bulkTransfer(UsbEndpoint endpoint, byte[] buffer, int length, int timeout);

意思就是说,这个函数是使用端点传输的,且输出和输入都是使用同一个方法,传输方向取决于所使用的端点,
也就是使用输入端点,则是Host 主机从HID从机获取数据,使用输出端点,则是Host主机向HID从机发送数据。


(1)endpoint : 定义通讯使用的端点, OUT or IN(Host to Device用OUT,Device to Host 用IN
如何获取输入或则输出端点呢?疑问
这就需要使用从接口中获取,我使用的,
        mUsbInterface = mUsbDevice.getInterface(0);        if(mUsbInterface == null)            return 2;        //从端点0, 为输入端点        mInEndpoint = mUsbInterface.getEndpoint(0);//端点号,一般可以在HID设备的端点描述符中查到,并且可以确定输入输出端点号        if(mInEndpoint == null)            return 3;        //端点1,一般为输出端点        mOutEndpoint = mUsbInterface.getEndpoint(1);        if(mOutEndpoint == null)            return 4;

(2)buffer: 输出流/h或输入流
当endpoint 指定为输出端点时,则buffer 定义为输出报告。
当endpoint指定为输入端点时,则buffer 定义为输入报告。

大神形象解释:你将要发送/接收的指令或数据,当endpoint为OUT,buffer为你定义好的指令或数据,将下发给device,当endpoint为IN,buffer则是一个容器,用来存储device返回的应答指令或数据,此时一定要注意buffer的大小,以足够存储所有的数据;

(3)length: 定义输入或则输出端点的报告的长度
这个长度,可以在HID设备端点描述符中,查看到如下图,我调试的HID设备的输入和输出端点描述符:


(4)timeout:: 超时时间

因此,针对上面的解析,以下是我调试设备配置的参数:偷笑

A、Host 主机发送输出报告
int outLength = mConnection.bulkTransfer(mOutEndpoint, outputStream, 64, timeOut);

B、Host主机获取输入报告
int recLength = mConnection.bulkTransfer(mInEndpoint, inputStream, 64, timeOut);


以上就是Android USB Host 通讯中,中断传输使用的bulkTransfer() 方法的参数解析。大笑这个相比于之前控制传输的controlTransfer() 参数还是简单些,而且也很容易理解。

下一篇,将记录我如何调试两种HID设备。得意