android USBHost 闪存驱动器

标签 android usb

我正在尝试申请读取外部存储文件系统,该文件系统使用 OTG 电缆连接到带有 ICS 的 XOOM。 我正在使用此代码来确定与闪存设备通信的 IN 和 OUT 端点

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 吗? 谢谢!

最佳答案

看来您缺少整个协议(protocol)层;您不能只从设备读取 512 个字节。它应该送回什么?它怎么知道你要从磁盘的开头开始读取呢?

您实际如何从 USB-MSC 设备读取内存位置取决于设备子类类型和支持的传输协议(protocol)。

普通闪存盘很可能使用SCSI transparent command set结合USB Mass Storage Class Bulk-Only (BBB) Transport .

您必须检查设备描述符以了解您的设备支持什么。另见 MSC device class overview所有可能的值和对其文档的引用。

关于android USBHost 闪存驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10189118/

相关文章:

android - Proguard从Gradle插件2.1.2到2.3.3输出了不同的混淆结果

android - USB 系绳和调试同时进行

c++ - Windows Phone 8 USB 通信

C# COM 端口通信 - USB 转串行适配器出现错误

android - 关闭 USB 设备连接

android - Google Maps Android API v2 SupportMapFragment 内存泄漏

android - 未定义 chrome ARC-Welder javascript 控制台插件

java - 使用 Intent 将参数传递给静态构造函数

usb - 为什么 USB CDC 设备需要 Windows 中的主机驱动程序?

windows - Kinect USB 3.0 经常重连