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

Heart-rate usage problem

Probably something simple...

 

Inside a clock.ontick event, I'm doing

    var hrm = new HeartRateSensor();
    let heartRate = hrm.heartRate;
    ShoHeartRate.text = `${"HeartRate="}${heartRate}`;

but I get 'null' for heartRate when it prints on the screen

What am I doing wrong?

Best Answer
0 Votes
9 REPLIES 9

You need to start measurement for the HR sensor before you read the value.
Also you must wait until the sensor had read the value. You can do it best in the onreading callback.
Here is an example:

// handling HR sensor:
var hrm = new HeartRateSensor();
hrm.onreading = function() { 
console.log("HR="+hrm.heartRate);
ShoHeartRate.text=hrm.heartRate;
}
hrm.onerror = function() { console.log("HR err"); }
hrm.start();

me.onunload = () => {
if (hrm) hrm.stop();
}

Never forget to stop the HR sensor at least when you stop the app (onunload).

 

Best Answer
0 Votes

Please a few more questions because I'm kind of new at this environment. Others might be interested too. And what you're telling me should be in the documentation someplace as well.

 

How long does it take (approximately) to do one reading?

 

Would it work to do the code you suggest (turning the heartrate facility on and off) every second, or does it take longer than that to do a reading so I'd need to do it every 10 seconds or whatever?

 

Can't I do a hrm.stop at the end of the onreading method to turn the sensor off? Or does on/off take a lot of time and so it should be left running until the app doesn't need it anymore?

 

Does the heartrate facility suck a lot of battery, thus I need to turn it off? My code is a clock face which doesn't exit, so is it valid to just leave the heartrate facility on? Or would that cause conflict with other apps? Is there a way to turn it off when I swipe to an app that also does heartrate? Would they conflict?


Thanks!

Best Answer
0 Votes

The HR sensor will only referenced by the API.
A start will increase the ref counter and stop decreases it. But it has no direct effect on real sensor.
The physical sensor is managed by the OS and is supposed to run and record data if you wear the Ionic.

 

If you wear your Ionic, the HR sensor is still running and every API sensor start will only reference the physical sensor and will get the next measurement within one second.
The physical sensor needs some seconds to start up. If you reference it in this situation, you'll get the measurement later than one second. 
If the sensor is off your skin then you'll get no data at all.

Best Answer
0 Votes

But if the OS manages the Heartrate hardware, wouldn't the OS also keep the heartrate number someplace in which case couldn't I just read the OS heart rate record data via some API call directly and immediately? Why do I have to start up another heart rate instance?

Best Answer
0 Votes

Like qooApps said (I think), you're really only registering your interest in receiving HR data; ie, adding your app to the list of processes to which the HR data will be passed.

 

Yes, the HR data is being recorded anyway, but the only way to get access to the default recordings is via the web API, which involves syncing the device first. That approach won't get you real-time data.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Oh, so that means that registering for HeartRate data does not affect the battery drain rate. But wouldn't that mean that the HR data would be immediately available? Or do you mean that my code would be called the next time the OS decides to take a sample and I have to wait for that and get a callback at that time?

Best Answer
0 Votes

Yes, all of that sounds right. In the normal scheme of things, HR callbacks happen about every five seconds (+/-).

 

It would be polite to stop monitoring HR readings when the screen goes off—not because this turns off the HR sensor, but just because there's no point having your app/clockface consume CPU time and battery life when it's got nothing to show for it.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Turning it off makes sense when the clock face is not up, but that one is up most of the time when the Ionic is on.

Is there some way to find out when the clock face is not up or the screen is blank so I can turn off the HR?

Best Answer
0 Votes

https://dev.fitbit.com/build/reference/device-api/display/

 

I don't think you need to worry about what happens when your clockface is closed because an app is being opened. The OS is smart enough to work out that your clockface shouldn't receive any more HR events.

Peter McLennan
Gondwana Software
Best Answer
0 Votes