Garmin-Watchface: How to show Heartrate?

1.7k Views Asked by At

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.

2

There are 2 best solutions below

3
douglasr On BEST ANSWER

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.

1
Paul McDaniel On

That didn't work for me. I had to do this, using currentHeartRate as a property, not a function method.

    var heartRate = null;
    var activity = Activity.getActivityInfo();
    if (activity != null) {
        heartRate = activity.currentHeartRate;
    }

I was able to also test this in the simulator using Simulation->Activity Data. I have not tested it on my real watch yet to see if this also works just while wearing it, and not during an "activity" .