03-03-2021 23:46
03-03-2021 23:46
Hello, all
I'm a beginner for development of fitbit application.
Although I could get the current heart-rate data, I could not access the batched heart-rate data.
I referred to Heart Rate Sensor Guide (fitbit.com),but my code did not work.
I tried simple test codes(test1,2,3) to make my question clear.
Test1 and Test2 worked.
I don't know why test3 does not work. The error code was 'Cannot read the property '0' of undefined. '
Any help would be appreciated.
import { HeartRateSensor } from "heart-rate"
import clock from "clock";
/*Test3*/
const hrm = new HeartRateSensor({frequency:1 , batch:3});
hrm.start();
clock.granularity = "seconds";
clock.addEventListener("tick", (evt) => {
console.log(String(hrm.readings.bpm[0]));
});
/*Test2
const hrm = new HeartRateSensor({frequency:1});
hrm.start();
clock.granularity = "seconds";
clock.addEventListener("tick", (evt) => {
console.log(String(hrm.heartRate));
});
*/
/* Test1
const hrm = new HeartRateSensor();
hrm.start();
clock.granularity = "seconds";
clock.addEventListener("tick", (evt) => {
console.log(String(hrm.heartRate));
});
*/
Answered! Go to the Best Answer.
03-05-2021 12:45 - edited 03-05-2021 12:58
03-05-2021 12:45 - edited 03-05-2021 12:58
The error previously pointed out in the heartrate documentation still exists resulting in devs continuing to run into this problem.
https://community.fitbit.com/t5/SDK-Development/Documentation-error-on-HR-docs/m-p/4717521#M14340
I hope the error in the documentation can be corrected soon as it's a minor change.
Edit: As for why you are still getting "undefined" - this should still be a readable value in the simulator, and you can find a slider for heartrate somewhere in the options. But you probably need to make sure permissions are set for this, and you'll want to check for the presence of the heart rate sensor and said permissions in your code. Here's a snippet I'm using:
import { me as appbit } from "appbit";
import { HeartRateSensor } from "heart-rate";
import { display } from "display";
let heartRate = 0;
if (HeartRateSensor && appbit.permissions.granted("access_heart_rate")) {
let heartRateMonitor = new HeartRateSensor({ frequency: 1, batch: 10 });
heartRateMonitor.addEventListener("reading", () => {
//read only the most recent value
heartRate = heartRateMonitor.readings.heartRate[heartRateMonitor.readings.heartRate.length-1];
});
display.addEventListener("change", () => {
// Automatically stop the sensor when the screen is off to conserve battery
display.on ? heartRateMonitor.start() : heartRateMonitor.stop();
});
heartRateMonitor.start();
} else {
console.log("This device does NOT have a HeartRateSensor or permission not granted");
}
You should also consider including the body presence sensor and checking against that for keeping the heart rate monitor active / heart rate displayed (otherwise when you take the watch off it may continue displaying the last reading when that's no longer really valid)
2nd Edit: On a final note, the conversion to String is not necessary for console.log 😉
03-04-2021 00:53
03-04-2021 00:53
Instead of
readings.bpm[
try
readings.heartRate[
I suspect the guide you were looking at contains an error; consider https://dev.fitbit.com/build/reference/device-api/heart-rate/
03-04-2021 02:09
03-04-2021 02:09
Thank you for your quick reply.
Error code disappeared in your advice, but console log still displays 'undefined'.
Is it due to OS simulator ? (I don't have any fitbit device, and waiting for amazon now.)
I'll try in the device.
03-05-2021 12:45 - edited 03-05-2021 12:58
03-05-2021 12:45 - edited 03-05-2021 12:58
The error previously pointed out in the heartrate documentation still exists resulting in devs continuing to run into this problem.
https://community.fitbit.com/t5/SDK-Development/Documentation-error-on-HR-docs/m-p/4717521#M14340
I hope the error in the documentation can be corrected soon as it's a minor change.
Edit: As for why you are still getting "undefined" - this should still be a readable value in the simulator, and you can find a slider for heartrate somewhere in the options. But you probably need to make sure permissions are set for this, and you'll want to check for the presence of the heart rate sensor and said permissions in your code. Here's a snippet I'm using:
import { me as appbit } from "appbit";
import { HeartRateSensor } from "heart-rate";
import { display } from "display";
let heartRate = 0;
if (HeartRateSensor && appbit.permissions.granted("access_heart_rate")) {
let heartRateMonitor = new HeartRateSensor({ frequency: 1, batch: 10 });
heartRateMonitor.addEventListener("reading", () => {
//read only the most recent value
heartRate = heartRateMonitor.readings.heartRate[heartRateMonitor.readings.heartRate.length-1];
});
display.addEventListener("change", () => {
// Automatically stop the sensor when the screen is off to conserve battery
display.on ? heartRateMonitor.start() : heartRateMonitor.stop();
});
heartRateMonitor.start();
} else {
console.log("This device does NOT have a HeartRateSensor or permission not granted");
}
You should also consider including the body presence sensor and checking against that for keeping the heart rate monitor active / heart rate displayed (otherwise when you take the watch off it may continue displaying the last reading when that's no longer really valid)
2nd Edit: On a final note, the conversion to String is not necessary for console.log 😉
03-06-2021 18:10
03-06-2021 18:10
Thank you for your kind lecture.
I'll use your code about permission and existence of Heart-Rate.
The reason why I included 'clock' in my test code was to find the meanings of 'batched'.
Since I set the batch:3, I expected the code below would produce console logs as follows; 'NaN', 'NaN', '60','60',...
(I thought I could get some numbers in '3' seconds.)
import { HeartRateSensor } from "heart-rate";
import clock from "clock";
if (HeartRateSensor && appbit.permissions.granted("access_heart_rate")) {
let heartRateMonitor = new HeartRateSensor({ frequency: 1, batch: 3 });
heartRateMonitor.start();
clock.granularity = "seconds";
clock.addEventListener("tick", (evt) => {
heartRate = heartRateMonitor.readings.heartRate[3];
console.log(heartRate);
});
As a result, I got console logs; NaN,NaN,NaN,......
However, interestingly, I could get valid number, when I changed the heartrate values with the slide bar for '3' times.
I figured out what 'batched' means at least in the OS simulator.
Thank you for your help