07-24-2020 21:36
07-24-2020 21:36
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);
});
Answered! Go to the Best Answer.
Best Answer07-26-2020 10:38
07-26-2020 10:38
@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 Answer07-24-2020 21:58
Gold Fitbit Product Experts share support knowledge on the forums and advocate for the betterment of Fitbit products and services. Learn more
07-24-2020 21:58
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.
Best Answer07-26-2020 10:38
07-26-2020 10:38
@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