How i get current HeartRate?

Hello Developers :)

can anyone please tell me, how i get the current heartrate every second in the "function onPartialUpdate(dc)" ?
Here's an example https://apps.garmin.com/en-US/apps/03030574-3c6e-484a-9bd8-ce2ca0249651#0
i tried with ActivityMonitor and SensorHistory but something is wrong, it is not the current heartrate.

var sensorIter = getHRIterator();
if (sensorIter != null) {
//System.println("HR-Iter " + sensorIter.next().data);
dc.setColor(HeartRateColor, BackgroundColor);
dc.drawText(25, ycenter+45, LCD30, sensorIter.next().data.format("%03d"), Graphics.TEXT_JUSTIFY_LEFT);
}

function getHRIterator() {
// Check device for SensorHistory compatibility
if ((Toybox has :SensorHistory) && (Toybox.SensorHistory has :getHeartRateHistory)) {
return Toybox.SensorHistory.getHeartRateHistory({});//{:period=>null,:order=>SensorHistory.ORDER_OLDEST_FIRST});
}
return null;
}


Thanks a lot from Germany
Patrick

  • A two part question actually.

    First, to get the HR, you first want to check Activity.Info.currentHeartRate. That will change in "real time" If that's not null, use that.

    As a fallback check currentHeartRateHistory and use the newest sample. (that only changes every 1-2 minutes)

    Now as far as doing it in onPartialUpdate, you want to be careful here if the HR is used along with seconds, etc, as far as where you display it, and when you display it. For mine, I only update HR in onPartialUpdate if it's changed since it was last displayed. So instead of updating HR 60 times a minute, you might only do it 20 times, which helps with the power budget.

    As far as "where", with multiple clip regions, you want to minimize the number of rows, and that's the top row in clip 1 to the bottom row in clip 2. With seconds at the very top of the screen, and HR at the very bottom, you'll exceed the power budget.

    Also, see this thread for some general things about 1hz:

    https://forums.garmin.com/forum/developers/connect-iq/158133-?376929=

  • Thanks.
    I had tested with Activity.Info.currentHeartRate, and it is null on the Sim and the Watch. Forerunner 935(Fenix 5)

    In the watchface "noFrills" changed the heart rate every second.

    Power budget is actual not a Problem :)
    I had a very small clip for seconds and HR.
  • In the sim, you must "simulate data" or play back a .fit (with HR data) for the HR to change. On a real device, it might only change every few seconds.

    Is the problem that you don't see the HR change, or you don't see it change on the screen? If it's the screen, check the clip region is large enough for the HR. Nothing outside the clip reason gets updated, and be sure to clearClip() when you are done.

    using Activity.Info.currentHeartRate is how NoFrills gets the HR it BTW.
  • When the currentheartrate is null then i draw 000. See the picture from my watch. community.garmin.com/.../1411833.jpg
  • Activity.getActivityInfo().currentHeartRate works fine :D

    Thanks
  • This code works for me:
    using Toybox.ActivityMonitor as Act;
    using Toybox.Activity as Acty;

    if (Act has :getHeartRateHistory) {

    var heartRate = Activity.getActivityInfo().currentHeartRate;

    if(heartRate==null) {

    var HRH=Act.getHeartRateHistory(1, true);

    var HRS=HRH.next();




    if(HRS!=null && HRS.heartRate!= Act.INVALID_HR_SAMPLE){

    heartRate = HRS.heartRate;

    }

    }

    if(heartRate!=null) {

    heartRate = heartRate.toString();

    }

    else{

    heartRate = "--";

    }