Sleep API
chevron down
 

Sleep API

Variable: sleep

Type: Sleep

Sleep

String: ISleep | undefined

The Sleep API allows applications to determine whether the user is currently sleeping or not, and be notified when the user goes to sleep and wakes up.

This API provides a reliable update mechanism which is automatically deactivated when the device display is off.

The access_sleep permission must be granted, else this module will be empty.

import { me as appbit } from "appbit";

if (!appbit.permissions.granted("access_sleep")) {
   console.log("We're not allowed to read a users' sleep!");
}

import sleep from "sleep";

if (sleep) {
  sleep.addEventListener("change", () => {
     console.log(`User sleep state is: ${sleep.state}`);
   });
} else {
   console.warn("Sleep API not supported on this device, or no permission")
}

Interface: ISleep

The Sleep object describes the current sleep state of the user and provides events that report changes in sleep state.

Properties

onchange

((this: Sleep, event: Event) => any) or undefined

Event handler for the change event emitted by the sleep service.

readonly state

"unknown" or "awake" or "asleep"

Describes whether the user is asleep or not, or if the current sleep state is not known.