LiberadoSelso-5372 avatar image
0 Votes"
LiberadoSelso-5372 asked LiberadoSelso-5372 commented

Xamarin - USB Host - Android - Request Permission to connect to device (Crash)

I'm trying to resolve a crash when my application ask for permission to connect to a detected device.
I'm handling in the Android part using the usbManager.
It appears the the RequestPermission() from the usbManager does not wait for user input, so App continue exécution and crash.
After restart if permission was given everything works fine.

At first I added a broadcast receiver in the android interface to handle the event, but the Xamarin.forms part is not award that the process is async.

So I started to add Xamarin.Essential support like shown in the Xamarin doc.

I did add the code for Activity startup to init Xamarin Essential and the overrided function :

protected override void OnCreate(Bundle savedInstanceState)
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);

I then create a Platform interface as advised in the Xamarin Guide. In that function a call the platform interface the request permission.

async Task<PermissionStatus> IUsbPermissions.RequestAsync()
PermissionStatus status;
status = await _iusbManager.RequestPermAsync(_szDevName);
await base.RequestAsync();
return status;

The problem is that the request permission just call the usbmanager request, so that I never fall in the override function : OnRequestPermissionsResult().

How can make it wait for the permission event ?
Does the fact that I define a custom permission interface disable the override OnRequestPermissionsResult() ?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LiberadoSelso-5372 commented


Welcome to our Microsoft Q&A platform!

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. For details, you can refer to the following link.

And I check the Available Permissions for essemtials that do not contains USB permission.

Best Regards,

Leon Lu

If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello Leon-Lu !

This is what I resolved to do yesterday :)

If interested you may see it on my public github project.
Sadly I did not manage to extend Xamarin.essentials to handle USB persmission, there's something missing in there explanations to get it working.

  • I did change my code to process asynchronously the premission result :

  • In the broadcastreceiver I added an Event that my interface class will handle to finalize usb connection, or not :).

  • Then I cascade it's result the the UI code.

0 Votes 0 ·