iPhone Collecting Coremotion Data in the Background. (Longer Than 10 Mins)

iPhone collecting CoreMotion data in the background. (longer than 10 mins)

Use this one:

Apps that keep users informed of their location at all times, such as a navigation app

In other words, you make a location manager and tell it to start doing updates. You don't have to do anything with those updates! But as long as this is happening - that is, as long as your app is continuing to do location updates in the background - your app is also allowed to use Core Motion in the background. This is not just a trick; it is official Apple policy as explained in one of the WWDC videos from a couple of years ago.

Continuous accelerometer updates while in the background on iOS

Considering this was asked 3 years ago, I'm going to try and give a response applicable to 2017. If you are using Swift I would strongly suggest using this framework https://github.com/MHaroonBaig/MotionKit.

It makes it very trivial to use CoreMotion services. There is probably another friendly wrapper for Core Location services.

I found another SO question that seems to have answered this using your approach of hijacking location services to stay active in the background.

Run app for more than 10 minutes in background

Hopefully this helps. I'll just leave a word of caution that using location services to get continuous accelerometer data in the background without actually needing the location services would probably be grounds to have your app rejected from the app store. Navigation apps genuinely use both location and accelerometer data. The simple answer would be to add a feature that actually uses the location to justify getting the accelerometer data in the background. Anyway, best of luck.

Device motion updates in background

After doing more research, I figured out the problem. I was using mpmusicplayercontroller to play background audio. This will play the audio in the background, but this does not keep my app running in the background. When I switched to using avplayer to play audio, the app runs in the background including the device motion updates.

Detecting when someone begins walking using Core Motion and CMAccelerometer Data

According to your logs, accelerometerUpdateInterval is about 0.02. Updates could be less frequent if you change mentioned property of CMMotionManager.

Checking only x-acceleration isn't very accurate. I can put a device on a table in a such way (let's say on left edge) that x-acceleration will be equal to 1, or tilt it a bit. This will cause a program to be in walking mode (x > 0.1) instead of idle.

Here's a link to ADVANCED PEDOMETER FOR SMARTPHONE-BASED ACTIVITY TRACKING publication. They track changes in the direction of the vector of acceleration. This is the cosine of the angle between two consecutive acceleration vector readings.

cos(angle) formula

Obviously, without any motion, angle between two vectors is close to zero and cos(0) = 1. During other activities d < 1. To filter out noise, they use a weighted moving average of the last 10 values of d.

WMA10 formula

After implementing this, your values will look like this (red - walking, blue - running):

WMA(d)

Now you can set a threshold for each activity to separate them. Note that average step frequency is 2-4Hz. You should expect current value to be over the threshold at least few times in a second in order to identify the action.

Another helpful publications:

  • ERSP: An Energy-efficient Real-time Smartphone Pedometer (analyze peaks and throughs)
  • A Gyroscopic Data based Pedometer Algorithm (threshold detection of gyro readings)

UPDATE

_acceleration.x, _accelaration.y, _acceleration.z are coordinates of the same acceleration vector. You use each of these coordinates in d formula. In order to calculate d you also need to store acceleration vector of previous update (with i-1 index in formula).

WMA just take into account 10 last d values with different weights. Most recent d values have more weight, therefore, more impact on resulting value. You need to store 9 previous d values in order to calculate current one. You should compare WMA value to corresponding threshold.



Related Topics



Leave a reply



Submit