Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android USBHost flash drive

Tags:

android

usb

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!

like image 920
Alexandr Avatar asked Apr 17 '12 10:04

Alexandr


People also ask

Why is my phone not detecting flash drive?

Try following methods. Go to Settings> Storage> More (three dots menu)> USB computer connection, choose Media device (MTP). For Android 6.0, go to Settings> About phone (> Software info), tap “Build number” 7-10 times. Back to Settings> Developer options, check “Select USB Configuration”, choose MTP.


1 Answers

It appears you are missing a whole protocol layer; you can't just read 512 bytes from the device. What should it send back? How could it know you want to start to read at the beginning of the disk?

How you actually read a memory location from USB-MSC device depends on the device sub class type and the supported transport protocol.

It is likely that an ordinary flash disk uses the SCSI transparent command set in conjunction with the USB Mass Storage Class Bulk-Only (BBB) Transport.

You have to examine the device descriptor to find out what your device supports. See also the MSC device class overview for all possible values and references to their documentation.

like image 51
Alexander Avatar answered Sep 27 '22 20:09

Alexander