suspendfunreadSleepSessions(healthConnectClient:HealthConnectClient,startTime:Instant,endTime:Instant){valresponse=healthConnectClient.readRecords(ReadRecordsRequest(SleepSessionRecord::class,timeRangeFilter=TimeRangeFilter.between(startTime,endTime)))for(sleepRecordinresponse.records){// Retrieve relevant sleep stages from each sleep recordvalsleepStages=sleepRecord.stages}}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Track sleep sessions\n\n\u003e This guide is compatible with Health Connect version [1.1.0-alpha11](/jetpack/androidx/releases/health-connect#1.1.0-alpha11).\n\nHealth Connect provides a *sleep session* data type, to store information about\na user's sleep, such as a nightly session or daytime nap.\nThe `SleepSessionRecord` data type is used to represent these sessions.\n\nSessions allow users to measure time-based performance over a period of time,\nsuch as continuous heart rate or location data.\n\n`SleepSessionRecord` sessions contain data that records sleep stages, such as\n`AWAKE`, `SLEEPING` and `DEEP`.\n\n**Subtype** data is data that \"belongs\" to a session and is only meaningful when\nit's read with a parent session. For example, sleep stage.\n\nFeature availability\n--------------------\n\nThere is no feature availability flag for this data type.\n\nRequired permissions\n--------------------\n\nAccess to sleep sessions is protected by the following permissions:\n\n- `android.permission.health.READ_SLEEP`\n- `android.permission.health.WRITE_SLEEP`\n\nDeclare these permissions in the Play Console for your app, as well as in your\napp's manifest: \n\n \u003capplication\u003e\n \u003cuses-permission\n android:name=\"android.permission.health.READ_SLEEP\" /\u003e\n \u003cuses-permission\n android:name=\"android.permission.health.WRITE_SLEEP\" /\u003e\n ...\n \u003c/application\u003e\n\nYou are responsible for declaring all the appropriate permissions you intend to\nuse in your devices and apps. You should also check that each permission has\nbeen granted by the user before use\n\nGeneral guidance\n----------------\n\nHere are some best practice guidelines on how to work with sleep sessions in\nHealth Connect.\n\n- Sessions should be used to add data from a specific sleep session, for sleep:\n\n suspend fun writeSleepSession(healthConnectClient: HealthConnectClient) {\n healthConnectClient.insertRecords(\n listOf(\n SleepSessionRecord(\n startTime = Instant.parse(\"2022-05-10T23:00:00.000Z\"),\n startZoneOffset = ZoneOffset.of(\"-08:00\"),\n endTime = Instant.parse(\"2022-05-11T07:00:00.000Z\"),\n endZoneOffset = ZoneOffset.of(\"-08:00\"),\n title = \"My Sleep\"\n ),\n )\n )\n }\n\n- Sessions should **not** be used for general measurements, such as daily step counts.\n- Subtype data doesn't contain a UID, but associated data has distinct UIDs.\n- Subtype data needs to be aligned in a session with sequential timestamps that don't overlap. Gaps are allowed, however.\n- Sessions are useful if the user wants data to be associated with (and tracked as part of) a session, rather than recorded continuously.\n\nSleep sessions\n--------------\n\nYou can read or write sleep data in Health Connect. Sleep data is displayed as a\nsession, and can be divided into 8 distinct sleep stages:\n\n- `UNKNOWN`: Unspecified or unknown if the user is sleeping.\n- `AWAKE`: The user is awake within a sleep cycle, not during the day.\n- `SLEEPING`: Generic or non-granular sleep description.\n- `OUT_OF_BED`: The user gets out of bed in the middle of a sleep session.\n- `AWAKE_IN_BED`: The user is awake in bed.\n- `LIGHT`: The user is in a light sleep cycle.\n- `DEEP`: The user is in a deep sleep cycle.\n- `REM`: The user is in a REM sleep cycle.\n\nThese values represent the type of sleep a user experiences within a time range.\nWriting sleep stages is optional, but recommended if available.\n\n### Write sleep sessions\n\nThe `SleepSessionRecord` data type has two parts:\n\n1. The overall session, spanning the entire duration of sleep.\n2. Individual stages during the sleep session such as light sleep or deep sleep.\n\nHere's how you insert a sleep session without stages: \n\n SleepSessionRecord(\n title = \"weekend sleep\",\n startTime = startTime,\n endTime = endTime,\n startZoneOffset = ZoneOffset.UTC,\n endZoneOffset = ZoneOffset.UTC,\n )\n\nHere's how to add stages that cover the entire period of a sleep session: \n\n val stages = listOf(\n SleepSessionRecord.Stage(\n startTime = START_TIME\n endTime = END_TIME,\n stage = SleepSessionRecord.STAGE_TYPE_SLEEPING,\n )\n )\n\n SleepSessionRecord(\n title = \"weekend sleep\",\n startTime = START_TIME,\n endTime = END_TIME,\n startZoneOffset = START_ZONE_OFFSET,\n endZoneOffset = END_ZONE_OFFSET,\n stages = stages,\n )\n\n### Read a sleep session\n\nFor every sleep session returned, you should check whether sleep stage data is\nalso present: \n\n suspend fun readSleepSessions(\n healthConnectClient: HealthConnectClient,\n startTime: Instant,\n endTime: Instant\n ) {\n val response =\n healthConnectClient.readRecords(\n ReadRecordsRequest(\n SleepSessionRecord::class,\n timeRangeFilter = TimeRangeFilter.between(startTime, endTime)\n )\n )\n for (sleepRecord in response.records) {\n // Retrieve relevant sleep stages from each sleep record\n val sleepStages = sleepRecord.stages\n }\n }\n\n### Delete a sleep session\n\nThis is how to delete a session. For this example, we've used a sleep session: \n\n suspend fun deleteSleepSession(\n healthConnectClient: HealthConnectClient,\n sleepRecord: SleepSessionRecord,\n ) {\n val timeRangeFilter = TimeRangeFilter.between(sleepRecord.startTime, sleepRecord.endTime)\n healthConnectClient.deleteRecords(SleepSessionRecord::class, timeRangeFilter)\n }\n\n| **Note:** Deleting a session does not automatically delete data associated with that session."]]