21

I'm attempting to add a geofence to an Android application. I noticed the following in the logs:

07-04 11:41:19.201 1945-2271/com.google.android.gms.persistent W/GeofencerStateMachine: Ignoring addGeofence because network location is disabled.

I also noticed that when I enable "High accuracy" mode in the system settings, it works (This is described as GPS, Wi-Fi, Bluetooth, or cellular networks). It was previously on "Device only" mode, which is GPS only.

Is this the expected functionality for geofences? Will they only work if the users have their device in "High accuracy" mode?

enter image description here

zundi
  • 2,361
  • 1
  • 28
  • 45

2 Answers2

2

First of all for High accuracy mode is necessary for all aspect of the locations even Google Map also Use this mode nowadays.

So Geofencing combines awareness of the user's current location with awareness of the user's proximity to locations that may be of interest. To mark a location of interest, you specify its latitude and longitude. To adjust the proximity for the location, you add a radius. The latitude, longitude, and radius define a geofence, creating a circular area, or fence, around the location of interest. That's why its necessary for accurate location High accuracy Mode is necessary. Here is the official Doc

Secondly according to Documentation reliable data connection required because Geofence depends upon on it. According to this its dependent on Reliable data connection.

Depending on how a geofence is configured it can prompt mobile push notifications, trigger text messages or alerts, send targeted advertisements on social media, allow tracking on vehicle fleets, disable certain technology or deliver location-based marketing data. Moreover Detail about geofencing please read this.

Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
0

While, it is recommended to get High Accuracy permission from your users for Geofence purposes. You can ensure providing a dialog for high accuracy from your user if is changed, similar to other location apps do as of now like uber or ola etc.

Something like this:

public void displayLocationSettingsRequest(final Activity activity, final int requestCode) {

    final Task<LocationSettingsResponse> result = createLocationRequestForDialogDisplay(activity);

    result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
        @Override
        public void onComplete(Task<LocationSettingsResponse> task) {
            try {
                LocationSettingsResponse response = task.getResult(ApiException.class);


                if(!InventaSdk.locUpdateStarted) {
                    try {
                        initAndCheckEligibility(); //start location updates when all settings are satisfied
                    } catch (SecurityException s) {
                        P2pLogHelper.e(TAG, s.getMessage());
                    }
                }
                // All location settings are satisfied. The client can initialize location
                // requests here.
            } catch (ApiException exception) {
                switch (exception.getStatusCode()) {
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied. But could be fixed by showing the
                        // user a dialog.
                        try {
                            // Cast to a resolvable exception.
                            ResolvableApiException resolvable = (ResolvableApiException) exception;
                            // Show the dialog by calling startResolutionForResult(),
                            // and check the result in onActivityResult().
                            resolvable.startResolutionForResult(
                                    activity,
                                    requestCode);
                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the error.
                        } catch (ClassCastException e) {
                            // Ignore, should be an impossible error.
                        }

                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:

                        // Location settings are not satisfied. However, we have no way to fix the
                        // settings so we won't show the dialog.
                        break;
                }
            }
        }
    });
}


 private static Task<LocationSettingsResponse> createLocationRequestForDialogDisplay(Context context) {
    // Create LocationSettingsRequest object using location request
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.setNeedBle(true);
    builder.setAlwaysShow(true);
    builder.addLocationRequest(createLocationRequest());
    LocationSettingsRequest locationSettingsRequest = builder.build();

    // Check whether location settings are satisfied
    // https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
    SettingsClient settingsClient = LocationServices.getSettingsClient(context);
    return settingsClient.checkLocationSettings(locationSettingsRequest);
}

Besides this, also if your users have WiFi available the accuracy is higher.

Having Wi-Fi on can significantly improve the location accuracy, so if Wi-Fi is turned off, your application might never get geofence alerts depending on several settings including the radius of the geofence, the device model, or the Android version. Starting from Android 4.3 (API level 18), we added the capability of “Wi-Fi scan only mode” which allows users to disable Wi-Fi but still get good network location. It’s good practice to prompt the user and provide a shortcut for the user to enable Wi-Fi or Wi-Fi scan only mode if both of them are disabled. Use SettingsClient to ensure that the device's system settings are properly configured for optimal location detection.

Source: https://developer.android.com/training/location/geofencing#Troubleshooting

Arnab Saha
  • 511
  • 6
  • 15