Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Android Marshmallow Permission issue for USBManager

$
0
0

Am creating an Android app using Xamarin forms, which will access USB host. When am trying this application in android device version is less than 6.0(Marshmallow) its working perfectly.

Below is the snippet i am trying out.

static string[] PERMISSIONS_EXTERNAL_STORAGE = {
            Manifest.Permission.ReadExternalStorage,
            Manifest.Permission.WriteExternalStorage,
        };

static readonly int REQUEST_USB_ACCESS = 0;

if ((int)Build.VERSION.SdkInt < 23) {
    UsbManager.RequestPermission(mDevice, mPermissionIntent); // Working for Anroid version below 23
} else {

    RequestUSBPermissionAccess(); // Displaying popup but when taps on allow sametime not able to UsbManager.HasPermission       orUsbManager.OpenDevice
}


void RequestUSBPermissionAccess()
{
    if (ActivityCompat.ShouldShowRequestPermissionRationale((Activity)Forms.Context, Manifest.Permission.ReadExternalStorage))
    {
        ActivityCompat.RequestPermissions((Activity)Forms.Context, PERMISSIONS_EXTERNAL_STORAGE, REQUEST_USB_ACCESS);
    }
    else
    {
        ActivityCompat.RequestPermissions((Activity)Forms.Context, PERMISSIONS_EXTERNAL_STORAGE, REQUEST_USB_ACCESS);
    }
}   

void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
    if (requestCode == REQUEST_USB_ACCESS)
    {

        if (grantResults.Length == 1 && grantResults[0] == Permission.Granted)
        {

        }
        else
        {
            OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}   

Need to implement the same

if (UsbManager.HasPermission (mDevice)) {                   
    mConnection = UsbManager.OpenDevice (mDevice);
    if (mConnection != null) {
        ...................
        ...................
    }
}

Working for Anroid version below 6.0 but when am trying with this code UsbManager.RequestPermission(mDevice, mPermissionIntent);

But not working for RequestUSBPermissionAccess(), each time UsbManager.HasPermission (mDevice) or UsbManager.OpenDevice (mDevice) is null.

Please anybody help me with the same issue.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>