I've developed an app that receives the location from both GPS_PROVIDER and NETWORK_PROVIDER, however the NETWORK_PROVIDER returns the best values retrieved from WIFI and GPRS, without having any control over it. I need to get the value returned by the GPRS location listener even when you have WIFI active, so I can use it to dismiss the fake locations from other apps.
Is it possible to do this?
At the moment I'm testing this solution Disable / Check for Mock Location (prevent gps spoofing) , I'll let you know if it solves my issue
Unfortunately it is not possible, no.
You can do something like this:
final LocationManager locationManager = getLocationManager();
final List<String> providers = locationManager.getProviders(true);
if (providers.size() > 1) {
providers.remove(LocationManager.NETWORK_PROVIDER);
}
But you don't have the option to distinguish between a cell or a wifi network-provider!
This provider determines location based on availability of cell tower
and WiFi access points. Results are retrieved by means of a network
lookup.
Constant Value: "network"
Source: http://developer.android.com/reference/android/location/LocationManager.html#NETWORK_PROVIDER
What you can try is to use this solution. However, since this doesn't seem to be documented I'd be careful relying on it because it might break in future OS versions or might not work on every device.
Related
I am developing an android application in which i want to use wifi only to find user's location is it possible to find location of your device just by using Wifi ?
In this app i m trying to guide user to go from one place to another inside a building so i want exact location cell tower location or gps location will not be of any help
LocationManager.NETWORK_PROVIDER will use either Wifi or cell tower location (not GPS). If you want the best location, that would be fine GPS (wifi is the least accurate.) You can ask the LocationManager to getBestProvider and require fine GPS with:
criteria.setAccuracy(Criteria.ACCURACY_FINE);
Based on your latest comment, I think you would like to guide a user inside a building and somehow get more accurate locations than a gps location (which would probably not be accurate inside a building anyway). While there may be some specific situation in which this would be possible, I would say no, there is not a general solution that could be applied.
For Current Location, using GPS is highly recommended. Because with only Wifi, it will not be accurate. But still you can use this:
String uriName = "geo:"+ lat+ "," + lon+ "?q=my+street+address";
startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uriName)));
This should work with roughly accuracy.
I think you want to guide the user while inside the building, if so, then I don't think that GPS or Wifi will be able to help you.
GPS provides a very fine location but require clear contact with the gps satellites in the space which won't be available inside buildings. Also the accuracy is within some meter distance which won't be effective in your case.
Cellular wifi will provide you location using the nearby cell tower which i think is self explanatory in your case. So, I don't think that both location services will of use in your case.
I have written a small app that receives the location from mobile 3g/wifi by using locationManager & NETWORK_PROVIDER parameter.
according to google's api it will get the location i wish (the other option is using the GPS_PROVIDER)
what i really desired was the WIFI location. I wanted to see its behavior and how the phone gets its location (i.e with wireshark)
in order to do that, i changed into flight mode & activated wifi.
then, i launched the app and clicked the button which starts the NETWORK_PROVIDER location service by calling the requestLocationUpdates function with time parameter = 10.
what that actually happened is that i managed to get the location but saw no traffic at all in wireshark.
could it be that Google gives me the approx. location in advance, when connecting to the wifi? (its the only explenation)
If so, is it possible to clear that cache, or whatever data it have stored, and force the phone to get a new (but the same) location?
Thanks in advance,
Eran.
Have you tried re-booting the phone, just so to remove any cache files which might be storing the location?
Since NETWORK_PROVIDER use cell location, wifi ssid and their signal strength to get your GPS coordinates.
Since you are in flight mode and cannot get cell location, just try to switch the connected wifi , maybe google service will be invoked to get new location.
How does LocationManager choose which NETWORK_PROVIDER to use when getting location data? I would like to be able to identify the providers that LocationManager selects the NETWORK_PROVIDER from.
For example, there are two nearby NETWORK_PROVIDER locations, X (close) and Y (not as close). When standing in a single spot, sometimes the NETWORK_PROVIDER will be X, sometimes Y. I assume that the location with the highest accuracy gets returned (which is why usually X is returned), and all other networks are ignored, but I would like to return a list of all the nearby networks (or at least the top 2 or 3) at the same time.
Any of the .getProvider() methods (as far as I know) return the type of the provider (network, gps, passive), but nothing to actually identify the provider. When the location is returned, the location data is unique for the providers, but cannot be used to choose providers. I can get data from X, but I can't tell the program to ignore X (or can I?).
Anyway to make this happen? To return a list or array of NETWORK_PROVIDERS and then step through them getting location data?
And no, using GPS_PROVIDER is not an acceptable answer, because I need NETWORK_PROVIDER data only.
This answer is not based on any evidence, but rather my understanding of how the location provider works. When you request NETWORK_PROVIDER, you're basically asking it to give you the location that they estimate based on all the information that is available. For example, you might have 3 wifi networks and 2 cell towers, and that info is sent up to a server on the internet. Using that info, they calculate where they think you are, and return that to you. So, in other words, the network_provider location is based on the sum of all wifi/cell-tower info available to your phone.
Individually, a wifi hotspot doesn't tell you too much about where you are. A cell tower gives you some location info, but you'll need a few sources to accurately triangulate your location. Also, note that you need a internet/data connection, since the location is determined by some server, and not done on your phone. This is because knowing the nearby wifi hotspots (more specifically the MAC addresses) is useless without a database that maps each hotspot to it's physical location.
We know there are two positioning mode in Android: GPS and network. If we use network, then Android can use WIFI or 2G/3G cell tower to position. Without GPS, we can simply use LocationManager.NETWORK_PROVIDER to get the location data. But how can we know the mode is WIFI or 2G/3G cell tower exactly? Android doesn't provide separate providers for WIFI and cell tower.
I have thought a method. But I am not sure whether it is correct. Please review and give comments:
Check whether I can get a WIFI hotspot list. If nothing in the list, cell tower must be used.
If there are some WIFI hotspots, and the accuracy is high (<=100m), then WIFI is probably used. If the accuracy is low, is still cell tower used?
In my understanding, the accuracy of WIFI positioning is relatively high. But what's the normal range of the accuracy for it? Another question, does Android use WIFI and cell tower at the same time? If it does, then in my application, I can think it use WIFI, not cell tower.
Thanks!
Actually, there is a way to do this. It is not just documented well and thus might also change without notice.
You can get the used location estimation method via the network location provider location update extra data.
Bundle extras = location.getExtras();
if (extras.containsKey("networkLocationType")) {
String type = extras.getString("networkLocationType");
if (type .equals("cell")) {
// Cell (2G/3G/LTE) is used.
} else if (type .equals("wifi")) {
// Wi-Fi is used.
}
}
This is correct, Android does not explicitely give a way to determine how the network location is computed. The principle of the network provider is to collect all the relevant data on the device, and then send it all to the Google servers, where your location is computed and sent back to you. I am not sure, but the network location might also use the accelerometer/gyro/compass data to improve the accuracy of the location, especially when you compute your location continuously.
So I believe it can use simultaneously cell tower and Wifi info.
Your accuracy method should work in most cases, but is not 100% reliable.
Another possibility is to turn the wifi off, compute a first location fix, then turn it on again and give it another try. by comparing the two locations and their accuracy estimates, you can probably guess the influence of wifi on the location engine at your current location.
Out of curiosity, why do you need this for?
I am using this code to get the location provider and location.
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
mBestProvider = mLocationManager.getBestProvider(criteria, false);
mLocation = mLocationManager.getLastKnownLocation(mBestProvider);
If I turn off the GPS, the location is by network, but if I reboot the phone(so I loss the last position known) and with the 3G data connections off. So I am only using WIFI, I cant get any provider thus therefore any location. However google places app can locate me. I think it might be getting the lastknownlocation.But in that case my others applications should be able to get that location. Any idea whats happening?
The way you are calling this it will return all providers enabled or not because you are passing it false which is probably your intention but have you checked the return string?
mBestProvider = mLocationManager.getBestProvider(criteria, false);
You might be getting the gps provider, or you might be getting the network provider, I have learned not to trust the criteria mechanism because it seems to work differently per carrier and device (I have had some weird bugs reported because of this)
So I always ask specifically for gps and network providers and check last known for both, then use an algo to determine the best one to use.
The network provider can use cell or wifi hotspot/routers to determine location (google keeps a database of wifi information) so it's possible to get a fix with just wifi, not saying that is whats happening but it could be.
If that bears no fruit then it's possible that they are simply caching the last location update in preferences, some applications do that. To test the thesis, failing all of the above just leave the phone in that state and move to a very different location with the same properties if possible, should only take 2000 meters or so. If your app still reports null and places reports the old location you have your answer.
If places did report the newer location with wifi, and your app cannot (assuming you actually verify you are getting the network provider) then there is a chance they are using a private API via the Google Location Server (GLS) / MASF server via partial cell / wifi info but that's at the extreme end of the tin foil curve.