简体   繁体   中英

Transfer Files USB Mass Storage OTG

I have an external mass storage device connected to an Android device. There are several .BIN files in the root directory that I need to read into my app. I am able to connect to the device and receive usb permission using UsbDeviceConnection.

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
    String output = outputTV.getText().toString();
    String action = intent.getAction();
    outputTV.setText(outputTV.getText() + "\n" + " ACTION: " + action);

    if (ACTION_USB_PERMISSION.equals(action)) {
        synchronized (this) {
            UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

            if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                if (device != null) {
                    if (device.getInterfaceCount() > 0) {
                        usbIntf = device.getInterface(0);
                        usbEndIn = usbIntf.getEndpoint(0);
                        usbEndOut = usbIntf.getEndpoint(1);

                        usbConnection = mUsbManager.openDevice(device);
                        usbConnection.claimInterface(usbIntf, true);

                        byte[] buffer = new byte[usbEndIn.getMaxPacketSize()];
                        StringBuilder str = new StringBuilder();
                        int retVal = usbConnection.bulkTransfer(usbEndIn, buffer, usbEndIn.getMaxPacketSize(), 2000);

                        for (int i = 2; i < buffer.length; i++) {
                            if (buffer[i] != 0) {
                                str.append((char) buffer[i]);
                            }}}}}}}}};

Once I'm connected I am able to see that I have one Mass Storage interface with 2 Bulk Transfer endpoints (one in, one out).

When using usbConnection.bulkTransfer I receive -1 back and an empty buffer. So, I'm having trouble receiving data. Once past that hurdle, what will the data look like? How do I continue to pull all of the files from the device? Is there a way to transfer the data file by file to my app? Is there an all together better way of approaching this?

Well, I ended up going a totally different route with this. I am now using standard java.io.File and working my way through the directory structure. My code is not yet refined, but here is the gist of it...

private void setupFileIO() {
    File storageDir = new File("/storage");
    File files[] = storageDir.listFiles();

    File directory = null;
    if (files != null && files.length > 0) {
        for (File f : files) {
            if (f.isDirectory() && f.getPath().toLowerCase().contains("usb") && f.canRead()) {
                directory = f;
                break;
            }
        }
    }
    if (directory != null && directory.canRead()) {
        File file[] = directory.listFiles();
        if (file != null && file.length > 0) {
            for (int i = 0; i < file.length; i++) {
                String filePath = file[i].getPath();
                String extension = Files.getFileExtension(filePath);
                if (file[i].isFile() && file[i].canRead() && extension.equalsIgnoreCase("bin")) {
                    try {
                        byte[] r = Files.toByteArray(file[i]);
                        String result = BaseEncoding.base16().encode(r);
                        // do something with result
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM