Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Fit API, getting calories burned per activity

So I'm trying to create an app that connects to Google Fit and shows the user their data in a pretty streamlined way and I'm having trouble finding the calories the user burned for each individual activity per day. I can get the total calories for the entire day, and each activity the user did each day, but not the calories burned for each activity.

Link to GitHub: https://github.com/drb56/FitTest I've only added the java code not any of the xml stuff. And the Google Fit code is in the FitTestFragment.java. I'll paste some key code down below:

Here's where I connect to the google fit API client:

mClient = new GoogleApiClient.Builder(getContext())
            .addApi(Fitness.HISTORY_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
            .addConnectionCallbacks(this)
            .enableAutoManage(getActivity(), 0, new GoogleApiClient.OnConnectionFailedListener() {
            @Override
            public void onConnectionFailed(ConnectionResult result) {
                Log.i("MyApp", "Google Play services connection failed. Cause: " +
                        result.toString());
        }})
        .build();

Here's where I make the DataReadRequest for specific information:

DataReadRequest readRequest = new DataReadRequest.Builder()
                         .aggregate(DataType.TYPE_CALORIES_EXPENDED,     DataType.AGGREGATE_CALORIES_EXPENDED)
                        .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
                        .bucketByTime(1, TimeUnit.DAYS)
                        .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                        .build();

    DataReadResult dataReadResult = Fitness.HistoryApi.readData(mClient, readRequest).await(1, TimeUnit.MINUTES);


    if (dataReadResult.getBuckets().size() > 0)
    {
        Log.i("MyApp", "Number of returned buckets of DataSets is: "
                + dataReadResult.getBuckets().size());
        for (Bucket bucket : dataReadResult.getBuckets())
        {
            List<DataSet> dataSets = bucket.getDataSets();
                     for (DataSet dataSet : dataSets)
                    {
                        dumpDataSet(dataSet);
                    }
                }
             }
            else
            {
                Log.i("MyApp", "No data");
            }

Here's what my output looks like for some activities and calories expended:

Data point:
    Type: com.google.calories.expended
    Date: 06/09/2016
    Start: 2:58:13 PM
    End: 2:58:13 PM
    Field: calories Value: 2555.9749
Data point:
    Type: com.google.activity.summary
    Date: 06/09/2016
    Start: 2:58:13 PM
    End: 2:58:13 PM
    Field: activity Value: 3
    Field: duration Value: 76513626
    Field: num_segments Value: 17
Data point:
    Type: com.google.activity.summary
    Date: 06/09/2016
    Start: 4:13:58 PM
    End: 12:41:04 PM
    Field: activity Value: 7
    Field: duration Value: 4553146
    Field: num_segments Value: 17
like image 474
user292277 Avatar asked Jun 14 '16 19:06

user292277


People also ask

Does Google Fit show calories burned?

Google Fit uses a combination of your activity, your gender, your height, and your weight to estimate how many calories you burned. It's an estimate of total calories burned and includes your basal metabolic rate (BMR), not just calories you burned in your activity.

How does Fit app calculate calories burned?

Calories are calculated using your heart rate information (from Apple Watch, Fitbit, Garmin, or Samsung devices). These devices allow for more accuracy when calculating your workout stats because they combine the rate at which you burn calories at rest and your daily activity to estimate your calories burned.

How do I get data from Google Fit API?

Google Fit is available on Android devices with Google Play services 7.0 or higher. You should install the Google Play Service in your device, and then install the latest client library for Google Play services on your development host. Finally, get an OAuth 2.0 client ID for your Android applications.

Does Google Fit Auto Track exercise?

When you turn on activity tracking, Fit can sense activities like steps and repetitions. If you turn this off, Google Fit can't automatically track your activity. You'll only find info about activity that you enter manually or that's tracked by a connected app. On your Android phone, open the Settings app.


2 Answers

You can get calories for various activities for each day by using the following code

DataReadRequest readRequest = new DataReadRequest.Builder()
            .aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED)
            .bucketByActivityType(1, TimeUnit.SECONDS)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

You can run this code in a loop to get data for as many days as you want. It will return data buckets for all activities and you can fetch activity name from the bucket by this method that returns activity name as String

bucket.getActivity();

Store it in a custom Collection and you are good to go. Hope it helps! :)

like image 173
Arshdeep Singh Saini Avatar answered Oct 04 '22 17:10

Arshdeep Singh Saini


You need to work with Fitness.SESSIONS_API, instead of Fitness.HISTORY_API. Sessions represent a time interval during which users perform a fitness activity.

like image 45
Deepak Azad Avatar answered Oct 04 '22 18:10

Deepak Azad