2012-04-17 66 views
5

我正在尝试使用OTG电缆连接到使用ICS的XOOM读取外部存储文件系统的应用程序。 我使用此代码来确定通信IN和OUT端点与闪存设备android USBHost闪存驱动器

final UsbDeviceConnection connection = manager.openDevice(device); 
UsbInterface inf = device.getInterface(0); 
if (!connection.claimInterface(inf, true)) { 
    Log.v("USB", "failed to claim interface"); 
} 
UsbEndpoint epOut = null; 
UsbEndpoint epIn = null; 
// look for our bulk endpoints 
for (int i = 0; i < inf.getEndpointCount(); i++) { 
    UsbEndpoint ep = inf.getEndpoint(i); 
    if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { 
     if (ep.getDirection() == UsbConstants.USB_DIR_OUT) { 
      epOut = ep; 
     } else { 
      epIn = ep; 
     } 
    } 
} 
if (epOut == null || epIn == null) { 
    throw new IllegalArgumentException("not all endpoints found"); 
} 
final UsbEndpoint inEndPoint = epIn; 

它工作正常。 然后我想读前512个字节得到FAT32引导扇区

ByteBuffer arg1 = ByteBuffer.allocate(512); 
UsbRequest request = new UsbRequest(); 
request.initialize(connection, inEndPoint); 
request.queue(arg1, inEndPoint.getMaxPacketSize()); 
UsbRequest result = connection.requestWait(); // halt here 
connection.releaseInterface(inf); 
connection.close(); 

,但它并没有读取连接设备的任何数据。 所有这些代码在单独的线程运行在广播接收机设备上

PendingIntent mPermissionIntent = PendingIntent.getBroadcast(USBHostSampleActivity.this, 0, new Intent(ACTION_USB_PERMISSION), 0); 
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); 
registerReceiver(mUsbReceiver, filter); 
manager.requestPermission(lDevices.get(position),mPermissionIntent); 

挤级权限后,我刚开始与以前的代码新的线程; 我也试图拨打电话到 USBDeviceConnection.controlTransfer

byte[] b = new byte[0x10]; 
int cTransfer = connection.controlTransfer(128, 6, 16, 0,b, 12, 0); 

像libusb的样本中得到F0的数据和/或hwstats但它总是返回-1 还我试着用USBRequst同步bulkTransfers取代异步请求,但结果是一样。 有没有人使用过这部分Android SDK? 谢谢!

+0

我正在尝试做类似的事情 - 将USB HID设备连接到运行ICS的Xoom。在做了一些研究之后,我知道美国发货的Xooms在大多数情况下对USB Host API没有任何问题,但任何其他(例如欧盟或澳大利亚出货的)设备都以不允许制作控制转移。我试图用不同的图像来刷我的设备,当我跑完时我会通知您。你昨天找到有用的东西吗? – syntagma 2012-04-18 13:39:06

+0

是的,我几乎完成了使用usbhost api实现fat32解析。我可以告诉你,你根本不需要使用控制转移。所有你需要的是bulk_transfer和HID官方文档 – Alexandr 2012-04-20 20:06:54

回答

1

我写了一个库调用libaums:https://github.com/mjdev/libaums

该库负责低级别的USB通信,并实现了FAT32文件系统。它还包含一个示例应用程序。列出目录的工作原理如下:

UsbMassStorageDevice[] devices = UsbMassStorageDevice.getMassStorageDevices(this); 
device.init(); 

// we only use the first device 
device = devices[0]; 
// we always use the first partition of the device 
FileSystem fs = device.getPartitions().get(0).getFileSystem(); 
Log.d(TAG, "Capacity: " + fs.getCapacity()); 
Log.d(TAG, "Occupied Space: " + fs.getOccupiedSpace()); 
Log.d(TAG, "Free Space: " + fs.getFreeSpace()); 
UsbFile root = fs.getRootDirectory(); 

for(UsbFile f : root.listFiles()) 
    Log.d(TAG, f.getName()) 
+0

哪一个是示例应用程序?它是否是usbfileman?我试图运行应用程序,但应用程序被迫关闭。 – doraemon 2016-04-20 08:50:25

+1

是usbfileman是示例应用程序。检查权限和设置(例如,应用程序未使用新的Android运行时许可证获取)。如果你有错误日志,你也可以在github上提交问题。 – Magnus 2016-04-22 11:33:28

4

看来你缺少一个完整的协议层;你不能从设备读取512个字节。它应该发回什么?它如何知道你想在磁盘的开始处开始阅读?

实际从USB-MSC设备读取存储单元的方式取决于设备子类类型和支持的传输协议。

普通的闪存盘可能会使用SCSI transparent command setUSB Mass Storage Class Bulk-Only (BBB) Transport一起使用。

您必须检查设备描述符以找出设备支持的内容。请参阅MSC device class overview了解所有可能的值和对其文档的参考。

+0

嘿亚历山大,我也试图在类似的应用程序。你能更新你的链接吗?他们现在似乎已经死了。 – giannileuani 2014-10-07 18:07:00

+0

@ user1949634感谢您的提示。链接已更新。 – Alexander 2014-10-10 20:27:29