Add / read weight and height in Google Fit? Android

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); 
+6
source share
3 answers

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

I created a method to get a DataSet with the necessary parameters for the 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); 

This is very useful if you are reading a Google Fit Doc . There you can read more information about data types.

Hope this helps ^^

+6
source
 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. 
0
source

To insert weight and height into the updated GoogleFit API, use the following method:

 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 follows:

 insertWeightHeight(getContext(), DataType.TYPE_WEIGHT, 70f); insertWeightHeight(getContext(), DataType.TYPE_HEIGHT, 1.75f); 
0
source

Source: https://habr.com/ru/post/1206870/


All Articles