Background location that works?

  • 1
  • Problem
  • Updated 4 years ago
I use cordova-plugin-background-mode to achieve background geolocation on iOS, but even with this activated I don't get any locations on Android using the standard Javascript means (navigator.geolocation). It's postponed until I focus the app.

I found cordova-plugin-mauron85-background-geolocation-phonegapbuild that might fix this. It's adapted to PhoneGap Build, but it doesn't build for Android, and I get this advice:

npm WARN deprecated cordova-plugin-mauron85-background-ge...: This plugin is deprecated. Use cordova-plugin-mauron85-background-geolocation instead.

I do so, and then I still get an error:

* What went wrong:
Execution failed for task ':transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.99.x86_64/bin/java'' finished with non-zero exit value 2

Anyone that knows any rules of thumb for getting geolocation to work safely in the background on Android, and possibly a better way to support iOS as well, and that works with PGB as build system?

I know about cordova-plugin-background-geolocation, but I'd like to use something more lightweight, if available. Locations will not be stored anywhere. I only need latitude, longitude and accuracy, when they change.

And to cut to the chase: Is there a way to use navigator.geolocation in the background on Android? Maybe a missing permission?

Thanks in advance,
Anders
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes

Posted 4 years ago

  • 1
Photo of JesseMonroy650 (Volunteer)

JesseMonroy650 (Volunteer), Champion

  • 3325 Posts
  • 122 Reply Likes
@Anders,
If you are using Android 6.x as your test device, you have hit the new DOZE protocol. I have not blogged on this because there has not been a greate call for it. I suspect it will be another month or two --- likely by mid-summer this will be an issue for developers.

Here is my original post on this: HEADS UP: Android 6.0 SDK-23 blocks background operations.

Here are my raw notes: Android and Doze

Best of Luck
Jesse
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
The device is still on 5.1.1, but I build with SDK-23.

Google Play Developer Console doesn't like stepping down in build version (it simply doesn't allow me to upload such a version; I've tried it before). Otherwise SDK-22 would be a good choice.

I'll try that on PGB though.

Thanks for the note.

Anders
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
It didn't help to build for SDK-22, so it's something general regarding navigator.geolocation.

If anyone has succeeded getting locations while having background mode activated I'm all ears.

My code can use either getPosition or watchPosition via a condition. Neither generates any callbacks.

Anders
Photo of JesseMonroy650 (Volunteer)

JesseMonroy650 (Volunteer), Champion

  • 3325 Posts
  • 122 Reply Likes
@Anders,
the bigger point is that Android 6.x and Doze will block your background operations AND only work correctly while in the foreground. This appears to be your issue.

Are you running Android 6.x?
Jesse
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
Nope: 5.1.1.

I also tested removing cordova-plugin-geolocation to see if the browser's own support would kick in. A test on navigator.geolocation claimed that it was there, but there were no location callbacks.

I included cordova-plugin-geolocation again and it works in the foreground as before, but not in the background.

I'll do some Inspecting to see if anything went wrong Javascript-wise.
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
No Javascript errors. It's claimed that in Android the plugin should be ignored, but it can't be, as the behavior is different with or without the plugin.
Photo of JesseMonroy650 (Volunteer)

JesseMonroy650 (Volunteer), Champion

  • 3325 Posts
  • 122 Reply Likes
It's claimed that in Android the plugin should be ignored, but it can't be, as the behavior is different with or without the plugin.

As you know, Standard Answer: Speak with author of plugin.

Best of Luck, let me know if you want code review.
Jesse
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
Thanks.
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Would it make sense to determine why you don't get any callback? I think there are 3 possibilities:

1. the getCurrentPosition() function is undefined
2. the getCurrentPosition() function is not called
3. neither callback is called

You could do a setInterval() and use the function, say, every 30 seconds.
The executed function should:
- alert the object type value for getCurrentPosition()
- alert the start of getCurrentPosition()
- alert the execution of either callback.

=============

One other thought, though!:
getCurrentPosition() will only be executed (or better: a new position returned) if there is a "significant position change"! Android will not return a new position if the device has only moved, say, 50 meters.

