Rafael Muhamedzyanov
2 min readApr 28, 2021

--

Photo byThe Creative Exchange on Unsplash

Sleep tracking with mobile phone

I needed to add sleep tracking to CoachMe app. An app that tracks activity and provides you with helpful messages to keep you healthy.

Mobile phone became essential tool in our lives. We carry it wherever we go. Modern phones has multiple builtin sensors that can help collect data that I need. With this in mind I decided that phone can provide all required data to predict users activity.

My first version of sleep tracking was based completely on user apps usages in phone. By using UsageStatsManager I can get apps open and close times. To predict when user went to sleep I need to find big intervals during night time when user has not used his phone. After finding these intervals I just sum their times to get total predicted sleep time.

Of course this method is very limited and it can’t cover many cases. At the beginning of this 2021 year, Google released Sleep Api. I tested it on my phone, and it gave me wrong sleep intervals. Beside intervals this Api also can send periodical signals with sleep prediction. Signal includes sleep confidence rate, motion level and light level. I found this kind of information very useful. I used collected signals to refine my sleep intervals. This approach gave me more accurate results.

Here I will list cases that I tried to cover with my sleep tracking. Target user has standard sleeping time, somewhere between 9pm and 9am. He cary his phone mostly with him, periodically using it. User also uses phone alarm to wake up in the mornings.

In case user is sleeping during daytime or evenings, or if user is not using his phone very often tracking also will give wrong results.

--

--