如何使用usb manager授予打开usb设备的权限?openDevice()始终返回null

a.t*_*aby 5 android xamarin.android xamarin android-usb

我想在以下代码中使用USB设备.它成功列出了usb设备并迭代它们.在下面的代码中,对象"device"是我需要打开的usbdevice.除了总是返回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).
    }
Run Code Online (Sandbox Code Playgroud)

device_filter.xml包含以下行:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
   <usb-device product-id="8704" vendor-id="5401" />
</resources>
Run Code Online (Sandbox Code Playgroud)

当我尝试bool hasPermision = manager.HasPermission(device); 我看到hasPermission是假的.有人可以告诉我如何批准在xamarin中打开USB设备?谢谢你的帮助.

a.t*_*aby 9

最后我在尝试了几个建议后修复了它.我发现在清单中添加intent过滤器并不能解决权限问题,原因不明.这听起来像是Xamarin的一个bug.说实话,似乎Xamarin usb命名空间太天真了,而且你最好不要浪费时间用于USB管理和连接.它还有一些其他烦人的限制.比如看这里.(所以我建议在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
                    {

                    }
                }
            }
        }
    }


}
Run Code Online (Sandbox Code Playgroud)

  • 因此,您在“MainActivity”中发现需要权限,因此您请求它,然后最终进入“UsbReceiver”。但是你_不能_在那里设置设备通信,因为你需要“MainActivity”中的“Device”——一个不同的类?请求许可后如何将控制权传递回“MainActivity”以完成设置? (2认同)
  • 如果您使用从 UsbManager 手动请求权限,您的应用程序将永远不会记住用户的选择。每次插入和拔出设备时,用户都必须进行确认。 (2认同)