03-03-2018 16:29 - edited 03-03-2018 20:42
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-03-2018 16:29 - edited 03-03-2018 20:42
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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?

03-03-2018 21:29
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-03-2018 21:29
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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).

03-04-2018 07:26
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-04-2018 07:26
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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!

03-04-2018 10:39
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-04-2018 10:39
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.

03-04-2018 16:00
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-04-2018 16:00
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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?

03-04-2018 19:26
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


03-04-2018 19:26
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.
Gondwana Software

03-04-2018 20:11
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-04-2018 20:11
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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?

03-04-2018 20:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


03-04-2018 20:40
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.
Gondwana Software

03-05-2018 10:19
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-05-2018 10:19
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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?

03-05-2018 17:52
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


03-05-2018 17:52
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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.
Gondwana Software

