- Home /
How can I get GPS data on my Android device without WiFi?
Is there any way to get location data (GPS) on Android reliably when the device is offline without writing a plugin by now?
I'm referring to this plugin tutorial: http://www.mat-d.com/site/unity-gps-plugin-development-tutorial-building-a-android-plugin-for-unity-with-eclipse-and-ant/
I'm using this method right now with Unity 4.5: http://docs.unity3d.com/ScriptReference/LocationService.Start.html
I don't get any information when I'm offline (longitude and latitude 0.0) on my Asus tablet (Android 4.2.2). When I'm online I do get data, but with pretty low accuracy (~ 40m), even outside.
Running the same app with my Samsung Galaxy S3 (Android 4.3), I don't experience any problems, with or without WiFi. I get extremely accurate results (~ 3-5m; outdoors, of course).
Is this dependant on the device or the Android version? Does anyone have any idea?
If there's no other way I'll try to get the plugin working. (Sorry, I'm new to all of this and a bit intimidated by the whole plugin building process.)
EDIT: Solved! =D Kind of. See my answer for my solution.
And your Asus tablet has GPS? Without meaning to state the obvious, but if it doesn't have a GPS chip and you turn off the Wi-Fi, I'm not sure how else you'd expect it to report location.
It does have GPS, and I've tried turning it on manually with the same results. I seem to get the data just fine using apps from the Google Play Store.
A friend of $$anonymous$$e has the same problem when she runs my app on her Phone.
(But I see why you had to ask. =) )
Answer by yzeal · Jun 10, 2014 at 08:19 PM
In the end, I basically just followed http://www.mat-d.com/site/unity-gps-plugin-development-tutorial-building-a-android-plugin-for-unity-with-eclipse-and-ant/ but I thought I'd post it anyway, because I had some (n00b) trouble with it.
The good news: There is indeed a way to get GPS data without WiFi or mobile network connections. The bad news: I did have to write a Unity plugin. But as soon as the GPS is activated by the plugin, Unity's methods also start working properly.
Because I also use Vuforia for my AR app, I had some trouble with the plugin tutorial mentioned above. In case anyone is trying to do something similar, the solution is pretty simple but took me an embarrassing amount of time to figure out:
You can use the GPSTest.java code from the tutorial (I added some code to return latitude and logitude), but you need to add the following to the imports:
import com.qualcomm.QCARUnityPlayer.QCARPlayerNativeActivity;
And your class needs to extend QCARPlayerNativeActivity instead of UnityPlayerActivity
public class GPSTest extends QCARPlayerNativeActivity {
as described here: https://developer.vuforia.com/resources/dev-guide/unity-android-plugins
Follow the steps in the Vuforia tutorial, but use the altered GPSTest.java, and that's it!
In my code, I use the plugin just for testing like this (as described in the tutorials) for example:
using UnityEngine;
using System.Collections;
public class GPSTestSkript : MonoBehaviour {
public string speed;
public double latitude;
public double longitude;
private AndroidJavaObject jo;
private bool posted;
void Start () {
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
#endif
}
void Update() {
if(jo != null){
latitude = jo.CallStatic<double>("getLatitude");
longitude = jo.CallStatic<double>("getLongitude");
speed = jo.CallStatic<string>("getSpeed");
}
}
}
As always. Unity normally works with the android if to write own plug-ins. It is sad:)
True. At least I learned how to write Android plugins for Unity now. =) I'm sure it will work fine in Unity 5 or something.
Answer by VonTala · Jan 07, 2016 at 11:14 AM
The answer is in the configuration of your android: settings -> location services (More) disable USE WIRELESS NETWORKS enable USE GPS SATELLITES after that my program start working with true lat - long
Answer by zharik86 · Jun 08, 2014 at 07:24 PM
You can receive your location two methods. The first method is to use the Unity method. But for this purpose to be necessary connection from the Internet. (GPS works with Wi-Fi or other Internet conection: 3G, 4G and etc). Or the second method which is specified at you in the reference is an approximate calculation of your coordinates on the basis of a mobile network.
But why does it work on my S3 when I'm offline (and not on my friend's phone)? I don't really understand...
@yzeal Strange. I tested a code from Unity docs for GPS of coordinates on many devices of android (HTC and Sony and Sony Experia Tablet Z, and platform 2.2 - 4.2.2). If I in an offline, values were 0. As soon as it was connected to the Internet, I received coordinates. Unfortunately I have no S3 so couldn't check on it. $$anonymous$$aybe it depends on S3 (from its logic). Try to receive your coordinates when you are an offline and the mobile network is disconnected($$anonymous$$ode in the airplane). If it and after that will give out the correct coordinates, I buy S3 :)
@zharik86 Thanks for taking so much time to help me! I just tried what you suggested, and I guess you're getting a new phone. ;) Indoors, it just gave me the values from the last connection and stopped updating (as can be expected, because it was using the WiFi connection), but as soon as I went outside and waited for a bit, it gave me values with very nice accuracy and ~ the update frequency I specified in Start() when I walked around.
I still don't get why the device would need a WiFi connection to turn on its GPS chip. $$anonymous$$y navigation system certainly isn't capable of going online or connecting to a mobile network. But I have to admit that I don't understand very much about that kind of technology, so I'm probably getting something really wrong.
@yzeal Really, in I created the project in the last Unity(4.5). I set it on Sony Experia Tablet Z (android 4.4.2). I delivered in pad settings: Location-> only GPS. And without Wi-Fi I received any coordinates excellent from 0. In Unity 4.1.2 which I use, Wi-Fi was in most cases necessary. Try to change Location Settings on other device. And the main thing, different Android versions work on a different with GPS.
And when I created applications for location deter$$anonymous$$ation, I had to test them to the Android 4.2.2 version. So there is nothing surprising that for this version the android and is necessary to Wi-fi.
@yzeal Have you correct coordinates of GPS(offline mode)? Simply latitude at me correct, but here longitude incorrect. It turns out that longitudeTRUE = longitudeGPS - latitudeGPS. It is strange. There can be the next bug the android 4.4.2.
Your answer
