How to get current Hearth Rate on Watch Faces?

Former Member
Former Member
I developed a quick Watch Face and want to display the current Hearth Rate exactly same way as Hearth Rate widget.

I used this code to achieve that:

var HRH=Act.getHeartRateHistory(1, true);
var HRS=HRH.next();
var HRSnow=HRS.heartRate;

if(HRSnow!= Act.INVALID_HR_SAMPLE){
heartRateText = HRSnow;
}

The problem with this is that it refreshes (only) each minute, so it's not accurate at all. Is there a way to get last Hearth Rate calculated by device and get it refreshed every second?

  • On some devices. you can use Activity.Info.currentHeartRate.

    I check that first, and if it's null, use the newest sample from ActivityMonitor.getHeartRateHistory.
  • Former Member
    Former Member over 7 years ago
    I tried to use Activity.Info.currentHeartRate but I keep getting 4 squares and nothing more. Am I calling it properly?

    var test = Activity.Info.currentHeartRate;
    heartRateText = heartRateText + " " + test;

    Is the fenix 5s compatible?
  • Activity.getInfo().currentHeartRate
  • Former Member
    Former Member over 7 years ago
    Activity.getInfo().currentHeartRate


    Activity.getInfo().currentHeartRate don't work!

    The Console says:

    Could not find symbol getInfo.Failed invoking <symbol>Symbol Not Found Error
    in onUpdate (D:\Development\eclipse\Concise\source\conciseView.mc:214)


  • Typo. Should be:

    Activity.getActivityInfo().currentHeartRate
  • Former Member
    Former Member over 7 years ago
    Typo. Should be:

    Activity.getActivityInfo().currentHeartRate


    Thank you for the help, here is how I do it now:

    // ----------------------- Hearth Rate -----------------------------

    var heartRateText = "";
    var currentHeartRate = Activity.getActivityInfo().currentHeartRate;

    if(currentHeartRate){

    heartRateText = currentHeartRate;
    dc.drawText(width/2, height/1.45 , font_symbol,heartRateText, Gfx.TEXT_JUSTIFY_CENTER);

    }else{

    var HRH=Act.getHeartRateHistory(1, true);
    var HRS=HRH.next();
    var HRSnow=HRS.heartRate;

    if(HRSnow!= Act.INVALID_HR_SAMPLE){
    heartRateText = HRSnow;
    dc.drawText(width/2, height/1.45 , font_symbol,heartRateText, Gfx.TEXT_JUSTIFY_CENTER);
    }

    }

  • You can clean this up a bit. in the If(currentHeartRate) you probably should do !=null.

    Also, you can eliminate a bit if code with something like:
    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) {
    dc.drawText(width/2, height/1.45 , font_symbol,heartRate.toString(), Gfx.TEXT_JUSTIFY_CENTER);
    }

    (there might be typos..)
    get Activity.Info.currentHeartRate (the best source), and if it's null, check history, and if it's ok, use it, and then if you have a heart rate, show it.
    (no duplicate drawTexts, one less var, and making sure in drawText it's a string. I tend to do things like null check HRS too)
  • Do not forget on this:

    var am = ActivityMonitor.getInfo();
    if (am has :getHeartRateHistory) {
    // code
    }

    Because if the device does not support heart rate history, this code can fail.
  • Even with the code examples above, I am not able to get HeartRate in my Watchface:

    class MaSch5WatchFaceView extends Ui.WatchFace
    {
    ....
    function onUpdate(dc) {
    var currentHeartRate;
    currentHeartRate ="xx";
    if (am has :getHeartRateHistory) {
    var appInfo = Activity.getActivityInfo();

    if (appInfo != null)
    {
    if (appInfo.currentHeartRate() != null)
    {
    currentHeartRate = appInfo.currentHeartRate();
    System.println("currentHeartRate" + currentHeartRate.toString());
    }
    }
    }


    I get an exception on appInfo.currentHeartRate() (testing with simulator for Fenix 5
    In the debugger, I see that currentHeartRate is null.
    Thanks for anyhelp
  • Here's the basic flow of what you want to do:
    ----
    HR= Activity.Info.currentHeartrate

    if it's null and if getHeartRateHistory is available:
    get the HR from newest sample (which may be INVALID_HR_SAMPLE

    if HR is still null or invalid, HR="--";
    -----
    so check Activity, and if the HR is null, try to get it from history

    in the sim, Activity.Info.currentHeartRate will be null unless you use simulation and simulate data or play back a .fit with HR data

    on a real device, it will be null if you're not wearing the watch, and the newest sample will be invalid for the same reason.