This skill helps Android developers integrate the Asleep SDK for sleep tracking functionality...
The Asleep Android SDK wraps audio capture, upload, and session management into a single library. It records ambient sound via the device microphone, streams audio to Asleep servers for analysis, and returns sleep stage classifications in real time and as a final report. The SDK manages its own foreground service to keep tracking alive in the background.
| Requirement | Value |
|---|---|
| minSdk | 24 (Android 7.0) |
| targetSdk | 34 |
| Java | 17 |
| Kotlin | 1.9.24+ |
dependencies {
implementation 'com.squareup.okhttp3:okhttp:4.11.0'
implementation 'com.google.code.gson:gson:2.10'
implementation 'ai.asleep:asleepsdk:3.1.4'
}
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<!-- Android 13+ -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
Asleep.initAsleepConfig(
context: Context,
apiKey: String,
userId: String?,
baseUrl: String?,
callbackUrl: String?,
service: String?,
asleepConfigListener: AsleepConfigListener
)
Initializes the SDK configuration. Pass null for userId to have the server generate one (returned in onSuccess). The callbackUrl is an optional webhook endpoint. The service string identifies your app in analytics.
AsleepConfigListener
onSuccess(userId: String?, asleepConfig: AsleepConfig?) -- SDK ready. Store the asleepConfig for tracking calls and persist userId for future sessions.onFail(errorCode: Int, detail: String) -- Initialization failed. Check API key validity and network connectivity.Asleep.beginSleepTracking(
asleepConfig: AsleepConfig,
asleepTrackingListener: AsleepTrackingListener,
notificationTitle: String,
notificationText: String,
notificationIcon: Int,
notificationClass: Class<*>
)
Starts audio capture and opens a sleep session. Automatically creates a foreground service with the provided notification parameters. notificationClass is the Activity opened when the user taps the notification.
AsleepTrackingListener
onStart(sessionId: String) -- Tracking session opened. Store sessionId if needed.onPerform(sequence: Int) -- Called approximately every 30 seconds. Each call represents one sequence (one audio chunk uploaded).onFinish(sessionId: String?) -- Tracking stopped and session closed. Final report available via API.onFail(errorCode: Int, detail: String) -- Error during tracking. See Error Codes below.Asleep.endSleepTracking()
Stops audio capture, closes the session, and tears down the foreground service.
Asleep.isSleepTrackingAlive(context: Context): Boolean
Returns true if the tracking foreground service is currently running. Use this on app resume to reconnect UI to an active session.
Asleep.getCurrentSleepData(
asleepSleepDataListener: AsleepSleepDataListener
)
Fetches preliminary sleep stage data for the current session. Only available after sequence 10; call every 10 sequences for updated results.
AsleepSleepDataListener
onSleepDataReceived(session: Session) -- Contains sleepStages and snoringStages arrays with classifications up to the current point.onFail(errorCode: Int, detail: String) -- Data fetch failed.The SDK progresses through these states during its lifecycle:
| State | Description |
|---|---|
STATE_IDLE |
No configuration loaded |
STATE_INITIALIZING |
initAsleepConfig called, waiting for response |
STATE_INITIALIZED |
Config ready, can start tracking |
STATE_TRACKING_STARTING |
beginSleepTracking called, service launching |
STATE_TRACKING_STARTED |
Audio capture active, sequences incrementing |
STATE_TRACKING_STOPPING |
endSleepTracking called, finalizing session |
STATE_ERROR |
Error occurred, check error code |
The SDK automatically manages a foreground service during tracking. Key behaviors:
beginSleepTracking is called and stops when endSleepTracking completes.notificationTitle, notificationText, notificationIcon, notificationClass) control what the user sees in the notification shade.endSleepTracking() in onDestroy(). The service is designed to outlive the Activity.Preliminary sleep data becomes available after sequence 10. Best practice is to check every 10 sequences (i.e., when sequence > 10 && sequence % 10 == 0 in the onPerform callback). Data includes sleep stage and snoring classifications up to the current point. This data is preliminary and may differ from the final processed report.
| Code | Description |
|---|---|
ERR_MIC_PERMISSION |
Microphone permission revoked or unavailable |
ERR_AUDIO |
Audio capture failed (hardware or OS conflict) |
ERR_INVALID_URL |
Base URL or callback URL malformed |
ERR_COMMON_EXPIRED |
API key or session expired |
ERR_UPLOAD_FORBIDDEN |
Upload rejected (e.g., same user_id active on another device) |
ERR_UPLOAD_NOT_FOUND |
Session not found on server |
ERR_CLOSE_NOT_FOUND |
Attempted to close a non-existent session |
| Code | Description |
|---|---|
ERR_AUDIO_SILENCED |
Microphone receiving silence (blocked or muted) |
ERR_AUDIO_UNSILENCED |
Microphone resumed receiving audio after silence |
ERR_UPLOAD_FAILED |
Temporary upload failure (will retry automatically) |
isSleepTrackingAlive(context) returns true, reconnect your UI to the active tracking session (re-register listeners).Request permissions sequentially, not in parallel:
Show rationale before each request. Handle denial by guiding the user to app settings.
getCurrentSleepData.user_id is actively tracking on another device. Use unique user IDs per device or check for active sessions before starting.apiKey parameter to initAsleepConfig.https://api.asleep.ai