Reading sensor data permanently in the background

Hey guys,

I’m trying to develop an app which tries to detect certain states based on sensor data. In particular the proximity sensor and the acceleration sensor. The app should run in the background gather data to analyse the behaviour of the owner of the phone. A little bit like a step counter which tells the user how many steps he has done over the day.

Now I’ve got a few questions about this project and hope some of you guys can help me with that.

  1. What are the limitations of reading sensor data in the background? I already did some tests with an android 8 phone and an iPhone 5s (I think). What I noticed it that the both phones limited the frequency of reading sensor data in the background. I set the frequency to 1 Hz which both did in foreground. But it ended up measuring mostly about once every 10 seconds on Android and once every 2 seconds in on iOS when in background. Is this normal? Can I change this? Can I assume this will stay that way or get even worse?

  2. Will the app be terminated at some stage if not put in foreground for a longer period of time? Is there a way to prevent this? Also I’ve got a notification on my Android phone that my app is running in the background. Is there a way to disable this?

  3. Is there a way to trade accuracy to energy consumption? I don’t need high accuracy but I noticed that the battery drained way faster even with the frequencies mentioned above which aren’t that high.

  4. I only use the acceleration to determine the position of the phone. This can also be achieved by using the W3C Device Orientation API. Is it better to use this one than the Ionic Native (cordova) plugin?

  5. Any general advice what I should consider while developing? I’m currently just doing an prototype to see what Ionic can do with sensor reading in background.

Versions I use:

Cli packages:

@ionic/cli-utils  : 1.19.2
ionic (Ionic CLI) : 3.20.0

global packages:

cordova (Cordova CLI) : 7.0.1

local packages:

@ionic/app-scripts : 3.1.9
Cordova Platforms  : android 7.1.0
Ionic Framework    : ionic-angular 3.9.2


Acceleration sensor: cordova-plugin-device-motion: 2.0.1
Proximity sensor: (master)
Background mode: cordova-plugin-background-mode: 0.7.2

Thank you very much in advance! I really hope I can create the project using Ionic :slight_smile:

1 Like

After a week I’d like to give it a little bump… :slight_smile: