Search code examples
androidgoogle-fitgoogle-fit-sdk

Calories expenditure using google fit api


I am working on app fitness app , for that I used google fit api . Till now I am successful in fetching steps count , distance but I am unable to get calorie expenditure . Thanks in Advance


Solution

  • You need to set the user's weight and height first. The expended calories are calculated using this information.

    These are the methods that I use to do this. (mClient is a GoogleApiClient instance)

    public static void saveUserHeight(int heightCentimiters) {
        // to post data
        float height = ((float) heightCentimiters) / 100.0f;
        Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.DAY_OF_YEAR, -1);
        long startTime = cal.getTimeInMillis();
    
        DataSet heightDataSet = createDataForRequest(
                DataType.TYPE_HEIGHT,    // for height, it would be DataType.TYPE_HEIGHT
                DataSource.TYPE_RAW,
                height,                  // weight in kgs
                startTime,              // start time
                endTime,                // end time
                TimeUnit.MILLISECONDS                // Time Unit, for example, TimeUnit.MILLISECONDS
        );
    
        com.google.android.gms.common.api.Status heightInsertStatus =
                Fitness.HistoryApi.insertData(mClient, heightDataSet)
                        .await(1, TimeUnit.MINUTES);
    }
    
    public static void saveUserWeight(float weight) {
        // to post data
        Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.DAY_OF_YEAR, -1);
        long startTime = cal.getTimeInMillis();
    
        DataSet weightDataSet = createDataForRequest(
                DataType.TYPE_WEIGHT,    // for height, it would be DataType.TYPE_HEIGHT
                DataSource.TYPE_RAW,
                weight,                  // weight in kgs
                startTime,              // start time
                endTime,                // end time
                TimeUnit.MILLISECONDS                // Time Unit, for example, TimeUnit.MILLISECONDS
        );
    
        com.google.android.gms.common.api.Status weightInsertStatus =
                Fitness.HistoryApi.insertData(mClient, weightDataSet)
                        .await(1, TimeUnit.MINUTES);
    }
    
    public static DataSet createDataForRequest(DataType dataType,
                                               int dataSourceType,
                                               Object values,
                                               long startTime,
                                               long endTime,
                                               TimeUnit timeUnit) {
        DataSource dataSource = new DataSource.Builder()
                .setAppPackageName(appContext)
                .setDataType(dataType)
                .setType(dataSourceType)
                .build();
    
        DataSet dataSet = DataSet.create(dataSource);
        DataPoint dataPoint = dataSet.createDataPoint().setTimeInterval(startTime, endTime, timeUnit);
    
        if (values instanceof Integer) {
            dataPoint = dataPoint.setIntValues((Integer) values);
        } else {
            dataPoint = dataPoint.setFloatValues((Float) values);
        }
    
        dataSet.add(dataPoint);
    
        return dataSet;
    }