AsleepConfig

Initialize Config

Asleep.initAsleepConfig()

  • Initialize the SDK.
initAsleepConfig(
    context: Context,
    apiKey: String,
    userId: String?,
    baseUrl: String?,
    callbackUrl: String?,
    service: String?,
    asleepConfigListener: Asleep.AsleepConfigListener,
    asleepLogger: AsleepLogger?
)
Parameter NameTypeDescription
contextContextEnter ApplicationContext
apiKeyStringEnter the value issued by Generate API key
userIdString?Enter null if there is no initial userId, and enter the userId that has been issued since
baseUrlString?If null, use the default value, enter the proxy server address
callbackUrlString?Enter the url of the server to receive sleep session analysis results
serviceString?your app name
asleepConfigListenerAsleepConfigListenerlistener to receive callback for userId
asleepLoggerAsleepLogger?A listener for receiving SDK log callbacks

Token authentication

If you called setup with app credentials, call initAsleepConfig with the same credentials.

Asleep.initAsleepConfig(
    context = applicationContext,
    appId = "YOUR_APP_ID",
    appSecret = "YOUR_APP_SECRET",
    userId = userId,
    asleepConfigListener = asleepConfigListener
)
ParameterTypeDescription
appIdString?App ID issued from the dashboard
appSecretString?App Secret issued from the dashboard
isTestEnvironmentBoolean?Set to true only when using the test environment. Default null

The remaining parameters (userId, baseUrl, callbackUrl, service, asleepConfigListener, asleepLogger) are the same as the API Key method.

Pass the same credentials when resuming a session that was interrupted.

val config = Asleep.getSavedAsleepConfig(context, appId, appSecret)

Asleep.ConfigListener

interface AsleepConfigListener {
    fun onSuccess(userId: String?, asleepConfig: AsleepConfig?)
    fun onFail(errorCode: Int, detail: String)
}
  • If success, onSuccess()is called.
Parameter NameTypeDescription
userIdString?Newly issued or entered userId
asleepConfigAsleepConfig?Required set value to use the SDK
  • If failure, onFail() is called.
Parameter NameTypeDescription
errorCodeIntAsleepErrorCode
detailStringerrorCode Message

Asleep.AsleepLogger

interface AsleepLogger {
  fun d(tag: String, msg: String, throwable: Throwable? = null)
  fun e(tag: String, msg: String, throwable: Throwable? = null)
  fun i(tag: String, msg: String, throwable: Throwable? = null)
  fun w(tag: String, msg: String, throwable: Throwable? = null)
}
  • Inside the SDK, it is called when d(debug), e(error), i(info), and w(warn) occur, respectively.
Parameter NameTypeDescription
tagString"[Asleep SDK]"
msgStringlog message
throwableThrowable?A class that represents problems that can occur during SDK execution.

Set User ID

Asleep.setUserId()

  • Sets the userId the SDK uses directly. The value you set is used in the SDK's subsequent operations.
Asleep.setUserId(userId: String)
Parameter NameTypeDescription
userIdStringThe userId to set

Preferred Mic

Asleep.setPreferredMicSelector()

  • Injects the microphone selection policy used for recording. It must be set before calling createSleepTrackingManager() / beginSleepTracking() to apply to that session.
  • When set, the SDK switches to STEREO capture so the preferred microphone selection can be trusted (devices that cannot do STEREO fall back to MONO, in which case the selection may be ignored depending on the device). When null, it works as before with MONO capture and the system's default routing.
  • The SDK keeps the reference, so pass null to release it when it is no longer used.
Asleep.setPreferredMicSelector(selector: PreferredMicSelector?)
Parameter NameTypeDescription
selectorPreferredMicSelector?Microphone selection policy. null returns to the default behavior

Delete User

Asleep.deleteUser()

  • Delete all data of the user, including userId.
fun deleteUser(deleteUserIdListener: DeleteUserIdListener?)
Parameter NameTypeDescription
deleteUserIdListenerDeleteUserIdListener?listener to receive callback for deleted userId

Asleep.DeleteUserIdListener

interface DeleteUserIdListener {
    fun onSuccess(userId: String?)
    fun onFail(errorCode: Int, detail: String)
}
  • If success, onSuccess()is called.
Parameter NameTypeDescription
userIdString?deleted userId
  • If failure, onFail() is called.
Parameter NameTypeDescription
errorCodeIntSee AsleepErrorCode
detailStringerrorCode Message

Continue Session

Asleep.hasUnfinishedSession()

  • After the foreground service is forcibly terminated and restarted, it checks if there is any session that has not been terminated.
