USB Host 模式下已安装的 USB 存储设备上的文件 I/O(Android 3.1 及更高版本)

发布于 2024-12-03 19:10:48 字数 2200 浏览 0 评论 0原文

好的,我有一台 Android 3.1 平板电脑(Acer Iconia Tab,顺便说一下,它很棒),我可以将其与 Android USB API 一起使用来连接 USB 大容量存储设备(一个简单的 USB 记忆棒)。

我使用 USB 主机模式,找到设备,获得连接到它的权限(使用 BroadcastReceiver)。一切都很好。问题是我不知道到底该怎么做才能将文件从外部存储目录复制到 USB 记忆棒。

这是我到目前为止所拥有的:

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 Dev Usb Host 上的文档 但它不是很明确,我发现了一个非常好的教程 Android USB 主机教程 - AdbTest 但它使用异步通信。

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

任何指向更明确文档的提示或指针将不胜感激。

谢谢

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

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.

This is what I have so far:

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

I read the documentation on the Android Dev Usb Host but it's not very explicit and I found a pretty good tutorial Android Usb Host Tutorial - AdbTest but it uses asynchronous communication.

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.

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

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗弃M 2024-12-10 19:10:48

Android USB 主机 API 仅提供原始 USB 访问。要访问存储设备上的文件,您的应用程序本身必须在 USB API 之上实现 USB 海量存储模式,然后在其之上实现文件系统的代码。

一些供应商定制的 Android 版本将在操作系统级别安装具有可识别文件系统的 USB 大容量存储设备,但这目前不是标准 Android 的一部分。如果您有 root 设备,您也可以使用它来说服内核挂载这样的文件系统。

The Android USB host APIs provide only raw USB access. To access files on a memory device, your app must itself implement USB Mass Storage Mode on top of the USB Apis, and then the code of a filesystem on top of that.

A few vendor-customized versions of Android will mount a USB mass storage device with a recognized file system at operation system level, but that is not currently part of standard android. It is also possible that if you have a rooted device you may be able to use that to convince the kernel to mount such a filesystem.

冷默言语 2024-12-10 19:10:48

您的连接已建立,端点基本上是设备上带有数据传输信息的标志。

对于你的棒,你需要做类似 VV 的事情来弄清楚你有多少个端点,

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 用于测试 in)。您想要的端点是特定于设备的,我的示例从 0 开始,您的示例将有所不同

要重新保存文件,您需要执行类似

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

我每次填充文件输出流时都保存缓冲区的操作,这可能效率低下(因为我认为批量传输已经保存在某个地方)但文档很少。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文