Begin and End Sleep Tracking
For sleep tracking, the app must continuously perform recording, data processing, and network operations throughout the night. However, the Android OS may enter Doze Mode, which can disable microphone and network resources or suspend running processes, imposing various restrictions. As a result, the sleep tracking process may be interrupted.
To address this issue, developers typically use a Foreground Service to separate tasks into a persistent process. However, this approach can be complex and challenging to implement.
To simplify this process, we have abstracted all these complexities within the SDK. Now, developers can easily start and stop sleep tracking by simply calling beginSleepTracking()
and endSleepTracking()
. This reduces the development burden and enables a faster and more stable implementation of sleep tracking functionality.
Additional Guidance for Hardware Control
If hardware control must be guaranteed based on Sleep Stage values during sleep, the built-in SDK functionality may have limitations.
In such cases, you should either:
- Implement control using a Webhook Server.
- Design a custom Foreground Service to ensure uninterrupted operation throughout the night.
Begin SleepTracking
Asleep.beginSleepTracking()
-
This function starts the sleep tracking process, and the following tasks are performed automatically:
- Foreground Service Execution: The Foreground Service starts automatically, ensuring background tasks remain stable.
- Notification Display:
- According to Android system policies, a notification will be displayed in the status bar while the foreground service is running.
- This notification informs the user that sleep tracking is in progress and can support user interaction if needed.
- Start Sleep Tracking:
- The SleepTrackingManager immediately begins the sleep tracking process.
beginSleepTracking(
asleepConfig: AsleepConfig,
notificationClass: Class<*>? = null,
notificationTitle: String? = null,
notificationText: String? = null,
notificationIcon: Int? = null,
asleepTrackingListener: AsleepTrackingListener
)
Parameter | Type | Description |
---|---|---|
asleepConfig | AsleepConfig | Value received from initAsleepConfig . |
notificationClass | Class<*>? | Activity to open when tapping the foreground service notification. |
notificationTitle | String? | Title of the foreground service notification. |
notificationText | String? | Body text of the foreground service notification. |
notificationIcon | Int? | Icon resource for the foreground service notification. |
asleepTrackingListener | Asleep.AsleepTrackingListener | Listener to receive callbacks during sleep tracking |
Asleep.AsleepTrackingListener
- An interface that provides callbacks for the start, end, and status of sleep tracking.
interface AsleepTrackingListener {
fun onStart(sessionId: String)
fun onPerform(sequence: Int)
fun onFinish(sessionId: String)
fun onFail(errorCode: Int, detail: String)
}
Function | Parameter | Type | Description |
---|---|---|---|
onStart | sessionId | String | Called when sleep tracking starts, providing the session ID. |
onPerform | sequence | Int | Called every 30 seconds during analysis, providing a sequence value starting from 0. |
onFinish | sessionId | String | Called when sleep tracking ends, providing the session ID of the completed session. |
onFail | errorCode | Int | Called if an error occurs during the sleep tracking process, returning an errorCode. |
detail | String | Additional description related to the errorCode . |
Termination Based on Error Codes
If the following errors are returned in the errorCode
parameter of onFail()
, sleep tracking is considered unable to continue and will automatically terminate, triggering the onFinish()
callback.
ERR_AUDIO, ERR_CREATE_FAILED, ERR_CREATE_UNAUTHORIZED, ERR_CREATE_CONFLICT, ERR_CREATE_VALIDATION, ERR_CREATE_SERVER_ERROR, ERR_UPLOAD_BAD_REQUEST, ERR_UPLOAD_UNAUTHORIZED, ERR_UPLOAD_FORBIDDEN, ERR_UPLOAD_NOT_FOUND, ERR_UPLOAD_TOO_LARGE, ERR_CLOSE_FAILED, ERR_CLOSE_BAD_REQUEST, ERR_CLOSE_UNAUTHORIZED, ERR_CLOSE_FORBIDDEN, ERR_CLOSE_NOT_FOUND, ERR_CLOSE_SERVER_ERROR
For other errors, sleep tracking will not be terminated, even if temporary issues prevent proper measurement:
ERR_AUDIO_SILENCED, ERR_AUDIO_UNSILENCED, ERR_UPLOAD_FAILED
Asleep.getCurrentSleepData()
- Retrieves real-time sleep data measured so far.
Asleep.getCurrentSleepData(asleepSleepDataListener = object: Asleep.AsleepSleepDataListener {
override fun onSleepDataReceived(session: Session) {
}
override fun onFail(errorCode: Int, detail: String) {
}
})
Asleep.AsleepSleepDataListener
- On success, retrieves the session values through the
onSleepDataReceived()
function. - On failure, returns an
errorCode
through theonFail()
function.
interface AsleepSleepDataListener {
fun onSleepDataReceived(session: Session)
fun onFail(errorCode: Int, detail: String)
}
Asleep.isSleepTrackingProcessAlive()
- This function checks whether sleep tracking is currently in progress.
- If the Foreground Service is running, it returns
true
. - When the app starts, calling this function will determine if sleep tracking is active.
- If
true
is returned, the app should display a UI indicator to inform the user that sleep tracking is in progress.
- If
Asleep.isSleepTrackingProcessAlive(context: Context)
Asleep.connectSleepTracking()
- If
isSleepTrackingProcessAlive()
returnstrue
, it means sleep tracking is running normally. - In this case, re-register the
AsleepTrackingListener
to receive events and data related to the sleep tracking status. - This ensures synchronization between the sleep tracking process and the UI state.
Asleep.connectSleepTracking(asleepTrackingListener: AsleepTrackingListener)
End SleepTracking
Asleep.endSleepTracking()
This function stops the sleep tracking session started with beginSleepTracking()
.
- Calling this function immediately halts sleep tracking.
- The Foreground Service is also terminated, ensuring that all sleep tracking-related processes are safely and completely stopped.
Asleep.endSleepTracking()
Refer to the Sample App for Sleep Tracking using the Begin-End method.
Updated 11 days ago