我正在尝试制作用于读取使用 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 吗?谢谢!