Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I am not getting excercise distance and calories burned #173

Open
sahil10C opened this issue Oct 17, 2024 · 5 comments
Open

I am not getting excercise distance and calories burned #173

sahil10C opened this issue Oct 17, 2024 · 5 comments

Comments

@sahil10C
Copy link

I am using react native health connect i want to get the data of activity that i have done in the day

I am not able to get the distance covered in particular activity and also not getting the calories that is burned in particular activity

Screenshot 2024-10-17 at 4 43 21 PM I am only getting this much of data previously i was using react native google fit and in that i was able to get the distance and calories burned as well

react-native-health-connect version - 3.2.1

@taisuke-j
Copy link
Contributor

Are you able to show more details on the query you're making?

@sahil10C
Copy link
Author

sahil10C commented Oct 21, 2024

This is the code query that i am making 

const result = await readRecords("ExerciseSession", {
  timeRangeFilter: {
    operator: "between",
    startTime: selectedDate.startOf("day").toJSDate().toISOString(),
    endTime: selectedDate.endOf("day").toJSDate().toISOString(),
  },
});
const results = result?.records;
console.log("resulttttttttt", JSON.stringify(results));
Screenshot 2024-10-21 at 7 27 50 AM Screenshot 2024-10-21 at 7 28 11 AM Screenshot 2024-10-21 at 7 27 59 AM

this first screenshot of the response that i am getting from react native health connect it doest include distacne and calories

where as the screenshot is of the response that i am getting from google fit its giving me distance, time , and also calories can i get the same in react native health connect as well?

@raulzc3
Copy link

raulzc3 commented Oct 25, 2024

I found a "workaround" (it might be the permanent fix depending if they change this).
GoogleFit exercise calories are given in TotalCaloriesBurned and distance is fiven in Distance, you can find both with the startTime and endTime of the exercise session.

For calories:

  async getActivityCalories(startTime, endTime) {
    try {
      const response = await readRecords('TotalCaloriesBurned', {
        timeRangeFilter: {
          operator: 'between',
          startTime: startTime,
          endTime: endTime,
        },
        ascendingOrder: false,
      });

      const activityRecord = response.records[0];
      console.log(activityRecord);
      const activityKcal = activityRecord?.energy?.inKilocalories || 0;
      return Math.round(activityKcal);
    } catch (error) {
      console.error(
        'Error reading TotalCaloriesBurned from Health Connect:',
        error,
      );
      return null;
    }
  }

For distance:

async getActivityDistance(startTime, endTime) {
    try {
      const response = await readRecords('Distance', {
        timeRangeFilter: {
          operator: 'between',
          startTime: startTime,
          endTime: endTime,
        },
        ascendingOrder: false,
      });

      const activityRecord = response.records[0];
      console.log('Registro distancia');
      console.log(activityRecord);
      const activityMeters = activityRecord?.distance?.inMeters || 0;
      return Math.round(activityMeters);
    } catch (error) {
      console.error(
        `Error reading Distance fromHealth Connect:`,
        error,
      );
      return null;
    }
  }

Edit: add distance and fix typos

@rousseau634
Copy link

rousseau634 commented Dec 2, 2024

hey @raulzc3 r when I use ActiveCaloriesBurned, I get empty! I'm confused

@matinzd
Copy link
Owner

matinzd commented Dec 2, 2024

Have you all checked if data is present in Google Health Connect app? Are Google Fit and Health Connect sync enabled?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants