1

I have a Xamarin Forms app where I am trying to get location of the phone using Xamarin Essentials. App targets Android version 11 (API 30). I have provided all the location permissions in manifest file. It works fine below Android 11 but having issue with Android 11.

Every request to var location = await Geolocation.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.Medium)); returns null.

Below is the code to request permission and location:

 private async Task locationPermission()
    {

        try
        {

            PermissionStatus whenInUsePermission = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
            if (whenInUsePermission != PermissionStatus.Granted)
            {
                whenInUsePermission = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
            }

            PermissionStatus locationAlwaysPermission = await Permissions.CheckStatusAsync<Permissions.LocationAlways>();
            if (locationAlwaysPermission != PermissionStatus.Granted)
            {
                locationAlwaysPermission = await Permissions.RequestAsync<Permissions.LocationAlways>();
            }

            if (whenInUsePermission == PermissionStatus.Granted || locationAlwaysPermission == PermissionStatus.Granted)
            {
                // do something
                await GetDeviceLocation();
            }
        }
        catch (Exception ex)
        {

        }
    }

    public async Task GetDeviceLocation()
    {
        try
        {
            PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
            if (status != PermissionStatus.Granted)
            {
                PermissionStatus results = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
            }

            if (status == PermissionStatus.Granted)
            {
                var location = await Geolocation.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.Medium));

            }

        }
        catch (Exception ex)
        {


        }
    }

I have given below permissions in the manifest file:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.location" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
<uses-feature android:name="android.hardware.location.network" android:required="false" />
Jason Roy
  • 43
  • 6
  • But *exactly* what goes wrong? Test the value of each permission after attempting to acquire it - does each one change to Granted or not? If not, what is the first one that doesn't? Also, when you run the app for first time (delete/reinstall if necessary), does it bring up a dialog asking user for permission to use location? – ToolmakerSteve Jul 28 '21 at 17:51
  • If each status is anything other than `Granted` or `Unknown`, what value is it? – ToolmakerSteve Jul 28 '21 at 18:09
  • Have you followed setup instructions in [Xamarin Essentials: Permissions](https://learn.microsoft.com/en-us/xamarin/essentials/permissions?tabs=android)? Also, if permission is `Denied`: *"on Android you can call ShouldShowRationale to detect if the user has already denied the permission in the past."* (If so, all you can do is explain to them how to manually re-enable it.) – ToolmakerSteve Jul 28 '21 at 18:15
  • Bug in your code. `PermissionStatus results = await Permissions.RequestAsync();` should be `status = await ...`. But that would only affect the first call; after that it should be Granted... – ToolmakerSteve Jul 28 '21 at 18:20
  • I can see all the permissions are granted. WhenInUse and Always both of them are granted. I even provided background permission in the manifest. To be sure, I enabled permission by going to settings -> My App > Permission -> Location -> Allow Always but that did not work too. I followed all the setup instructions from Xamarin.Essentials. – Jason Roy Jul 29 '21 at 04:16
  • You could try to open the Settings> Location> Wi-Fi and Bluetooth scaning to enable the Wi-Fi and Bluetooth Scanning to it easily find your location. – Wendy Zang - MSFT Jul 29 '21 at 07:42

0 Answers0