Add/Read weight and height in GoogleFit? Android

google fit data types
google fit api
google fit weight history
import weight into google fit
google fit import data
google fit rest api
google fit weight graph explained
google fit data export

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);

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 ^^

Track your weight - Android - Google Fit Help, Add a new weight measurement. On your phone, open the Google Fit app Google Fit . At the bottom right, tap Add Add and  Step 1: Enter your physical information On your phone, open the Google Fit app. At the bottom, tap Profile. Enter your height, weight, age, and gender.

private DataSet createDataForRequest(DataType dataType
,float dataSourceType
,int values
,long startTime, long endTime, TimeUnit timeUnit) {

    DataSource dataSource = new DataSource.Builder()
            .setAppPackageName(this)
            .setDataType(dataType)
            .setType(DataSource.TYPE_RAW)
            .build();

    DataSet dataSet = DataSet.create(dataSource);
    DataPoint dataPoint = dataSet.createDataPoint().setTimeInterval(startTime, endTime, timeUnit);

    float weight = Float.parseFloat(""+values);

    dataPoint = dataPoint.setFloatValues(weight);

    dataSet.add(dataPoint);

    return dataSet;
}

// 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,
        56,                  // weight in kgs
        startTime,              // start time
        endTime,                // end time
        TimeUnit.MINUTES                // Time Unit, for example, TimeUnit.MILLISECONDS
);

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

// Before querying the data, check to see if the insertion succeeded.
if (!weightInsertStatus.isSuccess()) {
    Log.i(TAG, "There was a problem inserting the dataset.");
    return null;
}

// At this point, the data has been inserted and can be read.
Log.i(TAG, "Data insert was successful!");

// I'm getting : There was a problem inserting the dataset.

Add & edit fitness activities - Android, You can manually add activity to your Google Fit activity history or edit an activity that's already there. You can also start challenges, log your weight, and more. You can also add weight changes or activities that Fit doesn't automatically track (like weightlifting, rowing, or baseball). On your Android phone, open the Google Fit app . At the bottom right,

You can use following method to insert Weight and Height in updated GoogleFit API:

private void insertWeightHeight(Context context, DataType dataType, float value) {
        long startTime = Calendar.getInstance().getTimeInMillis();
        long endTime = startTime;
        DataSource dataSource = new DataSource.Builder()
                .setAppPackageName(getContext())
                .setDataType(dataType)
                .setType(DataSource.TYPE_RAW)
                .build();

        DataPoint dataPoint = DataPoint.builder(dataSource)
                .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
                .setFloatValues(value)
                .build();

        DataSet dataSet = DataSet.builder(dataSource)
                .add(dataPoint)
                .build();

        Fitness.getHistoryClient(context, GoogleSignIn.getLastSignedInAccount(context))
                .insertData(dataSet)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        System.out.println("success");
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        System.out.println("failed");
                    }
                });
    }

You can call this method as follow:

insertWeightHeight(getContext(), DataType.TYPE_WEIGHT, 70f);//weight in kg
insertWeightHeight(getContext(), DataType.TYPE_HEIGHT, 1.75f);//height in meter

Body data types | Google Fit, Body fat percentage; Heart rate; Height; Weight https://www.googleapis.com/​auth/fitness.body.read This data type captures that user's height in meters. How to add third-party apps. Google Fit still allows you to share Google Fit data with other apps and devices and that's now done from the Profile tab. Look for the small cog icon in the top right

Data types | Google Fit, Google Fit provides a set of health and wellness data types under the app, you can insert, read, or delete the data from the fitness history with the History API. This weight loss tracker will assist you in weight control and helps you to lose weight or reach your weight goal. The app also includes a BMI Calculator which calculates your BMI and your ideal weight. You can also define your own personal target weight. Use the following features: • Track your daily weight • Integrated BMI Calculator • Analyze your weight history in various charts and

Android Google Fit get basic info (Gender, Height, Weight), I need to get height, weight and gender data from google fit. I could How to differentiate between manual added steps and sensor recorded steps in Google Fit Api How can I read data from all data sources using google fit android sdk? Google Fit will also help you: TRACK YOUR WORKOUTS FROM YOUR PHONE OR WATCH Get instant insights when you exercise and see real-time stats for your runs, walks, and bike rides. 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.

Google Fit guide: Everything you need to know, Read our full Google Fit guide to find out everything you need to Once you tell Google Fit a little about yourself (gender, date of birth, weight, and height), or add an activity, weight measurement, or blood pressure reading. Dan Graziano/CNET Google's health platform, which first debuted on Android Wear watches earlier this year, is now available on the Web and for Android devices running version 4.0 or higher.

Comments
  • I did the same you suggest by following method : Please see my post: any idea why i'm getting : There was a problem inserting the dataset.
  • Try now, there was a typo in the doc.
  • By the way, have you requested permissions for writing in Google Fit API?
  • maybe you didn't add body permissions in your code. I recommend to read this response: stackoverflow.com/questions/26390192/…
  • For me works only dataPoint.getValue(Field.FIELD_WEIGHT).setFloat((Integer)values);