文件我在USB主机模式下安装USB存储设备上的输入/输出(安卓3.1及以上)我在、存储设备、主机、文件

2023-09-04 23:39:41 作者:哭过的眼睛看世界更清楚

好了,我有一款Android 3.1平板电脑(宏碁Iconia Tab键,这是伟大的方式),我可以使用与Android USB API来使用USB大容量存储设备(一个简单的USB记忆棒)连接。

Ok, so I have a Android 3.1 tablet (Acer Iconia Tab, which is great by the way) which I can use with Android USB API to connect with a USB Mass Storage Device (a simple USB memory stick).

我用USB主机模式,找到该设备,获得许可(使用BroadcastReceiver的)连接到它。所有的伟大工程。问题是,我不知道到底该怎么做,才能从外部存储目录的文件复制到U盘。

I use USB Host mode, find the device, get permission to connect to it (using BroadcastReceiver). All works great. The problem is that I don't know exactly what to do in order to copy a file from the External Storage Directory to the USB memory stick.

这是我到目前为止有:

final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        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) {

                    // Got to a point where I should set up connection
                    // I'm setting up a very simple connection, just want some file transfer

                    UsbInterface intf = device.getInterface(0);
                    UsbEndpoint endpoint = intf.getEndpoint(0);
                    UsbDeviceConnection connection = UsbManager.openDevice(device);

                    if (connection.claimInterface(intf, true)) {
                        UtilsAndDialogs.print(getApplicationContext(), "Connected to device");

                        // Copy file to USB...

                    } else
                        UtilsAndDialogs.print(getApplicationContext(), "Could not connect!");
                }
            } else {
                UtilsAndDialogs.print(getApplicationContext(), "Permission denied");
                Log.d(UtilsAndDialogs.LOG_TAG, "Permission denied for device " + device);
            }
        }
    } 
};

我读了Android开发(http://developer.android.com/guide/topics/usb/host.html)的文件,但它不是很明确,我发现了一个pretty的很好的教程(http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/11289-android-usb-host-tutorial-adbtest.html)但它使用异步通信。

I read the documentation on the Android Dev (http://developer.android.com/guide/topics/usb/host.html) but it's not very explicit and I found a pretty good tutorial (http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/11289-android-usb-host-tutorial-adbtest.html) but it uses asynchronous communication.

我只是想知道我应该怎么设置连接和使用的端点(我没有得到端点的一部分,为什么需要他们)只是能够创建USB存储设备上的一个新的文件,并复制不同的文件的内容在里面,可能使用bulkTransfer()方法。

I just want to know how should I set up the connection and use the endpoint (I didn't get the endpoint part, why they are needed) to just be able to create a new file on the USB storage device and copy contents of a different file in there, probably using bulkTransfer() method.

任何提示或指针,以更为明确的文件将大大AP preciated。

Any hints or pointers to more explicit documentation would be greatly appreciated.

感谢您

推荐答案

您建立连接,终点基本上都是设备上的数据传送信息的标志。

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

有关你的坚持,你需要做的是这样VV找出你有多少终端都有,

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) {

这会让你计算出,如果你有兴趣的终点是散装(USB常数文档具有其他类型的),如果你可以发送数据或从该设备在该端点(usb_dir_in测试中)。你想要的终点是特定的设备,我的例子开始于0,你的将是不同的。

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);

我一直在每次用一个文件输出流填充节省时间缓冲,这可能是低效的(因为我假设bulktransfer已经节省的地方),但文件是稀缺的。

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.