Android的开发刍议USB传输 [英] Android Devlopment Usb Transfer

查看:225
本文介绍了Android的开发刍议USB传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的Andr​​oid的发展,我最近一直在探索的USB主机。

I am fairly new to Android Development and i have recently been exploring Usb Host.

会有人能告诉我怎么用批量传输,这样我可以看到一个外接摄像头看到的东西,而是表明它在我的平板电脑?

Would anyone be able to tell me how to use Bulk Transfer so that i can see what an external camera sees but instead show it on my tablet?

相机:佳能PowerShot A1300 平板电脑:的Iconia A200

Camera : Canon Powershot A1300 Tablet : Iconia A200

我环顾四周堆栈溢出和其他一些论坛,但至今尚未能找到关于如何使用批量传输或者是常数作为参数,用于检索某些数据很好的解释。

I have looked around stack overflow and some other forums but have not yet been able to find a good explanation on how to use Bulk Transfer or what constants to use as parameters for retrieving certain data.

我能看到终点,并建立与外部摄像头的连接,但我不知道何去何从。

I am able to see the endpoints and set up a connection with the external camera but I do not know where to go from here.

任何帮助深表AP preciated。

Any help is deeply appreciated.

推荐答案

在Android上的USB主机API是相当薄,我的意思是,一旦你已经超越枚举接口/端点和创建连接它不会做更加为您提供帮助。它的格式,您然后与原始USB的数据传输通信的境界,取决于设备类相机重新presents。您的要求是有点忌讳的,所以我会尽我所能,提供有用的资源。

The USB Host APIs in Android are fairly thin, by which I mean once you have gone beyond enumerating the interfaces/endpoints and creating a connection it doesn't do much more to assist you. You are then in the realm of communicating with raw USB data transfers, the format of which depend on the device class your camera represents. Your request is somewhat a can of worms, so I will do my best to provide helpful resources.

不幸的是,存储和媒体设备不是简单的设备类别间preT,所以它可能是困难的,如果你只是让你的脚湿的USB一般。我能给出的最好的建议是看看设备类规范的接口类相机的报告(大部分要么是大容量存储设备或MTP),它可以在这里找到:<一href="http://www.usb.org/developers/devclass_docs">http://www.usb.org/developers/devclass_docs

Unfortunately, storage and media devices are not the simplest device classes to interpret, so it may be difficult if you are just getting your feet wet on USB in general. The best advice I can give is to take a look at the device class specs for the interface class your camera reports (most are either Mass Storage or MTP), which can be found here: http://www.usb.org/developers/devclass_docs

该规范文件将枚举你需要使用的设备进行通信的命令。我也建议检查出USB简单地说,它确实指出了USB请求的方式一般构成了伟大的工作,它可以帮助你绘制你在一个规范文档在<$的方法发现的参数见C $ C> UsbDeviceConnection :<一href="http://www.beyondlogic.org/usbnutshell/usb1.shtml">http://www.beyondlogic.org/usbnutshell/usb1.shtml

The spec document will enumerate the commands you need to use to communicate with the device. I would also recommend checking out USB In a Nutshell, which does a great job of pointing out how USB requests are constructed in general, which can help you map what you see in a the spec docs to the parameters found in the methods of UsbDeviceConnection: http://www.beyondlogic.org/usbnutshell/usb1.shtml

有可能会是极少数的控制命令,你需要发送到端点0初步设置摄像机,然后将剩余转让将有可能发生在批量端点。

There will likely be a handful of control commands you need to send to "endpoint 0" initially to set up the camera, and then the remaining transfers will likely take place over the bulk endpoints.

在安卓方面,控制请求只能使用同步发送 UsbDeviceConnection.controlTransfer(),这意味着这种方法阻止,直到传输完成。这填补了此方法的参数被发现的规范文档为您的设备类。

In Android terms, control requests can only be sent synchronously using UsbDeviceConnection.controlTransfer(), meaning this method blocks until the transfer is complete. The parameters that fill in this method are found in the spec docs for your device class.

在批量端点请求可以同步通过 UsbDeviceConnection.bulkTransfer()或异步使用 UsbRequest 实例发送。随着 UsbRequest 你可以排队一个转移,然后再回来检查(通过 UsbDeviceConnection.requestWait())的结果。

Requests on bulk endpoints can be sent synchronously via UsbDeviceConnection.bulkTransfer() OR asynchronously using a UsbRequest instance. With UsbRequest you can queue a transfer and then later check back (via UsbDeviceConnection.requestWait()) for the results.

我在使用主机的API做一些基本的中断,并控制转移到得到这样的设备描述符的信息在我的Github上页的一些例子。也许一些,这将是对您有所帮助,以及:<一href="https://github.com/devunwired/accessory-samples">https://github.com/devunwired/accessory-samples

I have some examples on my Github page in using the host APIs to do some basic interrupt and control transfers to get information like device descriptors. Perhaps some of that will be helpful to you as well: https://github.com/devunwired/accessory-samples

至于您对USB例如code问题:

With regards to your question about the USB example code:

在此code提出的要求只是一个通用的获取配置描述符的要求,所有的USB设备必须响应(这是一个核心的命令,不特定类)。事实上,它在那里了Android的API让你可以为接口和终端查询信息的请求。该字段值来自于核心的USB规范(该指令明确定义在3.0规范第9.4.3和9.6.3)的 http://www.usb.org/developers/docs/ 或更有益的描述,你可以从USB概括地说,其中有一个多一点的讨论发现:<一href="http://www.beyondlogic.org/usbnutshell/usb5.shtml#ConfigurationDescriptors">http://www.beyondlogic.org/usbnutshell/usb5.shtml#ConfigurationDescriptors

The request made in this code is just a generic "Get Configuration Descriptor" request that all USB devices must respond to (it's a core command, not class-specific). In fact, its the request where the Android APIs get the information you can query for interfaces and endpoints. The field values come from the Core USB Specification (this command specifically is defined at section 9.4.3 and 9.6.3 in the 3.0 spec): http://www.usb.org/developers/docs/ or a more helpful description you can find from USB in a Nutshell, which has a little more discussion: http://www.beyondlogic.org/usbnutshell/usb5.shtml#ConfigurationDescriptors

的长度是有点武断,这告诉司机多少字节读取或写入。大多数USB主机驱动程序将首先查询设备描述符,它包括一个场告诉主机的最大分组大小的设备支持,然后将使用该尺寸作为用于将来的请求的长度。一个全功能的驱动程序很可能会令该命令,然后第一个(描述符的wTotalLength场)检查长度的字节,看是否缓冲区足够大,并修改/如果不重新发送。在这个例子中,我只是选择了64的简单,因为这是最大的最大数据包大小的协议定义为支持的。

The length is somewhat arbitrary, this tells the driver how many bytes to read or write. Most USB host drivers will first query the device descriptor, which includes a field telling the host the Max Packet Size the device supports, and then will use that size as the length for future requests. A full-featured driver would probably make this command and then check the length bytes first (the wTotalLength field of the descriptor) to see if the buffer was large enough, and modify/resend if not. In the example, I just chose 64 for simplicity because that is the "maximum" Max Packet Size the protocol defines as supportable.

此外,进而实现具体的数据设备所提供的要求,这些命令将在特定的类文件中找到,而不是核心规范。

Again, then making requests of the specific data your device has to offer, those commands will be found in the specific class document, not the core specification.

这篇关于Android的开发刍议USB传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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