Search code examples
c#unity-game-engineandroid-gps

Turn on GPS by code in Unity


I'm creating an application that use Location services, and want to give the possibility to the user to get to Location services in his android device by a simple click in the application. How must i proceed ?


Solution

  • On 4.4 an earlier version there was a bug which let people enable GPS programmatically. So for Android 5 and later there is no way to do that. In Unity accessing such features can only be done using Java Plug-in. Here is the Java code which will show a pop-up that ask the user to choose if they want let you use the GPS or not.

    startActivity(context, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
    

    I don't know if you know how to write Java Plug-in for Unity so here is the link where you can find a comprehensive guide. Go to this link Here you'll find how to convert your project to library after build your file will be here "build/output/aar/..."