Sleep Cycle SDK - Android Documentation
Overview
The Sleep Cycle SDK for Android enables developers to integrate advanced sleep analysis capabilities into their applications. The SDK provides real-time sleep tracking using audio and motion sensors, delivering detailed sleep insights and events throughout the night.System Requirements
Minimum Android API Level:- Min SDK: API level 28 (Android 9.0 Pie)
- Compile SDK: API level 36
- Kotlin Version: 1.9+ (JVM target 11)
- The SDK is written in Kotlin and provides a Kotlin-first API
Installation
Find the latest version on Maven Central.Groovy DSL
Add the Sleep Cycle SDK dependency to yourbuild.gradle:
Kotlin DSL
Add the Sleep Cycle SDK dependency to yourbuild.gradle.kts:
Prerequisites
Permissions
The SDK requires microphone access for audio-based sleep analysis:Keeping the analysis active using a foreground service
To ensure continuous sleep analysis throughout the night, you must implement a foreground service. This prevents Android from terminating the analysis process during extended periods. It is up to the host app to start the foreground service correctly. The service must declare appropriate foreground service types in its manifest to specify what system resources it needs access to. For sleep analysis, you’ll typically need thehealth or microphone service types, which grant access to health sensors and microphone respectively.
General
It’s typical to callinitialize() during application startup, often in your Application subclass’s onCreate() method. This ensures the SDK is ready when your activities need it. The SDK maintains its state across configuration changes.
The SDK is thread safe and can be called from any thread.
Initialize the SDK
The SDK requires authentication before use. The initialization process validates your credentials and determines available features.Start a sleep analysis session
Once initialized, you can start a sleep analysis session:dataSource: Optional custom data source. Whennull, the SDK uses live device sensors (microphone and accelerometer)config: Configuration object specifying which sensors to use
Stop a session
To stop an active analysis session and retrieve the results:Real-time events
The SDK provides real-time event updates during analysis through a Flow API:Audio clips
The SDK can capture short audio recordings when specific sleep events are detected, such as snoring, sleep talking, or coughing. To use audio clips, create an audio clips producer and pass it tostartAnalysis:
AudioClip contains the audio samples as a FloatArray, the event type, start time, and sample rate.