Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

HR Sensor not starting

I have had a few customers contact me about the HR stat not displaying at all. 

From my troubleshooting, I have determined that

  • Permissions are granted
  • The text color is set to something other than the BG color (White on White)
  • All communications are ok between the watch and mobile device. 

It almost seems as if the HRM is not starting, however I am unable to find anything in my code that could cause it. It seems to be happening on 4.2 and not 5.0 so I am unsure if it was a firmware change that could have caused it....

 

I would appreciate a quick review of my HR code below to see if I have missed anything or if the code has been updated which is why I am having issues. 

 

import { display } from "display";
import { HeartRateSensor } from "heart-rate";
import { user } from "user-profile";
import { vibration } from "haptics";
import document from "document";
import * as simpleButtons from "./buttons";

let hrm, watchID, hrmCallback;
let lastReading = 0;

let heartRate;
var lowerHR = document.getElementById("lowerHR");
var upperHR = document.getElementById("upperHR");

export function initialize(callback) {
  hrmCallback = callback;
  hrm = new HeartRateSensor();
  hrm.start();
  lastReading = hrm.timestamp;
  start();
}

function getReading() {
  if (hrm.timestamp === lastReading) {
    heartRate = "--";
  } else {
    heartRate = hrm.heartRate;
  }
  lastReading = hrm.timestamp;
  hrmCallback({
    bpm: heartRate,
    zone: user.heartRateZone(hrm.heartRate || 0),
    restingHeartRate: user.restingHeartRate
  });
    detectHR();  
}

display.addEventListener("change", function() {
  if (display.on) {
    start();
  } else {
    stop();
  }
})

export function start() {
  if (!watchID) {
    getReading();
    watchID = setInterval(getReading, 1000);
  }
}

export function stop() {
  clearInterval(watchID);
  watchID = null;
}


// ***************HR FUNCTIONS*************** \\
function detectHR() {
  let hr = (hrm.heartRate);
//  console.log("Detecting HR")
  if (lowerHR.text == "--" || lowerHR.text.length == 0 || hr == null) {
    return;
  } 
  if (hr != null && hr <= lowerHR.text) {
    console.log("Low HR Detected at " + lowerHR.text + " BPM")
    console.log("Launching Pop-Up Screen")
    vibration.start("nudge-max");    
    simpleButtons.hrDetect();
  }
  if (upperHR.text == "--" || upperHR.text.length == 0 || hr == null) {
    return;    
  }  
  if (hr != null && hr >= upperHR.text){
    console.log("High HR Detected at " + upperHR.text + " BPM")
    console.log("Launching Pop-Up Screen")
    simpleButtons.hrDetect();   
  }
}

 

Best Answer
0 Votes
1 REPLY 1

I can't seem to spot anything out of a quick skim.

I did notice you never do hrm.stop(). This maybe causing an issue with the HRM never stopping and potentially breaking. It maybe a stretch but try putting hrm.stop() at the top of stop()

Best Answer
0 Votes