Android Usb Host API and Usb Storage

Android usb host api and USB storage

The Android USB Host APIs do not include USB Mass Storage filesystem code, nor as of this writing in 2014 will "stock" Android mount a USB Mass Storage volume at operating system level.

To access a USB Mass Storage device using the stock Android USB Host APIs, you must therefore implement in your Application code both the necessary raw-USB operations to achieve block-level device access, and the appropriate filesystem logic itself. Needless to say, the details of such are of a complexity beyond the scope of an answer here, but you could start by studying documentation or existing implementations of USB Mass Storage drivers and filesystem drivers for other platforms.


It appears the situation in Android 6 may be different, and access via the USB host apis to something that version recognizes as a storage device might even no longer be permitted. Those targeting Android 6+ may need to look elsewhere, but older devices will remain in use for some time.

File I/O on a mounted USB storage device in USB Host mode (Android 3.1 and up)

Your connection is set up, the end points are basically flags on the device with information on data transfer.

For your stick you need to do something like VV to figure out how many endpoints you have,

UsbInterface intf = device.getInterface(0);
// checks for 2 endpoints
if (intf.getEndpointCount() != 2) {
Toast toast = Toast.makeText(context, "could not find endpoint", duration);
toast.show();
return;
}
UsbEndpoint ep = intf.getEndpoint(0);
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {

this will let you figure out if the endpoint you're interested in is a bulk (usb constants docs has other types) and if you can send data to or from the device at that endpoint (usb_dir_in to test for in). What endpoint you want is device specific, my example starts on 0, yours will be different

To resave the file you need to do something like

mConnection.bulkTransfer(mEndpointOut, bytes, 512, TIMEOUT);

I have been saving the buffer each time it fills with a file output stream, this is probably inefficient (as I assume bulktransfer is already saving somewhere) but documentation is scarce.

Send data from android to connected Usb storage device in Usb Host Mode

After going with all suggested ideas I have found the solution which works as accepted. Use UsbHost Api to get the device related information and then
use Storage Access Framework to perform the filing operation.

Using Android phone as USB Host to mount external drives to phone and accessing the memory of the external drive through phone

The only sure way of getting this done is to use API level above 12, otherwise a few phones may have support for usb host but most of them wont support it.
The reason being first of all you need hardware support for usb host, even if that is present the drivers needed might not be compiled into the kernel, i did some work while trying to implement usb host on nook color, even though it had hardware support, getting usb host working took almost 2 months and a dedicated app. So its not something you will be able to do for every device. A few might support it out of the box but even those would need root and lots more work for mounting drives and all that..
the DSLR camera also does it the same way look at the requirements

- Android device with ARMv7-A or newer CPU architecture (most 1ghz+ devices)
- Not rooted: Android 3.1 or higher with USB host kernel+API support
- Rooted: Android 2.3.1 or higher with USB host kernel support

These are the only devices that can support USB host.

Browsing a removable USB OTG on Android N, is Storage Access Framework really the only way?

Accessing removable storage data can be done without SAF in Android M and up. You just need to do low-level-ish coding. As illustrated by this project.
We in our project might opt for implementing something similar to this linked project since SAF is proving to be unreliable as of this time.

The project also includes an implementation of SAF but with the low-level USB reading library as a base.

How to access the filesystem of an USB Stick by Android SDK?

I have found a "dirty" solution. I just access "/storage/usbdisk" with standard Java File-I/O. This works on two Devices. On a Motorola Moto G and on a Sony xPeria Z1. But I don't know if every Android 4.x Device mounts USB sticks to "/storage/usbdisk". Maybe someone knows? My App has to run on an Android 4.x Tablet.

And still I'd like to make my app recognize that an USB device was plugged/ unplugged. But the Broadcast Receiver in my posted code doesn't work. How to fix that?



Related Topics



Leave a reply



Submit