Unable to retrieve location using 'LocationManager.NETWORK_PROVIDER' even when WiFi is enabled in device running android M

1.3k views Asked by At

I'm trying to retrieve location using LocationManager following this tutorial.

I'm running the code on an android device running Marshmallow and I have checked the permission and running below code once the permission is granted.

Here's my code:

public Location getLocation() {
        try {
            locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled

                Log.d("noProviderEnabled", "called");

            } else {
                this.canGetLocation = true;
                // First get location from Network Provider
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            currentLatDouble = location.getLatitude();
                            currentLngDouble = location.getLongitude();

                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                currentLatDouble = location.getLatitude();
                                currentLngDouble = location.getLongitude();

                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

Even though WiFi is enabled, isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); is returning false and Log.d("noProviderEnabled", "called"); is getting printed out.

Update: when I turned the GPS on and then run the above code.. only the isNetworkEnabled returned true and isGPSEnabled still returned false.

What's wrong here? Why am I unable to retrieve location using LocationManager.NETWORK_PROVIDER even when WiFi is enabled?

P.S.: This is working perfectly fine on android L and below.

0

There are 0 answers