React-native-background-geolocation: Question about heartbeat behavior

Created on 8 Jul 2020  Â·  12Comments  Â·  Source: transistorsoft/react-native-background-geolocation

Environment

  • Plugin version: 3.7.0
  • Platform: iOS and Android
  • OS version: Android 8 and iOS 13.5
  • Device manufacturer / model: Samsung Galaxy 7 and iPhone 8 Plus
  • React Native version (react-native -v): 0.65.1
  • Plugin config

Expected Behavior (just a question)

Fantastic plugin. Congratulations for your work. We planned to buy the license, but a question came up about Heartbeat. If you can help us, we would be very grateful:

Our app needs to perform different functions at different time intervals, both in the foreground, background and killed (completely closed)

For example, we need to activate a bluetooth device every 60 seconds and send some data to the server.

In addition, we have a panic button and the idea is that, if enabled, the application sends messages to the server every X seconds until the button is turned off. (60s, 120s, etc ... The user sets the interval).

if we kept everything at heartbeat listener, it would work perfectly, however, some functions should always be performed every 60 seconds and others may vary according to the user's choice. In that case, the ideal for us would be to run several heartbeats functions, each with a certain interval. It's possible?

Most helpful comment

You’re free to run your own timer in js code in onLocation. Keep resetting the timer with each location event. If the timeout expires, execute getCurrentPosition

All 12 comments

This is fine on Android but iOS requires preventSuspend: true, which is my own little hack. This feature for iOS is brittle and becoming more difficult to rely on as iOS continues to “turn the screws”.

iOS heartbeat is impossible if the user terminateS the app.

The ios heartbeat mechanism relies on briefly turning location-services on/off every 30s to refresh the time remaining on a background-task. It’s like walking on glass while spinning plates.

iOS has no built-in mechanism to periodically run code at a desired interval when your app is in the background with location-services OFF.

However, once the plugin is in the moving state, where location-services are ON and the plugin recording locations, your app is completely alive in the background.

I understand... now I see how the heartbeat works on iOS. It is an interesting solution.

Our initial idea really was to work with onLocation, but we have no control over its execution interval on iOS, as this varies according to the distance and if speed change, interval change too.

On Android it works well because we can set a fixed interval at locationUpdateInterval.

Is there any way to establish a fixed location update interval on iOS?

For your iOS “panic mode”, you could try setting desiredAccuracy to LOW, execute changePace(true) to manually enter moving state, which turns on location-services. set a very high stopTimeout.

Is there any way to establish a fixed location update interval on iOS?

No. iOS location API CLLocationManager is strictly distance-based.

See api docs Config.disableElasticity to prevent distanceFilter auto-scaling with speed.

You’re free to run your own timer in js code in onLocation. Keep resetting the timer with each location event. If the timeout expires, execute getCurrentPosition

Be sure to use iOS platform-detection for timers so you don’t run unnecessary code for Android.

Nice, i hadn't thought of that...I'll try. Thanks!

You’ll also want to implement background-fetch, already included as a dependency. See its readme.

Chris, I added the setInterval inside the onLocation and it works perfectly in the foreground and background, however, when I completely close the application and it is restarted by the background plugin, for some reason setInterval behaves an unstable way and runs 2 times.

Everything that is running out of it runs correctly, the problem is with setInterval.
The same is true for setTimeout.

The strange thing is that this only happens if app is closed.
Do you have any idea what can cause this?

The strange thing is that this only happens if app is closed.

iOS or Android?

iOS. I haven't tested it on Android yet. I will do that now.

When you terminate an iOS app, everything stops. The device must move at least 200 meters before iOS relaunches your app in the background and tracking resumes.

For Android, you must implement Android Headless Mode for the terminated case.

Was this page helpful?
0 / 5 - 0 ratings