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
Android UsbManager RequestPermission返回Null_Android_Xamarin_Usb_Android Permissions - Fatal编程技术网

Android UsbManager RequestPermission返回Null

Android UsbManager RequestPermission返回Null,android,xamarin,usb,android-permissions,Android,Xamarin,Usb,Android Permissions,我对Xamarin开发相当陌生,我发现了一些代码,可以让我连接到usb设备(本例中是热敏打印机)。 我试图请求权限,但请求似乎返回null var matchingDevice = usbManager.DeviceList.FirstOrDefault(item => item.Value.VendorId == vendorId && item.Value.ProductId == productId); if (usbManager.Devi

我对Xamarin开发相当陌生,我发现了一些代码,可以让我连接到usb设备(本例中是热敏打印机)。 我试图请求权限,但请求似乎返回null

var matchingDevice = usbManager.DeviceList.FirstOrDefault(item => item.Value.VendorId == vendorId && item.Value.ProductId == productId);
             if (usbManager.DeviceList.Count == 0)
                throw new Exception("No USB device connected");
             if (matchingDevice.Value == null)
                throw new Exception("Device not found, try to configure it in settings.");

             // DeviceList is a dictionary with the port as the key, so pull out the device you want.  I save the port too
             var usbPort = matchingDevice.Key;
             var usbDevice = matchingDevice.Value;

             // Get permission from the user to access the device (otherwise connection later will be null)
             if (!usbManager.HasPermission(usbDevice))
             {
                try
                {
                   //PendingIntent pi = PendingIntent.GetBroadcast(Forms.Context, 0, new Intent(ACTION_USB_PERMISSION), 0);
                   PendingIntent pi = PendingIntent.GetBroadcast(MainActivity.Instance, 0, new Intent(ACTION_USB_PERMISSION), PendingIntentFlags.UpdateCurrent);
                   usbManager.RequestPermission(usbDevice, pi);
                   throw new Exception("Relaunch the application");
                }
                catch (Exception ex)
                {
                   throw new Exception($"{usbDevice.DeviceName} - {ex.Message}");
                }
             }
当我运行应用程序时,我会得到端口号、设备号和设备名,但权限弹出窗口显示“允许访问空” 为什么RequestPermission会尝试将设备附加到null?任何帮助都将不胜感激


非常感谢

首先,如果将
添加到您的
AndroidManifest.xml
文件中,请首先检查
。我建议你在使用之前先阅读这篇文章@LeonLu MSFT感谢您的链接,这里有一些有用的信息,希望能对我有所帮助。您是否尝试在您的
AndroidManifest.xml
中添加该属性?@LeonLu MSFT将会是什么属性?抱歉,我是Xamarin的新手,目前我所知不多!!我写了一个简单的代码,可以测试一下。我没有这些设备。我不能为你测试它。