首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何授予使用usb管理器打开usb设备的权限?openDevice()总是返回null

如何授予使用usb管理器打开usb设备的权限?openDevice()总是返回null
EN

Stack Overflow用户
提问于 2015-01-25 14:07:08
回答 1查看 29.5K关注 0票数 9

我想在下面的代码中使用usb设备。它成功地列出了usb设备并对它们进行了迭代。在下面的代码中,object "device“是我需要打开的usbdevice。除了总是返回空值的OpenDevice()方法之外,一切看起来都很好!

代码语言:javascript
复制
[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包含以下行:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" ?>
<resources>
   <usb-device product-id="8704" vendor-id="5401" />
</resources>

当我尝试bool hasPermision =manager.HasPermission(设备)时,我发现hasPermission是假的。有人能告诉我怎么才能允许我在xamarin打开usb设备吗?谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-29 15:13:05

经过几个建议,我终于把它修好了。我发现在清单中添加意图筛选器并不能解决未知原因的权限问题。听起来像是萨马林的窃听器。说实话,Xamarin usb名称空间似乎太天真了,最好不要浪费时间用于USB管理和连接。它还有其他一些恼人的限制。例如,看看这里。(所以我建议用java编写低级别的usb通信代码,并通过JNI用xamarin导入jar文件,我对此感到厌倦,应该说比第一次看起来容易得多)

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

代码语言:javascript
复制
 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
                    {

                    }
                }
            }
        }
    }


}
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28137280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档