1

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?

4

1 回答 1

2

好吧,我最终走了一条完全不同的路线。我现在使用标准的 java.io.File 并通过目录结构工作。我的代码还没有完善,但这里是它的要点......

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();
                    }
                }
            }
        }
    }
}
于 2015-07-30T19:33:10.623 回答