Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add/Read weight and height in GoogleFit? Android

By Google, I got this code to insert DataType.TYPE_STEP_COUNT_DELTA. but how to insert TYPE_HEIGHT AND TYPE_WEIGHT using Android

 com.google.android.gms.common.api.Status insertStatus =
                    Fitness.HistoryApi.insertData(mClient, dataSet)
                            .await(1, TimeUnit.MINUTES);
like image 376
Vinod Joshi Avatar asked Nov 14 '14 12:11

Vinod Joshi


People also ask

Does Google Fit work on Android?

Fit will use your Android phone's sensors or Wear OS by Google smartwatch's heart rate sensors to record your speed, pace, route, and more. See your daily progress on your Heart Points and Steps goal.


1 Answers

To insert data, you need to create a new DataSet object for both height and weight.

I have created a method in order to get a DataSet object with the necessary parameters for a request.

/**
 * This method creates a dataset object to be able to insert data in google fit
 * @param dataType DataType Fitness Data Type object
 * @param dataSourceType int Data Source Id. For example, DataSource.TYPE_RAW
 * @param values Object Values for the fitness data. They must be int or float
 * @param startTime long Time when the fitness activity started
 * @param endTime long Time when the fitness activity finished
 * @param timeUnit TimeUnit Time unit in which period is expressed
 * @return
 */
private DataSet createDataForRequest(DataType dataType, int dataSourceType, Object values,
                                     long startTime, long endTime, TimeUnit timeUnit) {
    DataSource dataSource = new DataSource.Builder()
            .setAppPackageName(mAppId)
            .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;
}

Then, you need to do something like this:

     DataSet weightDataSet = createDataForRequest(
           DataType.TYPE_WEIGHT,    // for height, it would be DataType.TYPE_HEIGHT
           DataSource.TYPE_RAW,
           value,                  // weight in kgs
           startTime,              // start time
           endTime,                // end time
           timeUnit                // Time Unit, for example, TimeUnit.MILLISECONDS
      );

    com.google.android.gms.common.api.Status weightInsertStatus =
            Fitness.HistoryApi.insertData(mClient, weightDataSet )
                    .await(1, TimeUnit.MINUTES);

It is very helpful if you read Google Fit doc. There, you can read more information about data types

I hope this helps ^^

like image 115
José Carlos Avatar answered Sep 22 '22 12:09

José Carlos