Collect sensor data in the background

I'm creating an app that uses CMDeviceMotion to collect gyroscope and accelerometer data from the watch and phone respectively. The data collection should be able to last continuously for about 10 minutes. Problem is, the collection stops whenever the screen goes black. What would be the best way to make sure the collection continues in sleep mode? Can I collect data from these sensors in a HKWorkoutSession?


I'm using XCode version 10.2 and Swift 4.2.

Post not yet marked as solved Up vote post of Karonsbo Down vote post of Karonsbo
6.4k views

Replies

I'm not sure about the gyrscope but you can use a CMSensorRecorder to record up to 12 hours of accelerometer data in the background.


https://developer.apple.com/documentation/coremotion/cmsensorrecorder

https://developer.apple.com/documentation/coremotion/cmsensorrecorder/1615987-recordaccelerometer


If there is any API that we can use like the CMSensorRecorder, I would want to know too. But it seems like its only for accelerometer though.

Thank you! It could be an alternative for the accelerometer, but how strange that there isn't a corresponding function for the gyroscope.

I finally got it to work by enabling Background Mode for Workout processing and using HKWorkoutSession. 🙂

Hey, I want to ask if you know any database with background sensors data or something, cause I want to investigate about that but don´t have a iOS phone. Thank you so much :)