Dan
Dan

Reputation: 2344

Trouble getting location data using LTE rather than WiFi

I am trying to get the Lat/Long for the device when using 4G/LTE. The code below works great when using WiFi but the onLocationChanged method doesn't get called at all when using 4G/LTE. Any idea why?

I only have a limited window to get the location coordinates as they need to be appended to an audit log at the beginning.

Does LTE/4G usually take much longer than WiFi to pinpoint the lat/long?

lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);



if (networkType.equals("WiFi")) {
    lp = LocationManager.NETWORK_PROVIDER;
}
else {
    lp = LocationManager.GPS_PROVIDER;
}


locationListener = new LocationListener() {
     public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
         gpsTestLocation = locationStringFromLocation(location);
         System.out.println("mcsoutput location: " + gpsTestLocation);
        }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {


    }

    @Override
    public void onProviderEnabled(String provider) {


    }

    @Override
    public void onProviderDisabled(String provider) {


    }
    };

    // Register the listener with the Location Manager to receive location updates
    lm.requestLocationUpdates(lp, 0, 0, locationListener);

Network Type is figured out by:

private String checkNetworkState() {
        ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    //  NetworkInfo mEthernet = connManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
        NetworkInfo m3G = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        TelephonyManager telephonyService = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);


        if (mWifi!=null) isOnWifi = mWifi.isConnected();
        if (m3G!=null) is3G = m3G.isConnected();

        if(isOnWifi == true) {
            return "WiFi";
        }
        else {
            int cellType = telephonyService.getNetworkType();
            return isConnectionFast(cellType);
        }

    }

Upvotes: 0

Views: 443

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93678

You're using NETWORK_PROVIDER when on wifi and GPS when off. Odds are you aren't getting a GPS synch. Its actually very hard to do when indoors. Are you getting a flashing GPS symbol in your notification bar? If so, you aren't getting a full GPS synch and thus onLocationChanged won't be called.

When on wifi using NETWORK_PROVIDER you'll get an almost instant location because NETWORK_PROVIDER needs no satellites and is almost always available, it just isn't nearly as accurate.

Upvotes: 1

Related Questions