Search code examples
garminconnectiq

Garmin-Watchface: How to show Heartrate?


In a watchface I created which ConnectIQ (4.30) I can show the clocktime, but when I try to show the heartrate I get the error

Details: Module 'Toybox.Sensor' not available to 'Watch Face'

Stack: - onStart() at /Users/…/source/_garmin_projectAPP.mc:13 0x10000095 Permission Required

In the manifest I added all available permissions, I also imported the Sensor with

using Toybox.Sensor

I am also not sure where exactly to enable the heartrate-sensor with e.g.

Sensor.setEnabledSensors([Sensor.SENSOR_HEARTRATE]);
Sensor.enableSensorEvents(method(:onSensor));

I tried in the initialize() and onStart(state) method, but still I get the error shown above.


Solution

  • CIQ apps of type "watch face" do not have access to sensors in that manner. Instead you need to use the methods available in the Activity and/or ActivityMonitor modules.

    If the watch device is newer it will likely support calling this method, which returns an heart rate value that is updated every second:

    Activity.getActivityInfo().currentHeartRate()
    

    Otherwise, you can call this method and use the most recent value, which will be the heart rate within the last minute:

    ActivityMonitor.getHeartRateHistory()
    

    In both cases you will need to check for a null value, which will happen if the sensor is not available or the user is not wearing the watch.