Skip to main content

Sleep process

287-2B only

The bracelet monitors the night and computes the sleep itself ("Corsano sleep"). The SDK downloads the bracelet's sleep records, builds one summary per night from them and stores it. You do not run any sleep processing yourself: you read the result.

:::note Breaking change in 2.2.0

Sleep is computed on the bracelet since SDK 2.2.0. The former phone-side Philips/FXC processing and the bundled Philips library are removed, and several APIs moved or changed. See Migrating from 2.1.x at the bottom of this page.

:::

The path in short​

  1. Give the bracelet a plan that records sleep, and set its bedtime and rise time. See Requirements.
  2. Keep the automatic data transfer on. It downloads the sleep records and builds the night once the bracelet has ended its sleep session.
  3. Listen to sleepSummaryUpdated, or read sleepSummaryRepository, to get the night.
  4. Optional: offer a "Stop sleep now" button (Manual Stop sleep) and show the state of today's sleep (Sleep status).
What you wantCallWhere it arrives
A night, as soon as it is savedmanager.sleepSummaryUpdated.addListener(listener)onValueUpdated(model: SleepSummaryModel), once per saved night
A stored nightmanager.sleepSummaryRepository.getByDate("yyyy-MM-dd") { summary -> }Callback: SleepSummaryModel?, null when there is none for that day
Every stored night, or the last onegetAll { summaries -> }, or the suspend getLast()Callback: Array<SleepSummaryModel>?; returned: Result<SleepSummaryModel?>
The bracelet's records (stages and markers)manager.metricSleepUpdated, manager.metricSleepRepositoryList<MetricSleep> after each download; see MetricSleep
Where today's sleep ismanager.getSleepState(dayDate) (suspend)Returned: PhilipsSleepState
End the sleep session nowmanager.sendStopSleep(bleDevice, onSuccess, onError)onSuccess(Long) or onError(Throwable)

The fields of the night are described under SleepSummaryModel. Its key, serverDate, is the day the night ended.

The snippets on this page use:

val manager = DataSdk.getInstance().getManager()
val bleDevice = BleSdk.getInstance().getDevice(address) // address: the bracelet's BLE MAC address

Which thread. metricSleepUpdated is called on the main thread. sleepSummaryUpdated is called on the main thread when the automatic download builds the night, and in your own coroutine when you call processSleepIfNeeded() yourself. The repository callbacks come on the callback scheduler, the main thread by default. See Data events.

val sleepListener = object : ValueUpdated.Listener<SleepSummaryModel> {
override fun onValueUpdated(model: SleepSummaryModel) {
// model.serverDate: the day the night ended, "yyyy-MM-dd"
// model.sleepDuration: REM + light + deep, in seconds
showNight(model)
}
}

manager.sleepSummaryUpdated.addListener(sleepListener)
// When your screen goes away:
manager.sleepSummaryUpdated.removeListener(sleepListener)

Requirements​

  • A 287-2B. The SDK downloads sleep only from a 287-2B.
  • A plan that records sleep. See Set bracelet plan and Bracelet Plans.
  • Bedtime and rise time set on the bracelet, in GMT. The bracelet monitors sleep only around these two times. See Bedtime / risetime for the commands, and Sleep Settings for the monitoring window.
  • The automatic data transfer for the bracelet. See Data transfer.
  • A valid licence. Sleep is not raw data: it needs no raw data type in the licence. While the SDK is locked by the licence check, nothing is downloaded. See Licence Key.

Sleep explained​

Sleep is a complex matter, as we don't all behave the same way during our sleep. The sleep needs to be accurate in different situations:

  • No wake-up during the night.
  • The user wakes up and gets out of bed, for example to go to the bathroom.
  • The user checks their phone as soon as they wake up, or later during the day.

Monitoring period​

The bracelet monitors sleep in a window around the bedtime and the rise time you set, and ends the session in the morning when the user becomes active. Sleep Settings describes this window and when the bracelet stops.

If the user stays in bed and checks their phone, the bracelet may not detect activity, and the session goes on. For that case, put a "Stop sleep now" button in your app: it ends the session, and the night is ready soon after. See Manual Stop sleep.

Sleep analysis​

  1. The bracelet records the night: a sleep stage per interval, and a marker when the user goes to bed, falls asleep, wakes up and gets out of bed.
  2. The automatic download stores these records as MetricSleep (metricSleepUpdated is called), during the night and in the morning.
  3. The bracelet ends the session: activity in the morning, the end of its monitoring window, or Stop sleep.
  4. After each download of the sleep file, while today's state is IS_TODAY_AND_IS_ONGOING, the SDK asks the bracelet whether its session has finished. Once it has, the SDK builds the night from the records (see Sleep session).
  5. The night is saved as a SleepSummaryModel, with its start and end, its one-minute slots of light sleep, deep sleep, REM sleep and awake, and its totals. sleepSummaryUpdated is called.

In the download progress the sleep file is named FileOnWatch.CORSANO_SLEEP. Since 3.0.0 it is downloaded and processed once per download (before, it was downloaded twice). See Progress during a transfer.

Manual Stop sleep​

Sending the stop-sleep command tells the bracelet to end the current sleep session.

manager.sendStopSleep(
bleDevice,
onSuccess = { stopTime -> Log.d("Corsano-SDK", "Sleep stopped at ${Date(stopTime)}") },
onError = { error -> Log.e("Corsano-SDK", "Stop sleep failed: $error") }
)
  • The bracelet must be connected and its bracelet initialization finished. Otherwise onError is called; before the initialization has finished, with a NotInitializedException.
  • While the SDK is locked by the licence check, onError gets an SdkLicenceException. See Licence Key.
  • onSuccess gets the phone's time, in epoch milliseconds, at which the bracelet acknowledged the command. It is called once the SDK has also stored that time; if storing it fails, onError is called instead.
  • The callbacks come on the callback scheduler, the main thread by default.
  • The command only ends the session. The night is built by the downloads that follow, as described under Sleep analysis. To not wait for the next automatic download, start one with triggerDownload.

The command it sends is SetStopSleepRequest (see Bracelet commands).

Moved in 2.2.0: sendStopSleep is now on Manager (manager.sendStopSleep(...)). It was previously manager.sleepProcessUpdater.sendStopSleep(...).

Sleep processing on the app​

From 2.2.0 the sleep session is computed on the bracelet and downloaded, so there is no parsing to run on the phone. The step that turns the downloaded records into nights is:

suspend fun processSleepIfNeeded()

You do not need to call it. The SDK calls it itself after every download of the sleep file, also when the file was empty. It replaces the former manager.sleepProcessUpdater.schedule(...).

It builds nights only when all of these hold, and otherwise returns without doing anything:

  • The last session status the SDK read from the bracelet is FINISHED, and it was read today after 02:00. A FINISHED read between 00:00 and 02:00 does not hold the night back: the SDK asks the bracelet again. The SDK reads it during the download, as described under Sleep analysis.
  • getSleepState(today) is IS_TODAY_AND_IS_ONGOING: no night is saved for today yet.

Calling it yourself is safe (a call while it already runs is skipped), but it reads the same stored status, so it rarely makes a night appear earlier. To get the night sooner, trigger a download instead.

Sleep session​

How the SDK builds a night from the bracelet's records:

  • Markers. The records carry a sleep stage, or one of four markers: 101 went to bed, 102 fell asleep, 103 woke up, 104 got out of bed. See MetricSleep.
  • One night per "got out of bed" marker. The night is looked for in the 14 hours before that marker, and after the previous one. It runs from the "went to bed" marker to the "got out of bed" marker. A session ends before the next "went to bed" marker, so a night the bracelet split into two sessions gives two sessions; see Date for how they become one summary.
  • One-minute slots. Every slot starts at 0 seconds (22:01:00, 22:02:00, …), and so do the start and the end of the night.
  • Awake around the sleep. Every minute up to the "fell asleep" marker and from the "woke up" marker on is awake. The night therefore starts and ends with awake slots, which count in awakeTime.
  • Missing minutes. A gap in the records keeps the last stage for at most 10 minutes; the rest of the gap is awake.
  • Stages of a slot (SleepSlotModel.sleepState): 0 none, 1 awake, 2 light, 3 deep, 4 REM. Read them as SleepSlotModel.sleepType.
  • Totals. sleepDuration is REM + light + deep, in seconds. tranquility counts the wake-ups between the first and the last minute of sleep. sleepNeedDuration is always 7 hours, and performance is sleepDuration against it.
  • Date. serverDate and date are the day the night ended, date at local midnight. There is one summary per day.
  • Sessions of one night. A session that ends on a day that already has a night is joined to it, also when it arrives with a later download: one summary from the first bedtime to the last out-of-bed minute, the minutes between the sessions awake, and the totals, performance and tranquility computed over all of it (getting up counts as one wake-up). A session processed again changes nothing. A night stored by SDK 2.1 or earlier (30-second slots) is replaced by the session, not joined.

:::note Changed in 3.0.0

Up to 2.3.0, sleepDuration was the whole time from going to bed to getting out of bed, awake time included, and performance followed from it. tranquility counted two more wake-ups. A gap of missing minutes took the stage that followed it, so time off the wrist counted as sleep. A night the bracelet split into two sessions gave one long night that ran past the next bedtime. A night that ended in the afternoon had a date at noon. Sleep totals are therefore lower than with 2.3.0 for the same night.

:::

Sleep status​

To know where the sleep of a given day is, for example to show "your night is still being recorded", call:

suspend fun getSleepState(dayDate: Date): PhilipsSleepState
lifecycleScope.launch {
when (manager.getSleepState(Date())) {
PhilipsSleepState.AVAILABLE -> showNight() // read it from sleepSummaryRepository
PhilipsSleepState.IS_TODAY_AND_IS_ONGOING -> showStillRecording()
PhilipsSleepState.NOT_AVAILABLE_WATCH_NOT_WORN -> showNotWorn()
PhilipsSleepState.NOT_AVAILABLE -> showNoSleep()
else -> Unit // not returned since 2.2.0
}
}

Changed in 2.2.0: getSleepState is now a suspend function on Manager taking a single dayDate, returning the state directly. It replaces the callback-based PhilipsSleepUtils.getSleepState(...), which took the repositories and a risetime and delivered the result through an onDone callback.

It reads the phone's database only and does not ask the bracelet. It checks, in this order:

ValueReturned when
AVAILABLEA night is stored for that day (a SleepSummaryModel whose serverDate is dayDate)
NOT_AVAILABLE_WATCH_NOT_WORNNo night is stored, and the day's heart rate has a stretch of 4 hours without a value between midnight and 07:00. The 07:00 is fixed: it is not the rise time you set. Without a heart rate summary for that day (not downloaded yet), this value is not returned
IS_TODAY_AND_IS_ONGOINGNo night is stored, dayDate is today, and the bracelet was not found unworn: the sleep is still being recorded, or not yet downloaded and built
NOT_AVAILABLENo night is stored for a past day, and the bracelet was not found unworn: no sleep was identified
IS_TODAY_AND_IS_PROCESSINGNot returned since 2.2.0. Kept for source compatibility
IS_TODAY_AND_IS_PROCESSING_FROM_PROCESS_NOWNot returned since 2.2.0. Kept for source compatibility
enum class PhilipsSleepState {
NOT_AVAILABLE_WATCH_NOT_WORN,
AVAILABLE,
IS_TODAY_AND_IS_ONGOING,
IS_TODAY_AND_IS_PROCESSING,
IS_TODAY_AND_IS_PROCESSING_FROM_PROCESS_NOW,
NOT_AVAILABLE
}
note

The enum keeps the PhilipsSleepState name for source compatibility even though sleep is now Corsano-computed. Treat it as the sleep state, not a Philips-specific type.

Migrating from 2.1.x​

The 2.2.0 sleep migration removes the app-side Philips processing. If you integrated sleep before 2.2.0, apply these changes.

Removed — delete any use of these:

RemovedReplace with
Manager.sleepProcessUpdater and the SleepProcessUpdater classNothing to schedule: the SDK builds the nights after each download. processSleepIfNeeded() exists if you want to call it
PhilipsSleepUtilsManager.getSleepState(dayDate)
SleepProcessResultRemoved — there is no per-parse result any more
The bundled Philips/FXC libraryNothing — remove any packaging step for it

Moved or changed:

Before (≤ 2.1.x)After (2.2.0)
manager.sleepProcessUpdater.sendStopSleep(bleDevice, …)manager.sendStopSleep(bleDevice, onSuccess, onError)
manager.sleepProcessUpdater.schedule(…)Nothing: the SDK calls manager.processSleepIfNeeded() (suspend) itself
PhilipsSleepUtils.getSleepState(repos…, onDone)manager.getSleepState(dayDate) (suspend, returns the state)

Data model: MetricSleep gained movementLevel, activityCount, hrm and rmssd, and its quality field is deprecated — see MetricSleep.

Tips:

  • The removed symbols are compile errors, so the compiler points you at every call site — fix them one by one against the table above.
  • getSleepState and processSleepIfNeeded are suspend; call them from a coroutine. If you previously drove sleep from the onDone callback, move that logic after the suspend call returns.
  • Sleep processing no longer retries on the phone and there is no .wiff-parsing failure to handle, so any retry/SleepProcessResult handling you had can be deleted rather than ported.
  • Nothing changes in how you read a finished sleep: SleepSummaryModel via sleepSummaryRepository, and MetricSleep via metricSleepRepository, are unchanged apart from the new MetricSleep fields. The totals of a night changed in 3.0.0, see Sleep session.