In order to prevent confusion, you should try watchPosition() with GPS quality, to make sure that you will detect smaller movements.
Also be aware that 'cached positions' could be in effect. Mind the maximumAge and timeout options.

May I assume that your GPS stays alive when the app turns to background mode?
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
I imagine it has to give me an initial location to kick things off. I'm aware it could be conservative from that point on.

With watchPosition and fine precision the GPS icon stays even when the app is in the background with background mode activated.

getCurrentPosition is called continuously, when I use that method, but there's no callback without the plugin.

I only used enableHighAccuracy. I've now also set maximumAge to 60 seconds. I don't need better than that.

I'll walk around the neighborhood to see if things change.
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
Odd without cordova-plugin-geolocation:
- No Javascript errors.
- The success function is never called.
- The failure function is called if I set a timeout ("timeout expired").

The reason why there's a difference in behavior with or without cordova-plugin-geolocation is because it adds permission for precise location.

Without it I get coarse location, but that's probably because the same app also does beacon detection. That in itself is a source of conflicts, as it goes completely bonkers if I initialize geolocation before beacon detection.

I'll test with only geolocation.
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
You should use the plugin, otherwise the app will ask for permission each time (at least in iOS).
You should also make sure you use FINE location (GPS), not COARSE location (network based), because that will not have an accuracy better than about 1km in Android.
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
After further testing it's clear watchLocation never calls the success function when the app is in the background.

When I used timeout and maximumAge, it always dropped into the failure function, even with long times (60 seconds or more).

I don't quite understand what criteria would be used here for determining it should not detect location, as the app is still fully active, yet not visible. Also, there's no error message, except of course timeout if I had a timeout set.

Have you or anyone you know tried cordova-background-geolocation-lt?
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
No sorry, I haven't.
I never needed to create an app that silently tracks the location of a user without him actively using the app.
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
Ok. It's not covert per se. It's rather that the user has the phone in the pocket but might need to be informed along the way. Locations are not saved.
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Just a thought (untested):
would it be useful to include the 'insomnia' plugin? Would that plugin enable you to wake up the app, catch a location, do what you need to do, then allow the app to sleep again? And repeat this every....5 minutes or so?
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
Could be, but wouldn't background mode achieve that too? I'll test it.
Photo of JesseMonroy650 (Volunteer)

JesseMonroy650 (Volunteer), Champion

  • 3325 Posts
  • 122 Reply Likes
@Anders,
it is worth knowing that even though Google/Android made the DOZE policy official with 6.x, I have a tablet running 4.4.2 that exhibits similiar behaviour. The "dozing" process is rather buggy. I've notice that apps in the background have all sort of failures. The tablet has few "location" errors, but unless I plug the tablet in it will shutdown the wifi. Note, this was not the case in previous tablets. I do have a way to turn off this behaviour, but it is hidden in the settings.

Perhaps you have a setting on this device?

Best of Luck
Jesse
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
True that. Parts of the Stamina settings in my Sony tablet were activated. I'll see if anything changes with everything on. Thanks for pointing that out.
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
Xperia Z3 is simply deactivating GPS when the app is not in focus, but most devices work with background mode activated, so I have to consider it good enough. Android 6 probably changes that, but that's for another day.
Photo of JesseMonroy650 (Volunteer)

JesseMonroy650 (Volunteer), Champion

  • 3325 Posts
  • 122 Reply Likes
@Anders,
Thanks I added your comments to my notes, should other people have similar issues.
Thanks Again
Jesse
Photo of gg121212

gg121212

  • 1 Post
  • 1 Reply Like
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Very interesting. Thanks
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
I've informed Steven Gill.
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
Thanks for proof. Now I can defend the app behavior.
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
The fun continues:

https://developers.google.com/web/upd...

I haven't tested if that completely breaks geolocation for PhoneGap/Cordova. I hope not.
Photo of Anders Borg

Anders Borg

  • 329 Posts
  • 12 Reply Likes
It still works with Chrome-based WebView, that's at version 50.