2

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
BNetz
  • 361
  • 4
  • 20

2 Answers2

2

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.

douglasr
  • 1,894
  • 1
  • 23
  • 29
  • Thanks, this is exactly what I needed! – BNetz Jul 16 '19 at 20:59
  • 1
    In case it helps anybody, currentHeartRate isn't a function call, should just be `Activity.getActivityInfo().currentHeartRate`. (Though that just gets me null, so I may still have it wrong.) – gjgjgj Jul 19 '21 at 06:44
  • 1
    Activity.getActivityInfo().currentHeartRate returns correct value while activity is started. While using simulator - yes, it returns null value - i found it while using both simulator and uploading and testing on current device. – Kamil Gryboś Sep 04 '21 at 19:44
0

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" .

Paul McDaniel
  • 1,471
  • 1
  • 7
  • 6