Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuous accelerometer updates while in the background on iOS

I have researched this topic thoroughly before asking the question but need to ensure that I have come to the right conclusions and, if so, I would like help on implementing this as efficiently as possible.

It is essential for my app to receive accelerometer updates every 5 seconds or so and may be more quickly under certain situations; without this feature, its not a very useful app.

So, how to go about achieving this? The basic strategy is to use the method:

[cmMotionManager startDeviceMotionUpdatesToQueue:...] 

with an appropriate deviceMotionUpdateInterval and then to somehow 'make sure' your application never gets suspended ie. you have to implement one of the background modes (e.g. Location, Background Audio, VOIP etc)

This is the strategy used by many applications already on the app store such as Sleep Cycle ("sound recording"), Nike+ and Pacer (sound playback).

(Correct me if there is another way to continuously get accelerometer information)

My problem is that my app won't be recording sound, doesn't need VOIP etc and doesn't require any of the new background fetch/remote notification API in iOS 7. So how to go about making sure I stay active in the background?

Well, since im designing an app that measures movement, its not a far stretch to receive Location information. My question is how to go about implementing location updates in the most efficient way possible given the fact that I don't really need these location updates at all while ensuring that my app is never suspended?

This is what I have at the moment in my app delegate:

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

- (void)applicationWillResignActive:(UIApplication *)application
{
    [self.locationManager startUpdatingLocation];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [self.locationManager stopUpdatingLocation];
}

I have noticed that sometimes for a short while after the time I press the home button on my app, my app becomes suspended - presumably as Core Location tries to get a location fix to deliver to the app.

I would also like to know, once you have been awoken by a location update, how long do you have before the system suspends you again if a new location update doesnt arrive? Will I have to try and receive continuous location updates (navigation style) to ensure I am never suspended?

Please help!

Thanks

like image 203
BYZZav Avatar asked Dec 22 '13 12:12

BYZZav


People also ask

How good is iPhone accelerometer?

The Accelerometer in iOS The iPhone is equipped with accurate accelerometer and gyroscope hardware. It can measure the altitude, rotation rate, and acceleration of your iPhone with high accuracy. Steve Jobs demonstrated the capabilities of these two sensors during the introduction of iPhone 4.

What is iOS accelerometer?

An accelerometer measures changes in velocity along one axis. All iOS devices have a three-axis accelerometer, which delivers acceleration values in each of the three axes shown in Figure 1. The values reported by the accelerometers are measured in increments of the gravitational acceleration, with the value 1.

How do you get the accelerometer data on Apple Watch?

No. Direct access to the Apple Watch sensors (which include the accelerometer) is not possible. As always, if this is something you'd like, please file a request for it at https://bugreport.apple.com. Show activity on this post.


1 Answers

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.

like image 175
Lobsterman Avatar answered Oct 06 '22 08:10

Lobsterman