3

I am developing an android app which will retrieve data every 30sec and send to the server, so mobile internet is necessary.

currently, I am using GPS but I also want to use network provider (wifi and cell tower's).

But my question is how does android retrieve location info through wifi? If the location can be retrieved from wifi then can it be retrieved from my mobile internet itself?

If no then why? If yes then show me some code snippets to retrieve location update from mobile internet and also what can be the approx accuracy? I need accuracy to be within 100meters.

I followed these and got this doubt http://developer.android.com/guide/topics/location/strategies.html Is android application able to retrieve location info from connected WiFi hotspot AP?

1 Answer 1

5

To retrieve the location from a NETWORK_PROVIDER you can use the following:

LocationListener locationListenerNetwork = new LocationListener() {
    public void onLocationChanged(Location location) {
        timer1.cancel();
        locationResult.gotLocation(location);
        lm.removeUpdates(this);
        lm.removeUpdates(locationListenerGps);
    }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

LocationManager lm;
if(lm==null)
  lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

This is however a basic example, so to distinguish between the wi-fi and the 3g network first take a look here and before the code above do:

One certain way of obtaining what you want is the following:

ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

State mobile = conMan.getNetworkInfo(0).getState();
State wifi = conMan.getNetworkInfo(1).getState();

After that, you distinguish them by:

if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
    //mobile
} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
    //wifi
}

Check, if the mobile or wifi is connected, if wifi - then use PASSIVE_PROVIDER, if mobile - use NETWORK_PROVIDER.

I hope all of this information is useful for you.

2
  • thanks for the reply but what will be the accuracy from wifi and mobile data? Accuracy wise both will be same?
    – Nagashayan
    Aug 16, 2013 at 16:11
  • No, it will not be the same, the 3g uses triangulation and depending from where the towers are this triangle can be very big. With the wi-fi however, the router's position is known and your coordinates are m ore precise (the calculated radius around it is much smaller).
    – g00dy
    Aug 18, 2013 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.