I know how to do following:
- listen to attach and detach events of usb devices
- getting all attached devices
- getting permissions for a device
So in the end I have an UsbDevice
and I have permissions to read/write it. How to go on from here?
I can open the device and get a FileDescriptor
like following:
UsbManager manager = (UsbManager) activity.getSystemService(Context.USB_SERVICE);
UsbInterface intf = device.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);
UsbDeviceConnection connection = manager.openDevice(device);
boolean interfaceClaimed = connection.claimInterface(intf, true);
int fileDescriptor = connection.getFileDescriptor();
How can I work with this FileDescriptor
now? Or how else can I access the files on the USB device?
EDIT
Solution for android < 6: Don't open a
UsbDeviceConnection
and just try to find the usb devices path. Still, then you have the problem to distinguish between multiple usb devices and don't know which path belongs to which device...Solution for android >= 6: Use the Storage Access Framework, actually I don't know how this works yet...