Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Runtime permissions do not work for GPS Location first two runs

Discussion in 'Android' started by eugeneloza, Nov 13, 2020.

  1. eugeneloza

    eugeneloza

    Joined:
    Dec 3, 2019
    Posts:
    24
    Hi all!

    I'm having an issue with requesting runtime permissions for location on Android.

    I ask for permissions this way:

    Code (CSharp):
    1.  
    2. if (!Permission.HasUserAuthorizedPermission(Permission.CoarseLocation) || !Permission.HasUserAuthorizedPermission(Permission.FineLocation))
    3. {
    4.   Permission.RequestUserPermission(Permission.CoarseLocation);
    5.   Permission.RequestUserPermission(Permission.FineLocation);
    6. }
    And the User is prompted to authorize the app for permissions. BUT, the first two runs of the app, the GPS service is unavailable. User has to restart the app twice to start receiving GPS location. After this the app receives location data flawlessly. If the app is uninstalled and installed again, again it has to be restarted twice to have GPS working.

    crop-screengrab-20201113-122947.jpg crop-screengrab-20201113-123002.jpg crop-screengrab-20201113-123013.jpg

    Maybe I'm doing something wrong? Or is this a bug?

    Reproduced on:
    Samsung Galaxy A40
    Samsung Galaxy A51
    Samsung Note 10+
    Xiaomi MI 9 SE

    The minimal test project is available at: https://github.com/eugeneloza/unity-gps-runtime-permissions-bug

    This happens since at least 21 February 2020, in Unity versions 2019.3.3f1 - 2020.1.13f1.
     
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    Permission.RequestUserPermission is asynchronous function, meaning it returns immediately even though the dialog still might be showing on the screen. You can easily confirm this by adding Debug.Log after each RequestUserPermission call, I am guessing Permission.RequestUserPermission(Permission.FineLocation); is ignored since the dialog from previous request is still showing.

    In future Unity versions this API will be extended, and you'll be able to request multiple permissions at once, like here https://docs.unity3d.com/2020.2/Doc...ndroid.Permission.RequestUserPermissions.html

    But for now, you need to rearrange code, maybe like this:
    Code (CSharp):
    1.  
    2. if (!Permission.HasUserAuthorizedPermission(Permission.CoarseLocation)
    3.   Permission.RequestUserPermission(Permission.CoarseLocation);
    4. if (Permission.HasUserAuthorizedPermission(Permission.CoarseLocation) &&  !Permission.HasUserAuthorizedPermission(Permission.FineLocation))
    5.    Permission.RequestUserPermission(Permission.FineLocation);
    6.  
    assuming this is called from a coroutine or Update.
     
    eugeneloza likes this.
  3. JuliusM

    JuliusM

    Unity Technologies

    Joined:
    Apr 17, 2013
    Posts:
    824
    If you are asking for a FineLocation permission, there is no point in asking for a CoarseLocation permission. The app's logic should require either one or the other. If user declines a FineLocation permission, you could try asking for a CoarseLocation permission, however if you first start by asking for a CoarseLocation permission, the user declines it and then you ask for a FineLocation permission, your app might be rejected during the app review.
     
    eugeneloza likes this.
  4. eugeneloza

    eugeneloza

    Joined:
    Dec 3, 2019
    Posts:
    24
    Many thanks! It worked like a charm.
     
  5. eugeneloza

    eugeneloza

    Joined:
    Dec 3, 2019
    Posts:
    24
    Thank you! Just tested and it works perfectly.
     
  6. frpe1

    frpe1

    Joined:
    May 4, 2020
    Posts:
    10
    It doesn't work.
    Is it possible to share some code example that really works ?
    Its not tracking, So it comes to a point when it says "Timed out, no connection".
    And I tested outside as well and make sure android has permission for using location.

    Note it used with 2019.4.17 like a charm
    but with new input system and 2020.2.6f1 definitely not.
     
    Last edited: Mar 7, 2021