Is it impossible to calling a particular function periodically in the background?

Code: https://github.com/coitloz88/Garmin-Project/tree/master/BackgroundTest using vscode


Hello! I'm developing part of a project that collects Garmin's sensor data (Accel / HeartRate) in real time and sends it to user's phone.

The goals are:


1. Collect user sensor data in real time in the background of Garmin watch
2. Receive collected data from the mobile phone app connected to the Garmin watch device app
3. Provide feedback to users after performing complex calculations on the mobile phone

However, 2. & 3. were completed, but I had the following problems while doing 1.:

Each background event can occur for a maximum of 30 seconds each time, and must be at least 5 minutes apart.

→ Therefore, while collecting sensor data for 30 seconds when an event can occur in the background, it is stored in the Dictionary var, and the data is transferred to the mobile phone using the transmit() method

I tried to perform the operation by calling the callback function, registerSensorDataListener(), to perform sensor data collection every n(1~4) seconds.

In fact, if I run garmin device app in emulator app, I could see that the sensor data is collected for 30 seconds, then stops, and runs again after 5 minutes.

However, in the debugging output txt file / emulator debug console, it shows that the operation is being performed well when the app is running, but when I leave the app, the operation stops.


To summarize my questions:


1. Can't I call a callback function periodically in the background?

2. Is there any other way to iterate over the background?

3. If you have any other ideas about collecting data in real time in the background and sending it to other devices such as mobile phones, I would appreciate it. (I'm also considering browsing past records with sensor history or sending a .fit file.)


Any advice would be greatly appreciated.

I'm sorry if there were some poor expressions in English.


thank you. :D

  • It looks like you are writing a device app so you have access to the full CIQ APIs.  Since this is launched as a full device app, you don't need or want to use the background process to do the collect and transfer to the phone.  Background task have some major limits that don't work well for real-time data gathering.  Background tasks can only be run once in a 5 minute window and at most for 30 seconds at a time.

    The best way to accomplish what you want is to add your background function as a function within your main view, then use a timer to call a function to orchestrate the different activities you want to run at different intervals.  Here is a rough example

    Timmer calls tasks() once every second 

    Tasks performs the following checks using task specific countdown timers to support different task intervals

    • if dispupdate = 0  run onUpdate and set dispupdate to 60 else subtract 1 from dispupdate
    • if sensorupdate = 0 run  send data to phone function and set sensorupdate to 4 else subtract 1 from sensorupdate
    • Add additional checks for any other interval you want or need.

    Summary the task function runs behind the scenes of the main view, but not as a background task.

    I have used this approach for an app that was used during a tennis tournament to capture and display live HR data for athletes while livestreaming the event.

  • Thank you for your reply! Actually, I should make it run in background because of some reasons TT

    However, I succeed to transfer data in background process using CIQ! I really appreciate your answers, too:)