Help users understand their sleep habits

The Sleep API, a library powered by Google Play services allows apps to determine when the user goes to sleep and wakes up.

After receiving permission from the user, Google Play services collects information related to surrounding brightness, device movement, and more to infer the times when the user falls asleep and wakes up. Your app can subscribe to updates to this information. That way, your app can inform users about their sleep habits and help encourage users to improve their sleep hygiene and overall well-being.

Before you begin

To prepare your app, complete the steps in the following sections.

App prerequisites

Make sure that your app's build file uses the following values:

  • A minSdkVersion of 29 or higher.
  • A compileSdkVersion of 29 or higher.
.

Configure your app

In your project-level build.gradle file, include Google's Maven repository and Maven central repository in both your buildscript and allprojects sections:

 buildscript {
    repositories {
        google()
        mavenCentral()
    }
} 

allprojects { repositories { google() mavenCentral() } }

Add the Google Play services dependency for the Sleep API to your module's Gradle build file, which is commonly app/build.gradle:

 dependencies {
    implementation 'com.google.android.gms:play-services-location:23.2.0'
} 

Add the ACTIVITY_RECOGNITION permission to your AndroidManifest.xml tag with android:name=”android.permission.ACTIVITY_RECOGNITION”.

 <manifest>
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<application>...</application>
</manifest>

Register for sleep updates

Prior to registering for updates, first check that the user has granted the ACTIVITY_RECOGNITION permission. For more information about permissions, see Request App Permissions.

Once permission has been granted, register for updates to the user's sleep behavior, including sleep segments and sleep event classification results, by calling requestSleepSegmentUpdates().

 val task = ActivityRecognition.getClient(context)
    .requestSleepSegmentUpdates(
        pendingIntent,
        SleepSegmentRequest.getDefaultSleepSegmentRequest())
    .addOnSuccessListener {
        viewModel.updateSubscribedToSleepData(true)
        Log.d(TAG, "Successfully subscribed to sleep data.")
    }
    .addOnFailureListener { exception ->
        Log.d(TAG, "Exception when subscribing to sleep data: $exception")
    }

Learn more about the Sleep API

Your app can retrieve information about sleep time from the daily sleep segment update event.

Each sleep segment event contains information about whether the API detected sleep or could detect sleep. The segment event also includes the times when the user most likely fell asleep and woke up, based on available sensor data.

Your app can also receive regular updates about sleep classification events.

Each sleep classification event provides a timestamp, along with values that represent device motion, environment brightness, and the likelihood that the user is asleep at that time. For example, if the sleep confidence significantly increases between one timestamp and the next timestamp, and if the surrounding brightness significantly decreases between those same 2 timestamps, then it's likely that the user recently fell asleep.

Your app can combine this information with additional user-provided data to more confidently determine when the user falls asleep and wakes up.

The SleepSampleKotlin sample app demonstrates an end-to-end workflow using the Sleep API.