Q:

How to determine if the iPhone User is Sleeping?

I want to do a sleep analysis for the user in my app.And I think the CoreMotion Framewrok should help.
func queryActivityStartingFromDate(start: NSDate!, toDate end: NSDate!, toQueue queue: NSOperationQueue!, withHandler handler: CMMotionActivityQueryHandler!)

So I use the API above to get the user motion data in the last 7 days.And Now I get a list of CMMotionActivity Object.
And My question is how to calculate the user sleep status from these thousands of CMMotionActivity Object.Is there any algorithms? Or any other way to achieve sleep analysis.
Many Thanks!

A:

CMMotionActivity includes a stationary property which might be useful in your case. 
Look for contiguous periods of inactivity, paired with location, timezone & CMDeviceMotion data you should begin to detect patterns in the dataset you have. use statistical variation to define thresholds and fine-tune your results.
Caveat, you will make assumptions which might not be true. Some people sleep in moving vehicles for instance.
I found this pdf useful ftp://ftp.tik.ee.ethz.ch/pub/students/2010-HS/SA-2010-26.pdf

