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

Exercise API does not return any results

ANSWERED

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?

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

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.

 

 

Community Council MemberMario Dings | Rotterdam NL
Fitbit: Versa, Versa2, Sense. (Versa light) - Phone: Android. - Developer clockfaces.(Nederlands)

View best answer in original post

Best Answer
0 Votes
2 REPLIES 2

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.

 

 

Community Council MemberMario Dings | Rotterdam NL
Fitbit: Versa, Versa2, Sense. (Versa light) - Phone: Android. - Developer clockfaces.(Nederlands)
Best Answer
0 Votes

Thank you! Adding a setTimeout to give it a few milliseconds worked - would be helpful if the example in the docs included this.

Best Answer
0 Votes