3

I do app with geofencing use and since I started to work with it I get to broadcastReceiver the transitions and when I turned off the GPS location I got an alert with code GEOFENCE_NOT_AVAILABLE and I started a service to reregister the geofences when I will turn on the GPS. Now I took a few days break and suddenly my broadcastReceiver doesn't get alerts when I turn off the GPS. I don't understand why? *when I enter/exit from location (like I defined in the geofences init) I trigger the alerts, the problem is just at the triggering of the GEOFENCE_NOT_AVAILABLE alert.

I will glad if anyone can help me

  • 1
    I am noticing the same problem. I have been using Geofences for several years. Historically, whenever a user turned off their location services my Geofence BroadcastReceiver would trigger the GEOFENCE_NOT_AVAILABLE intent and I would know that their location has been disabled. I am no longer getting this intent any more and I have not changed any of that code. Something must have changed internally but the docs still suggest I should be getting the intent. I will look into submitting a bug report to the issue tracker with a repo that reproduces the problem. – Stephen Ruda Sep 26 '19 at 20:20
  • @StephenRuda have you started an issue with Google? This problem has become serious with Android 11 - not a single Android 11 device is receiving the GEOFENCE_NOT_AVAILABLE event which ultimately breaks all geofences. – NoHarmDan Aug 24 '21 at 10:17
  • @NoHarmDan It has been a long time so hopefully I remember everything correctly. I came to the conclusion that it was actually a 'feature' in a newer Google Play Services update. Originally, if you would get the GEOFENCE_NOT_AVAILABLE intent it was letting you know that Geofences have been unregistered because location was disabled. From my testing on devices with newer versions of Google Play Services installed, it will not send you the GEOFENCE_NOT_AVAILABLE intent because it doesn't actually unregister your Geofences. If you turn your location services back on they should start working. – Stephen Ruda Aug 24 '21 at 20:35

3 Answers3

0

For anyone experiencing this issue while running an app on Android 11 (API 30, even when targeting API 29 or lower with the build), it is confirmed to be intended behavior as Android OS no longer unregisters geofences when location services are turned off.

However you still cannot register geofences while the location services are off and thus must check their availability and prompt the user to turn them on whenever registering new ones is required. The previously registered ones do not need re-registering.

No relevant information has been given as to how this can happen on devices running Android 10 or older, which has also been reported by some developers. But it seems safe to assume the change was introduced earlier at least in some versions of Android OS (depending on device manufacturers). It would be very helpful if the original question mentioned whether this issue was on just some APIs or not, but I imagine due to its age it wasn't concerning Android 11. If anybody who has encountered this issue on lower APIs could test if their old geofences remained registered and usable, I'd gladly update the answer accordingly.

Source: https://issuetracker.google.com/issues/143967408

UPDATE: As mentioned by Stephen Ruda, it appears that it's not dependent on Android OS version, but on Google Play Services version. Which makes this behavior completely unpredictable.

NoHarmDan
  • 492
  • 5
  • 16
  • Sorry, I didn't see this answer. I posted a reply to your comment on the original question. From the testing I have done, I determined it was based on the version of Google Play Services that the device itself was running. At some point in late 2019 there was a Google Play Services update that changed the behavior. If your device is running a modern version of Google Play Services that includes this update, you should not get the GEOFENCE_NOT_AVAILABLE intent. Devices running the older versions would still get the intent. Almost all of my app users are running this newer version. – Stephen Ruda Aug 24 '21 at 20:48
  • Thanks for the update. Well that's awesome, great job Google, as always... Way to randomly break things... – NoHarmDan Aug 27 '21 at 10:29
  • I am all for the change. It is annoying for us developers because there are two different sets of behavior but ultimately the new behavior is better because you don't have to worry about re-registering Geofences yourself. I still have logic in my code to handle the GEOFENCE_NOT_AVAILABLE intent and it is set up to send us a notification when it occurs. Initially we got quite a few notices of it occurring but we very rarely get them any more. It has been ~2 years since that Google Play Services update was released. Over 99% of our users have the updated behavior. – Stephen Ruda Aug 27 '21 at 21:10
  • Oh don't get me wrong, considering the recreating geofences part, this is brilliant. But what Google (as usual) hasn't thought of, is that there can be other related functionality in apps which relies on this event... – NoHarmDan Aug 31 '21 at 08:00
0

We get GEOFENCE_NOT_AVAILABLE error when precise location permission is not granted to the application. Check if the following options are enabled, here I am referring android 13 OS:

  1. Settings->Apps->YourApp->Permissions->Location->Use precise location(turn this on)
  2. Settings->Location->Location services->Google Location Accuracy(turn this on)

This should solve GEOFENCE_NOT_AVAILABLE error

-1

GEOFENCE_NOT_AVAILABLE is an expected error code, to let you know that the location adapter is off and location (and geofence) will not be tracked anymore.

Once available again, your geofences will be active and you will get the desired callback.

Few Reasons why the triggers aren't happening :

  1. Check if your expiry timestamp that you set in your geofence request, is still valid.
  2. Did you device get restarted ? If yes, you need to re-register fences. (Refer documentation : Re-register geofences only when required documentation)

  3. Is your location, set to High Accuracy mode in Device settings ?

  4. When you re-register your fences, you should un-register them first and then only re-register.

  5. Open Google maps and check if its showing the right location.

Mayuri Khinvasara
  • 1,437
  • 1
  • 16
  • 12
  • I tried all of what you say, I defined NEVER_EXPIRE in the geofences and all settings of the GPS is what should be. The problem is that for some reason, the system alerts when I enter/exit saved location, but not alert when I turn off the GPS provider. I tried also to rebuild the project, delete the app and reregister the broadcastReceiver class, but still isn't working. – Yehonatan Rozenberg Aug 09 '19 at 07:34
  • So basically, your question is you are not getting alerts (GEOFENCE_NOT_AVAILABLE)? If you specifically want just the state of location adapter try this (https://stackoverflow.com/questions/843675/how-do-i-find-out-if-the-gps-of-an-android-device-is-enabled) – Mayuri Khinvasara Aug 09 '19 at 08:19
  • I need both enter/exit alerts and state of the GPS location. And I need it in the background, so what in the link is not good to my case. – Yehonatan Rozenberg Aug 09 '19 at 08:28
  • Technically, I solved the problem with never ending service that register ACTION_PROVIDER_CHANGED broadcastReceiver so when I change the state of the GPS I get alert but it is not the ideal solution I want because I want to start that service when I get the GEOFENCE_NOT_AVAILABLE alert and this alert isn't called. – Yehonatan Rozenberg Aug 09 '19 at 08:32
  • This answer does not answer the question at all. The question is not perfectly formed, true, but it's talking about the issue of the GEOFENCE_NOT_AVAILABLE event not being delivered, not what it means when it is. – NoHarmDan Aug 24 '21 at 10:18