简体   繁体   English

Android USB OTG阅读Usb Drive

[英]Android USB OTG Read Usb Drive

I am currently working on a project which requires Usb Host mode in Android (using 4.2.2). 我目前正在开发一个需要在Android中使用Usb主机模式的项目(使用4.2.2)。 I need to be able to do standard file operations like listing files and reading from a usb drive. 我需要能够执行标准文件操作,如列出文件和从USB驱动器读取。 Using the host mode documentation I have gotten to the point of being able to detect the drive is connected, is USB mass storage, and find the various endpoints. 使用主机模式文档我已经到了能够检测到驱动器已连接的程度,是USB大容量存储,并找到各种端点。

Where I get stuck is what to do after that. 我被卡住的地方就是在那之后要做什么。 I guess I don't really understand the difference between control and bulk transfers or in the case of control transfers what is really even happening. 我想我并不真正了解控制和批量转移之间的区别,或者在控制转移的情况下,实际上甚至发生了什么。 Unfortunately my knowledge of USB itself is somewhat limited and I hear the documentation is rather daunting. 不幸的是,我对USB本身的了解有限,我听说文档相当令人生畏。

Can anyone help guide me through, or point me towards a working example for, what I would need to do to essentially "mount" the usb drive? 任何人都可以帮助指导我,或指出我的工作示例,我需要做什么来基本上“装载”USB驱动器? As I said previously I just need those file operations of listing files and reading them, not writing. 正如我之前所说,我只需要列出文件并读取它们的文件操作,而不是写入。 I should also mention that the target phone is the Galaxy Nexus by Samsung. 我还要提到目标手机是三星的Galaxy Nexus。

Thanks 谢谢

a control endpoint is (usually) used to send commands to usb device. 控制端点(通常)用于向USB设备发送命令。
a bulk endpoint is (usually) used to transfer data to usb device. 批量端点(通常)用于将数据传输到USB设备。

in bulk the structure of the data is not defined. 批量处理数据的结构未定义。
but in control transfer the structure of data is defined. 但在控制转移中,定义了数据结构。
the host send a 8byte packet containing the information about the command (like request ID, command parameters, length of data to transfer in data stage, etc...). 主机发送一个8字节的数据包,其中包含有关命令的信息(如请求ID,命令参数,在数据阶段传输的数据长度等)。 and then optionally some data is received/send and finally both the host/device agrees on success of command by moving to status stage. 然后可选地接收/发送一些数据,最后主机/设备通过移动到状态阶段同意命令成功。 thats it! 而已!
but, devils is in the details, see usb specification. 但是,魔鬼在细节中,请参阅usb规范。 :) :)


all usb memories (pendrives, memory card, hard drives) work on the usb mass storage class. 所有usb存储器(pendrives,存储卡,硬盘驱动器)都可用于usb海量存储类。

using the mass storage class, you can perform memory read-write but the filesystem (ex: EXT3, FAT...) on top of it need another driver. 使用大容量存储类,您可以执行内存读写,但文件系统(例如:EXT3,FAT ...)需要另一个驱动程序。

so, essentially to read/write a file. 所以,基本上是读/写文件。 you need to connect to the device: 你需要连接到设备:

  1. connect to the device (you have done it) 连接到设备(你已经完成)
  2. identify and take control over the mass storage interface 识别并控制大容量存储接口
  3. write a mass storage class driver 写一个大容量存储类驱动程序
  4. write filesystem driver that will read/write files using mass storage class driver 编写将使用大容量存储类驱动程序读/写文件的文件系统驱动程序
  5. use the filesystem driver api in your project. 在项目中使用文件系统驱动程序api。

to write a mass storage class driver, you have to read the USB Mass storage clas specification http://www.usb.org/developers/docs/devclass_docs/ and probebly you are looking for the Bulk only 要编写一个大容量存储类驱动程序,你必须阅读USB Mass storage clas规范http://www.usb.org/developers/docs/devclass_docs/并且你只是在寻找Bulk

and then, refer the filesystem(s) you are targetting for there design and specification. 然后,请参考您要针对的文件系统进行设计和规范。 (if available) (如果可供使用的话)


AFAIK, Android has filesystem mounting facility connected to USB, so you are probebly reinventing the wheel. AFAIK,Android具有连接到USB的文件系统安装设备,因此您可以重新发明轮子。


extra-note: 特注:
some filesystem are not openly documented (though reverse engg information probely exists) (ex: NTFS) 某些文件系统没有公开记录(尽管存在反向引用信息)(例如:NTFS)
and some filesystem are (partly) patented. 一些文件系统(部分)获得专利。 (ex: FAT) (例如:FAT)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM