Heart-rate API
chevron down
 

Heart-rate API

Variable: HeartRateSensor

Type: [IHeartRateSensor | undefined]

Interface: IHeartRateSensor

The HeartRateSensor API provides access to the heart rate data measured by the hardware sensor.

A Heart Rate Monitor (HRM) measures the rate of a person's heartbeat.

You must request the access_heart_rate permission in order to use this sensor.

See the Heart Rate Sensor Guide for further details.

import { HeartRateSensor } from "heart-rate";

if (HeartRateSensor) {
   console.log("This device has a HeartRateSensor!");
   const hrm = new HeartRateSensor();
   hrm.addEventListener("reading", () => {
     console.log(`Current heart rate: ${hrm.heartRate}`);
   });
   hrm.start();
} else {
   console.log("This device does NOT have a HeartRateSensor!");
}

Properties

readonly activated

boolean

Flag that indicates if the sensor is activated or not. When a sensor is created, the sensor is not activated, thus the initial value of this property equals false.

onactivate

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

Event handler that is called when the sensor is activated.

onerror

((this: Sensor, event: SensorErrorEvent) => any) or undefined

Event handler that is called when an error occurs. When an error occurs, the sensor is automatically stopped, and the activated property equals false.

onreading

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

Event handler that is called whenever a new reading is available.

readonly readings

BatchedHeartRateSensorReading or undefined

New in SDK 2.0

Interface: BatchedHeartRateSensorReading

New in SDK 2.0

Properties

readonly heartRate

Float32Array

readonly timestamp

Uint32Array

Interface: HeartRateSensorReading

Real-time measurement from the Heart Rate Monitor (HRM) sensor.

Properties

readonly heartRate

number or null

Heart rate in bpm (beats per minute)

readonly timestamp

number or null

Timestamp of the reading in milliseconds.

NOTE: this is relative to an unspecified arbitrary 0 time, or null if no reading is available (when the sensor is not yet activated and there are no valid cached values that can be used).