android USBHost flash drive

android, usb

Solution

I wrote a library for that called libaums: https://github.com/mjdev/libaums

The library takes care of low level USB communication and implements the FAT32 file system. It also includes an example app. Listing a directory would work as follows:

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())

Problem

I am trying to make application for reading external storage file system connected using OTG cable to XOOM with ICS. i am using this code to determine IN and OUT endpoint for communication with flash device ``` 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; ``` it works normal. then i am trying to read first 512 bytes to get FAT32 boot sector ``` 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(); ``` but it does not read any data from connected device. all this code run on separate thread after granding permission on device ``` 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); ``` in Broadcast receiver i just start new thread with previous code; i also tried to make call to USBDeviceConnection.controlTransfer ``` byte[] b = new byte[0x10]; int cTransfer = connection.controlTransfer(128, 6, 16, 0,b, 12, 0); ``` like in libusb sample to get f0 data and/or hwstats but it always return -1 also i tried replace async request using USBRequst to sync bulkTransfers but result is the same. Have anyone worked with this part of Android SDK? Thanks!

Original source