Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

RequestPermission Is Null

Hi,

Am new to Xamarin development, have found some code from these forums that gets me to connect to a usb device (a thermal printer in this case).

Am trying to request permissions, but the request appears to be returning 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}");
            }
         }

When I run the app, I get the port number and the device number, but the popup for permissions says something like 'Allow connection to null'

Why would RequestPermission return null? Any help would be massively appreciated.

Many thanks

Answers

  • DeepakDYDeepakDY INMember ✭✭✭

    Hello @rodkort ,

    Are you resolved this issue.

    When i tried to connect usb, the Device list is null.

    have you any idea for this.

Sign In or Register to comment.