如何授予使用 USB 管理器打开 USB 设备的权限?openDevice() 总是返回 null [英] How to grant permission to open usb device with usb manager? openDevice() always returns null

查看:17
本文介绍了如何授予使用 USB 管理器打开 USB 设备的权限?openDevice() 总是返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在以下代码中使用 USB 设备.它成功地列出了 USB 设备并遍历它们.在以下代码中,对象设备"是我需要打开的 USB 设备.除了 OpenDevice() 方法总是返回一个 空值

I want to use a usb device in the following code. It successfully lists the usb devices and iterates over them. In the following code the object "device" is the usbdevice that i need to open. Everything seems Ok except the OpenDevice() method that always returns a null value!

[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 包含以下几行:

The device_filter.xml contains the following lines:

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

When I tried bool hasPermision = manager.HasPermission(device); I saw that hasPermission is false. Could anybody tell me How can I grant permission for opening a usb device in xamarin? Thanks for any help.

推荐答案

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

At last I fixed it after trying several suggestions. I found that adding the intent filter in manifest does not solve the permission issue for unknown reasons. It's sounds like a bug of the Xamarin. To tell the truth, It seems that Xamarin usb namespace is too naive, and its better you do not waste your time to use that for USB management and connection. It also has some other annoying limitations. For example look at here. (So I suggest to write low level usb communication codes in java and import the jar file in xamarin by JNI, I tired it and I should say It's a lot easier than it seemed at first time)

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

To grant the permission of opening the usb device, you have to use the grantPermission() method. The following code shows how to use the method and BroadcastReceiver to pop up a dialog asking the user to let the usb usage.

 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
                    {

                    }
                }
            }
        }
    }


}

这篇关于如何授予使用 USB 管理器打开 USB 设备的权限?openDevice() 总是返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