【问题标题】:How to grant permission to open usb device with usb manager? openDevice() always returns null如何授予使用 USB 管理器打开 USB 设备的权限? openDevice() 总是返回 null
【发布时间】:2017-09-13 00:44:37
【问题描述】:

我想在以下代码中使用 USB 设备。它成功地列出了 USB 设备并对其进行了迭代。在以下代码中,对象“设备”是我需要打开的 USB 设备。除了总是返回 null 值的 OpenDevice() 方法之外,一切似乎都正常!

[Activity(Label = "TestApp", MainLauncher = true, Icon = "@drawable/icon")]
[IntentFilter(new[] {UsbManager.ActionUsbDeviceAttached})]
[MetaData(UsbManager.ActionUsbDeviceAttached, Resource = "@xml/device_filter")]

public class MainActivity : Activity
{
    int count = 1;
   {
       base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        UsbManager manager = (UsbManager)GetSystemService(Context.UsbService);
        UsbDevice device = null;
        foreach (var dev in manager.DeviceList)
        {
            if (dev.Value.VendorId == 5401)
            {
                device = dev.Value;
            }
        }
        var connection = manager.OpenDevice(device);
        // Read some data! Most have just one port (port 0).
    }

device_filter.xml 包含以下几行:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
   <usb-device product-id="8704" vendor-id="5401" />
</resources>

当我尝试 bool hasPermision = manager.HasPermission(device);我看到 hasPermission 是假的。谁能告诉我如何授予在 xamarin 中打开 USB 设备的权限? 感谢您的帮助。

【问题讨论】:

    标签: android xamarin xamarin.android android-usb


    【解决方案1】:

    在尝试了几个建议后,我终于修复了它。我发现在清单中添加意图过滤器并不能解决权限问题,原因不明。这听起来像 Xamarin 的错误。说实话,Xamarin usb 命名空间似乎太幼稚了,最好不要浪费时间将其用于USB管理和连接。它还有一些其他恼人的限制。例如查看here。 (所以我建议用java写低级usb通信代码,用JNI在xamarin中导入jar文件,累了应该说比第一次容易多了)

    要授予打开USB设备的权限,您必须使用grantPermission()方法。下面的代码展示了如何使用方法和BroadcastReceiver来弹出一个对话框,要求用户让usb使用。

     public class MainActivity : Activity
    {
        private static String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
        UsbDevice device; 
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            UsbManager manager = (UsbManager)GetSystemService(Context.UsbService);
    
            UsbReciever usbReciever = new UsbReciever();
            PendingIntent mPermissionIntent = PendingIntent.GetBroadcast(this, 0, new Intent(
                ACTION_USB_PERMISSION), 0);
            IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
            RegisterReceiver(usbReciever, filter);
    
            foreach (var dev in manager.DeviceList)
            {
                if (dev.Value.VendorId == 8192)
                {
                    device = dev.Value;
                }
            }
            manager.RequestPermission(device, mPermissionIntent);
            bool hasPermision = manager.HasPermission(device);
    
            UsbDeviceConnection connection = manager.OpenDevice(device);
            if (connection == null)
            {
                return;
            }
            Button button = FindViewById<Button>(Resource.Id.MyButton);
        }
        class UsbReciever : BroadcastReceiver
        {
            public override void OnReceive(Context context, Intent intent)
            {
                String action = intent.Action;
                if (ACTION_USB_PERMISSION.Equals(action))
                {
                    lock (this)
                    {
                        UsbDevice device = (UsbDevice)intent
                                .GetParcelableExtra(UsbManager.ExtraDevice);
    
                        if (intent.GetBooleanExtra(
                                UsbManager.ExtraPermissionGranted, false))
                        {
                            if (device != null)
                            {
                                // call method to set up device communication
                            }
                        }
                        else
                        {
    
                        }
                    }
                }
            }
        }
    
    
    }
    

    【讨论】:

    • 这个com.android.example.USB_PERMISSION有什么用?
    • @i-Droid 在与 USB 设备通信之前,您的应用程序必须获得用户的许可。注意:如果您的应用程序使用意图过滤器在 USB 设备连接时发现它们,并且如果用户允许您的应用程序处理意图,它会自动获得权限。如果没有,您必须在连接到设备之前在您的应用程序中明确请求权限。参考:developer.android.com/guide/topics/connectivity/usb/host.html
    • 这不会为我弹出“权限”对话框:/ 我对 Xamarin.Android 非常陌生,我的任务是创建一个从罗技 C270 网络摄像头拍摄照片的应用程序。你能帮我完成第一步吗?
    • 所以你在MainActivity 中发现你需要许可,所以你请求它,然后你最终进入UsbReceiver。但是您不能在那里设置设备通信,因为您需要在MainActivity 中使用Device —— 一个不同的类?请求许可后如何将控制权交还给MainActivity 以完成设置??
    • 如果您使用从 UsbManager 手动请求权限,您的应用将永远不会记住用户的选择。每次插入和拔出设备时,用户都必须确认。
    猜你喜欢
    • 2019-09-20
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 2014-05-07
    相关资源
    最近更新 更多