SleepSessionRecord


public final class SleepSessionRecord implements Record


Captures the user's sleep length and its stages. Each record represents a time interval for a full sleep session.

All sleep stage time intervals should fall within the sleep session interval. Time intervals for stages don't need to be continuous but shouldn't overlap.

Example code demonstrate how to read sleep session:

import androidx.health.connect.client.readRecord
import androidx.health.connect.client.records.SleepSessionRecord
import androidx.health.connect.client.request.ReadRecordsRequest
import androidx.health.connect.client.time.TimeRangeFilter

val response =
    healthConnectClient.readRecords(
        ReadRecordsRequest<SleepSessionRecord>(
            timeRangeFilter = TimeRangeFilter.between(startTime, endTime)
        )
    )
for (sleepRecord in response.records) {
    // Process each sleep record
}

Summary

Nested types

public final class SleepSessionRecord.Stage

Captures the sleep stage the user entered during a sleep session.

Constants

static final int

The user is awake and either known to be in bed, or it is unknown whether they are in bed or not.

static final int

The user is awake and in bed.

static final int

The user is in a deep sleep stage.

static final int

The user is in a light sleep stage.

static final int

The user is out of bed and assumed to be awake.

static final int

The user is in a REM sleep stage.

static final int

The user is asleep but the particular stage of sleep (light, deep or REM) is unknown.

static final int

Use this type if the stage of sleep is unknown.

Public fields

static final @NonNull AggregateMetric<@NonNull Duration>

Metric identifier to retrieve the total sleep session duration from androidx.health.connect.client.aggregate.AggregationResult.

Public constructors

SleepSessionRecord(
    @NonNull Instant startTime,
    ZoneOffset startZoneOffset,
    @NonNull Instant endTime,
    ZoneOffset endZoneOffset,
    String title,
    String notes,
    @NonNull List<@NonNull SleepSessionRecord.Stage> stages,
    @NonNull Metadata metadata
)

Public methods

boolean
equals(Object other)
@NonNull Instant

End time of the record.

ZoneOffset

User experienced zone offset at endTime, or null if unknown.

@NonNull Metadata

Set of common metadata associated with the written record.

final String

Additional notes for the session.

final @NonNull List<@NonNull SleepSessionRecord.Stage>
@NonNull Instant

Start time of the record.

ZoneOffset

User experienced zone offset at startTime, or null if unknown.

final String

Title of the session.

int

Constants

STAGE_TYPE_AWAKE

public static final int STAGE_TYPE_AWAKE = 1

The user is awake and either known to be in bed, or it is unknown whether they are in bed or not.

STAGE_TYPE_AWAKE_IN_BED

public static final int STAGE_TYPE_AWAKE_IN_BED = 7

The user is awake and in bed.

STAGE_TYPE_DEEP

public static final int STAGE_TYPE_DEEP = 5

The user is in a deep sleep stage.

STAGE_TYPE_LIGHT

public static final int STAGE_TYPE_LIGHT = 4

The user is in a light sleep stage.

STAGE_TYPE_OUT_OF_BED

public static final int STAGE_TYPE_OUT_OF_BED = 3

The user is out of bed and assumed to be awake.

STAGE_TYPE_REM

public static final int STAGE_TYPE_REM = 6

The user is in a REM sleep stage.

STAGE_TYPE_SLEEPING

public static final int STAGE_TYPE_SLEEPING = 2

The user is asleep but the particular stage of sleep (light, deep or REM) is unknown.

STAGE_TYPE_UNKNOWN

public static final int STAGE_TYPE_UNKNOWN = 0

Use this type if the stage of sleep is unknown.

Public fields

SLEEP_DURATION_TOTAL

public static final @NonNull AggregateMetric<@NonNull DurationSLEEP_DURATION_TOTAL

Metric identifier to retrieve the total sleep session duration from androidx.health.connect.client.aggregate.AggregationResult.

Public constructors

SleepSessionRecord

Added in 1.1.0-alpha07
public SleepSessionRecord(
    @NonNull Instant startTime,
    ZoneOffset startZoneOffset,
    @NonNull Instant endTime,
    ZoneOffset endZoneOffset,
    String title,
    String notes,
    @NonNull List<@NonNull SleepSessionRecord.Stage> stages,
    @NonNull Metadata metadata
)

Public methods

equals

public boolean equals(Object other)

getEndTime

Added in 1.1.0-alpha07
public @NonNull Instant getEndTime()

End time of the record.

getEndZoneOffset

Added in 1.1.0-alpha07
public ZoneOffset getEndZoneOffset()

User experienced zone offset at endTime, or null if unknown. Providing these will help history aggregations results stay consistent should user travel. Queries with user experienced time filters will assume system current zone offset if the information is absent.

getMetadata

Added in 1.1.0-alpha07
public @NonNull Metadata getMetadata()

Set of common metadata associated with the written record.

getNotes

Added in 1.1.0-alpha07
public final String getNotes()

Additional notes for the session. Optional field.

getStages

Added in 1.1.0-alpha07
public final @NonNull List<@NonNull SleepSessionRecord.StagegetStages()

getStartTime

Added in 1.1.0-alpha07
public @NonNull Instant getStartTime()

Start time of the record.

getStartZoneOffset

Added in 1.1.0-alpha07
public ZoneOffset getStartZoneOffset()

User experienced zone offset at startTime, or null if unknown. Providing these will help history aggregations results stay consistent should user travel. Queries with user experienced time filters will assume system current zone offset if the information is absent.

getTitle

Added in 1.1.0-alpha07
public final String getTitle()

Title of the session. Optional field.

hashCode

public int hashCode()