05-16-2022 11:26
05-16-2022 11:26
I am new to this, and I'm trying simple snippets of code lifted directly from the documentation. The code snippets for reading from the sensors seem to work fine, but whenever I try to read any stats from the exercise API, it just returns 'undefined'. I have given the app ALL permissions except cluster storage.
Here is my code:
//This block works fine:
import { HeartRateSensor } from "heart-rate";
if (HeartRateSensor) {
console.log("This device has a HeartRateSensor!");
const hrm = new HeartRateSensor();
hrm.addEventListener("reading", () => {
console.log(`Current heart rate: ${hrm.heartRate}`);
});
hrm.start();
} else {
console.log("This device does NOT have a HeartRateSensor!");
}
import { me as appbit } from "appbit";
import exercise from "exercise";
if (!appbit.permissions.granted("access_exercise")) {
console.log("We're not allowed to create exercises!");
}
if (!appbit.permissions.granted("access_heart_rate")) {
console.log("We're not allowed to read a users' heart rate!");
}
if (!appbit.permissions.granted("access_location")) {
console.log("We're not allowed to read a users' location!");
}
//This block does not work:
exercise.start("run", { gps: true });
if (exercise.state === "started") {
console.log('HR from exercise API: ' + exercise.stats.heartRate.current);
exercise.stop();
}
The output from this is shown below (heart rate monitor returns a value when accessed directly, but not from the exercise API after starting an exercise):
[19:18:00]Error 22 Invalid attribute 'data-size' in animate-in [HOST]
[19:18:00]App Startedindex.js:3,1
[19:18:00]Default JS app started [HOST]
[19:18:00]Launch complete - durations: foregrounding(569ms), first paint(1ms), total(586ms).
[19:18:00]Sideload successful ![HOST]
[19:18:00]App Closed [HOST]
[19:18:01]App Started app/index.js:3,4
[19:18:01]This device has a HeartRateSensor! app/index.js:32,4
[19:18:01]HR from exercise API: undefined app/index.js:6,6
[19:18:01]Current heart rate: 70 app/index.js:6,6
[19:18:01]Current heart rate: 70 [HOST]
[19:18:01]Launch complete - durations: foregrounding(44ms), first paint(3ms), total(463ms).
Have I missed a step?
Answered! Go to the Best Answer.
05-24-2022 15:56
05-24-2022 15:56
Give the exercise time to run more than a millisecond.
the last if-statement has no function in this example.
Give exercise.stop a timeout for a minute and repeat the heartrate call every second.
05-24-2022 15:56
05-24-2022 15:56
Give the exercise time to run more than a millisecond.
the last if-statement has no function in this example.
Give exercise.stop a timeout for a minute and repeat the heartrate call every second.
05-25-2022 11:15
05-25-2022 11:15
Thank you! Adding a setTimeout to give it a few milliseconds worked - would be helpful if the example in the docs included this.