android - 如何判断USB Host设备类型?

标签 android xamarin xamarin.android

我正在尝试使用 this guide at android.developers 读取 USB 大容量闪存驱动器的内容。 .我无法确定哪个驱动器实际上是闪存驱动器。

我正在使用此代码遍历设备:

    protected override void OnResume()
    {
        usbManager = (UsbManager)this.GetSystemService(Context.UsbService);
        monitorUsb();
    }

    private async void monitorUsb()
    {
        var deviceList = usbManager.DeviceList;
        foreach (var device in deviceList.Values)
        {
            Debug.WriteLine("USB",device.DeviceName);
            Debug.WriteLine("USB",device.DeviceProtocol.ToString());
        }
        await Task.Delay(30000);
        monitorUsb();
    }

我插入了两个设备,一个鼠标和一个闪存驱动器。

上面的代码产生以下结果:

[0:] /dev/bus/usb/003/003
[0:] 0
[0:] /dev/bus/usb/003/019
[0:] 0
[0:] /dev/bus/usb/003/020
[0:] 0

通过排除,020是U盘,019是鼠标。我不知道 003 是什么,也许是内部端口或其他端口之一(我通过板载以太网连接了 adb)。

根据 this documentationUSB_CLASS_MASS_STORAGE协议(protocol)为80USB_CLASS_PER_INTERFACE

相关

如果它们都返回 USB_CLASS_PER_INTERFACE,我如何在代码中确定哪个设备是闪存驱动器?我查看了 UsbDevice 的其他属性,但似乎没有一个返回对我有用的任何内容。

最佳答案

我找错协议(protocol)了。

        foreach (var device in deviceList.Values)
        {
            for (int i = 0; i < device.InterfaceCount; i++)
            {
                var deviceInterface = device.GetInterface(i) as UsbInterface;
                Debug.WriteLine("USB", deviceInterface.InterfaceProtocol.ToString());
            };

            //Debug.WriteLine("USB", device.DeviceName);
            //Debug.WriteLine("USB", device.DeviceProtocol.ToString());
        }

这会将协议(protocol)返回为 80 而不是文档建议的 8。我无法解释这一点。

关于android - 如何判断USB Host设备类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38524860/

相关文章:

Android - 如何在图库中保存照片

android - 如何从MySQL访问android应用程序中的数据并在离线模式下显示?

c# - 汉堡菜单 Prism xamarin形式?

android - Xamarin Android SDK 位置

c# - Azure 移动应用程序参与 Xamarin 身份验证

android - 在 android 中找不到 SupportMapFragment 类错误

java - 应用程序关闭并出现此错误 - 请求带有表名的列名

c# - Xamarin.Forms 设置 IsVisible 在函数完成之前不会触发。

android - Xamarin + Android + Binding YouTube视频播放器编译报错

android - 像 iphone 的通用图像加载器这样的库-(在 monotouch 和 monodroid 中使用 android 库)