Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何授予使用usb管理器打开usb设备的权限?openDevice()始终返回null_Android_Xamarin_Xamarin.android_Android Usb - Fatal编程技术网

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

Android 如何授予使用usb管理器打开usb设备的权限?openDevice()始终返回null,android,xamarin,xamarin.android,android-usb,Android,Xamarin,Xamarin.android,Android Usb,我想在下面的代码中使用usb设备。它成功列出usb设备并对其进行迭代。在下面的代码中,对象“device”是我需要打开的usbdevice。除了总是返回空值的OpenDevice()方法外,一切似乎都正常 device_filter.xml包含以下行: <?xml version="1.0" encoding="utf-8" ?> <resources> <usb-device product-id="8704" vendor-id="5401" />

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

device_filter.xml包含以下行:

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

当我尝试bool HasPermission=manager.HasPermission(设备)时;我知道你的许可是假的。有谁能告诉我如何才能授予在xamarin中打开usb设备的权限?
谢谢你的帮助。

在尝试了几个建议后,我终于修复了它。我发现在清单中添加意图过滤器无法解决权限问题,原因不明。这听起来像是Xamarin的一只虫子。说实话,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
                    {

                    }
                }
            }
        }
    }


}
公共类main活动:活动
{
私有静态字符串ACTION\u USB\u PERMISSION=“com.android.example.USB\u PERMISSION”;
USB设备;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
UsbManager manager=(UsbManager)GetSystemService(Context.UsbService);
UsbReciever UsbReciever=新的UsbReciever();
PendingEvent mPermissionIntent=PendingEvent.GetBroadcast(此,0,新意图(
操作(USB权限),0);
IntentFilter筛选器=新建IntentFilter(操作\u USB\u权限);
注册接收者(usbReciever,过滤器);
foreach(manager.DeviceList中的变量dev)
{
如果(dev.Value.VendorId==8192)
{
设备=偏差值;
}
}
manager.RequestPermission(设备、mPermissionIntent);
bool haspmission=manager.haspmission(设备);
UsbDeviceConnection=manager.OpenDevice(设备);
if(连接==null)
{
返回;
}
Button Button=FindViewById(Resource.Id.MyButton);
}
UsbReciever类:广播接收机
{
公共覆盖void OnReceive(上下文、意图)
{
字符串action=intent.action;
if(动作\u USB\u权限等于(动作))
{
锁(这个)
{
UsbDevice设备=(UsbDevice)意图
.GetParcelableExtra(UsbManager.ExtraDevice);
if(intent.GetBooleanExtra)(
UsbManager.extrapermissiongrated,false))
{
如果(设备!=null)
{
//调用方法来设置设备通信
}
}
其他的
{
}
}
}
}
}
}

thiscom.android.example.USB_PERMISSION有什么用?@i-Droid在与USB设备通信之前,您的应用程序必须具有用户的权限。注意:如果您的应用程序在连接USB设备时使用意图过滤器来发现这些设备,那么如果用户允许您的应用程序处理意图,它将自动接收权限。否则,在连接到设备之前,必须在应用程序中明确请求权限。请参阅:这不是为我弹出权限对话框:/我对Xamarin.Android非常陌生,我的任务是创建一个从Logitech C270网络摄像头拍摄照片的应用程序。您能帮我完成第一步吗?因此,您在
MainActivity
中发现您需要许可,因此您请求许可,然后进入
UsbReceiver
。但是您不能在那里设置设备通信,因为您需要
设备
内部
main活动
——一个不同的类?请求权限后,您如何将控制权传递回
MainActivity
以完成设置?如果您使用UsbManager的手动请求权限,您的应用程序将永远不会记住用户的选择。用户必须在每次插入和拔出设备时进行确认。
 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
                    {

                    }
                }
            }
        }
    }


}