如何授予使用 usb 管理器打开 usb 设备的权限? openDevice() 总是 returns 空

How to grant permission to open usb device with usb manager? openDevice() always returns null

我想在下面的代码中使用一个usb设备。它成功地列出了 USB 设备并遍历它们。在下面的代码中,对象 "device" 是我需要打开的 USB 设备。除了总是 returns 一个 空值 !

的 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 设备的权限? 感谢您的帮助。

在尝试了几个建议后,我终于修复了它。我发现在清单中添加 intent 过滤器并不能解决权限问题,原因不明。这听起来像是 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
                    {

                    }
                }
            }
        }
    }


}