hasUnfinishedSession(context: Context): Boolean
Parameter NameTypeDescription
contextContext?Enter ApplicationContext

Asleep.getSavedAsleepConfig()

🚧

Deprecated — getSavedAsleepConfig() has been renamed to restoreAsleepConfig(). The behavior is the same. Use restoreAsleepConfig() below in new code.

  • After the foreground service is forcibly terminated and restarted, it retrieves the previously saved asleepConfig.
getSavedAsleepConfig(context: Context, apiKey: String): AsleepConfig?
Parameter NameTypeDescription
contextContext?Enter ApplicationContext
apiKeyStringEnter the value issued by Generate API key

Asleep.restoreAsleepConfig()

  • The new name of getSavedAsleepConfig(). The behavior is identical to getSavedAsleepConfig(): after the foreground service is forcibly terminated and restarted, it retrieves the previously saved asleepConfig.
  • Overloads are provided for the API Key method and the Token method (appId and appSecret).
restoreAsleepConfig(context: Context, apiKey: String): AsleepConfig?
restoreAsleepConfig(context: Context, appId: String, appSecret: String): AsleepConfig?
Parameter NameTypeDescription
contextContext?Enter ApplicationContext
apiKeyStringEnter the value issued by Generate API key
appIdStringApp ID issued from the dashboard (Token method)
appSecretStringApp Secret issued from the dashboard (Token method)



User Metadata

Save and retrieve the user's date of birth, gender, height, and weight. These are called as methods of the AsleepConfig received from initAsleepConfig.

asleepConfig.saveUserMetadata()

val metadata = UserMetadata(
    birthYear = 1990,
    birthMonth = 5,
    birthDay = 21,
    gender = UserMetadata.Gender.FEMALE,
    height = 165.5f,
    weight = 55.2f
)

asleepConfig?.saveUserMetadata(metadata, object : Asleep.SaveUserMetadataListener {
    override fun onSuccess(userInfo: UserInfo) { }
    override fun onFail(errorCode: Int, detail: String) { }
})
ParameterTypeDescription
metadataUserMetadataUser information to save
listenerSaveUserMetadataListenerlistener to receive the result. On success, the updated UserInfo is delivered

asleepConfig.fetchUserMetadata()

asleepConfig?.fetchUserMetadata(object : Asleep.FetchUserMetadataListener {
    override fun onSuccess(metadata: UserMetadata?) {
        // null if there is no saved information
    }

    override fun onFail(errorCode: Int, detail: String) { }
})
ParameterTypeDescription
listenerFetchUserMetadataListenerlistener to receive the result. onSuccess(null) if there is no saved information

Listener

interface SaveUserMetadataListener {
    fun onSuccess(userInfo: UserInfo)
    fun onFail(errorCode: Int, detail: String)
}

interface FetchUserMetadataListener {
    fun onSuccess(metadata: UserMetadata?)
    fun onFail(errorCode: Int, detail: String)
}

UserMetadata

data class UserMetadata(
    val birthYear: Int? = null,
    val birthMonth: Int? = null,
    val birthDay: Int? = null,
    val gender: Gender? = null,
    val height: Float? = null,
    val weight: Float? = null
)

enum class Gender { MALE, FEMALE, NON_BINARY, OTHER, PREFER_NOT_TO_SAY }
PropertyTypeDescription
birthYearInt?Year of birth (1900~2100)
birthMonthInt?Month of birth (1~12)
birthDayInt?Day of birth (1~31)
genderGender?Gender
heightFloat?Height in cm (1~500). Saved to one decimal place (the rest is truncated)
weightFloat?Weight in kg (1~1000). Saved to one decimal place (the rest is truncated)
🚧

If you pass a value that is out of range, UserMetadata throws IllegalArgumentException at construction time. This is not a listener callback, so if you pass user input directly, validate it at the input stage or wrap it in try-catch.

  • Every property is optional. Properties that are null are not included in the request.
  • The combination of month and day (for example, February 31) is not validated.

UserInfo

Delivered when saveUserMetadata succeeds. The metadata field has been added since v3.3.0.

data class UserInfo(
    val createdTimezone: String?,
    val nickName: String?,
    val toBeDeleted: Boolean?,
    val userId: String?,
    val metadata: UserMetadata? = null
)

Error Codes

CodeNameDescription
27000ERR_SAVE_METADATA_FAILEDFailed to save (network · server error, userId not set, SDK not initialized)
27100ERR_FETCH_METADATA_FAILEDFailed to fetch (network · server error, userId not set, SDK not initialized)
27400ERR_SAVE_METADATA_VALIDATIONThe server rejected the value (400). detail contains the reason

Did this page help you?