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

heart rate returning null

ANSWERED

Hi, I am very new to javascript and am not sure what i am doing wrong here. I am having a problem where I am trying to obtain heart rate but it is returning null. I have allowed permission in the package.json files. I have simplified my code significantly just to see if I could get the value back but it is still returning null. My previous code was checking all of the necessary permissions/body presence/display on/etc. Using print statements I was able to verify that all of those worked. I have separated out the below code, and it is still returning null. What I want is to have the heart rate start when the display turns on. Do i need to add the hrm event listener? I was hoping not to need to use that, and just use the display event listener and then to grab the most recent value.

display.onaddEventListener("change", () => {
  const hrm = new HeartRateSensor();
  hrm.start();
  console.log(hrm.heartRate);
});

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

@eclarkie22 wrote:

Hi, I am very new to javascript and am not sure what i am doing wrong here. I am having a problem where I am trying to obtain heart rate but it is returning null. I have allowed permission in the package.json files. I have simplified my code significantly just to see if I could get the value back but it is still returning null. My previous code was checking all of the necessary permissions/body presence/display on/etc. Using print statements I was able to verify that all of those worked. I have separated out the below code, and it is still returning null. What I want is to have the heart rate start when the display turns on. Do i need to add the hrm event listener? I was hoping not to need to use that, and just use the display event listener and then to grab the most recent value.


The heart rate sensor is oddly asynchronous, and you don't want to keep it actively recording if the display is off.

 

so,....

 

you can add a routine to record the heart rate:

var cHR=0;
if (HeartRateSensor) {
  const hrm = new HeartRateSensor();
  hrm.addEventListener("reading", () => {
    cHR=hrm.heartRate
  });
  display.addEventListener("change",()=>{
    if (display.on){
      hrm.start()
      cHR=hrm.heartRate
      if (typeof cHR=="number") cHRZ=user.heartRateZone(cHR)
      else cHR=0
    }else{
      hrm.stop();
    }
  });
  hrm.start();
}

 

 Then you can display it during your normal clock update:

clock.granularity='seconds'
clock.ontick=function (evt) {
  console.log(cHR)  
}

Regards,

Reign

View best answer in original post

Best Answer
0 Votes
2 REPLIES 2

Try addEventListener.

 

Even so, that's an odd structure. You'll be starting hrm whenever the display goes on or off, and you're never stopping the hrm.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

@eclarkie22 wrote:

Hi, I am very new to javascript and am not sure what i am doing wrong here. I am having a problem where I am trying to obtain heart rate but it is returning null. I have allowed permission in the package.json files. I have simplified my code significantly just to see if I could get the value back but it is still returning null. My previous code was checking all of the necessary permissions/body presence/display on/etc. Using print statements I was able to verify that all of those worked. I have separated out the below code, and it is still returning null. What I want is to have the heart rate start when the display turns on. Do i need to add the hrm event listener? I was hoping not to need to use that, and just use the display event listener and then to grab the most recent value.


The heart rate sensor is oddly asynchronous, and you don't want to keep it actively recording if the display is off.

 

so,....

 

you can add a routine to record the heart rate:

var cHR=0;
if (HeartRateSensor) {
  const hrm = new HeartRateSensor();
  hrm.addEventListener("reading", () => {
    cHR=hrm.heartRate
  });
  display.addEventListener("change",()=>{
    if (display.on){
      hrm.start()
      cHR=hrm.heartRate
      if (typeof cHR=="number") cHRZ=user.heartRateZone(cHR)
      else cHR=0
    }else{
      hrm.stop();
    }
  });
  hrm.start();
}

 

 Then you can display it during your normal clock update:

clock.granularity='seconds'
clock.ontick=function (evt) {
  console.log(cHR)  
}

Regards,

Reign

Best Answer
0 Votes