07-24-2020 21:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-24-2020 21:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.

Accepted Solutions
07-26-2020 10:38
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-26-2020 10:38
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
@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

07-24-2020 21:58
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


07-24-2020 21:58
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.
Gondwana Software

07-26-2020 10:38
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-26-2020 10:38
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
@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

