- Android 8.0 (API level 26) or higher
- Java 1.8 or higher
- Android Gradle plugin 8.0 or higher
The API key is required to use the Asleep SDK.
For how to issue an API key, see this link Generate API key
- Create a project using Android Studio.
- Add AsleepSDK.aar file in the [root]/app/libs folder of the generated project folder.
- Open the AndroidManifest.xml file to add permissions.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
...
📘 The REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission is not required.
However, it can be helpful if you have the appropriate permissions to prevent you from falling into Doze mode.
https://developer.android.com/training/monitoring-device-state/doze-standby
📘 The FOREGROUND_SERVICE permission may be required when developing the app .
If you want the app you want to develop to work even when the user is sleeping, you must use this feature.
https://developer.android.com/guide/components/foreground-services
- To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.
plugins {
...
}
android {
...
}
dependencies {
...
implementation 'org.jetbrains.bio:npy:0.3.5'
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
implementation 'com.google.code.gson:gson:2.9.1'
implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
}
- Write the source code for obtaining permission in the appropriate location.
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), 0)
Get AsleepConfig by initAsleepConfig.
Enter apiKey, userId(null if nothing was previously received), baseUrl(address of proxy server, null if not), callbackUrl(address where the session receives the analyzed results), and service(nickname for the app you want to develop).
If you do not have a userId, create a new one and return it from onSuccess(), and if you enter an existing userId, it will be validated and returned by onSuccess().
If the existing userId is in tracking, exit it from inside the SDK.
import ai.asleep.asleepsdk.Asleep
import ai.asleep.asleepsdk.data.AsleepConfig
import ai.asleep.asleepsdk.AsleepErrorCode
...
Asleep.initAsleepConfig(
context = applicationContext,
apiKey = "[input your apiKey]",
userId = "",
baseUrl = null,
callbackUrl = "",
service = "[input your AppName]",
object : Asleep.AsleepConfigListener {
override fun onSuccess(userId: String?, asleepConfig: AsleepConfig?) {
...
/* save userId and asleepConfig */
}
override fun onFail(errorCode: Int, detail: String) {
...
}
})
From initAsleepConfig, Enter AsleepConfig as a parameter.
import ai.asleep.asleepsdk.tracking.SleepTrackingManager
...
var sleepTrackingManager = Asleep.createSleepTrackingManager(asleepConfig, object : SleepTrackingManager.TrackingListener {
override fun onCreate() {
}
override fun onUpload(sequence: Int) {
}
override fun onClose(sessionId: String) {
...
/* save sessionId */
}
override fun onFail(errorCode: Int, detail: String) {
}
})
- Start Recording
When you start, the sequence number is called back once every 30 seconds from the onUpload function of the listener that was registered when the SleepTrackingManager was created.
sleepTrackingManager?.startSleepTracking()
- Analyze during Recording
sleepTrackingManager?.requestAnalysis(object : SleepTrackingManager.AnalysisListener {
override fun onSuccess(session: Session) {
Log.d("", "${session.toString()}")
}
}
for session information, reference API>Data API>Get a session
When you stop, the sessionId is called back from the onClose function of the listener registered at the time of the SleepTrackingManager creation.
sleepTrackingManager?.stopSleepTracking()
From initAsleepConfig, enter AsleepConfig as a parameter.
val reports = Asleep.createReports(asleepConfig)
- Receive one SessionID result
enter sessionID when sleepTracking is stopped as a parameter
reports?.getReport(sessionId, object : Reports.ReportListener {
override fun onSuccess(report: Report?) {
}
override fun onFail(errorCode: Int, detail: String) {
}
})
- Get results by date
val today = LocalDate.now()
reports?.getReports(today.minusDays(7).toString(), today.toString(), "DESC", 0, 20, object : Reports.ReportsListener {
override fun onSuccess(reports: List<SleepSession>?) {
}
override fun onFail(errorCode: Int, detail: String) {
}
})
- Deleting recording history
Enter SessionID to delete the corresponding recording history.
reports?.deleteReport(sessionId, object : Reports.DeleteReportListener {
override fun onSuccess(sessionId: String?) {
}
override fun onFail(errorCode: Int, detail: String) {
}
})
- Deleting all my information
Deletes the userId and all measured records. The deleted userId is no longer available.
Asleep.deleteUser(object : Asleep.DeleteUserIdListener {
override fun onSuccess(userId: String?) {
}
override fun onFail(errorCode: Int, detail: String) {
}
})
- Available from v2.3.0
In the Android OS, the Google Play Services package sometimes updates, which can lead to the system forcibly terminating and restarting an app's foreground service. To address this, functionality is provided to continue the operation of the sleepTrackingManager.
Firstly, when the foreground service is restarted by the system, the LifecycleService()'s onStartCommand function is called.
At this time, if there is an unfinished session as determined by the Asleep.hasUnfinishedSession function, the AsleepConfig stored inside the AsleepSDK can be retrieved. A new sleepTrackingManager is then created, and startSleepTracking is called to continue tracking.
private var asleepConfig: AsleepConfig? = null
private var sleepTrackingManager: SleepTrackingManager? = null
...
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
if(Asleep.hasUnfinishedSession(applicationContext)) {
asleepConfig = Asleep.getSavedAsleepConfig(applicationContext, BuildConfig.ASLEEP_API_KEY)
sleepTrackingManager = Asleep.createSleepTrackingManager(asleepConfig, object : SleepTrackingManager.TrackingListener {
override fun onCreate() {
...
}
override fun onUpload(sequence: Int) {
...
}
override fun onClose(sessionId: String) {
...
}
override fun onFail(errorCode: Int, detail: String) {
...
}
}
...
sleepTrackingManager?.startSleepTracking()
}
...
}