Reputation: 2835
I want to update calories in google fit api , i also try step count update and step counts are successfully updated by code given by https://developers.google.com/fit/android/history
I edit code for updating calories but unfortunately iam unable to do this
following code i use:
private DataUpdateRequest updateRequest() {
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
cal.add(Calendar.MINUTE, 0);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.MINUTE, -500);
long startTime = cal.getTimeInMillis();
// Create a data source
DataSource dataSource = new DataSource.Builder()
.setAppPackageName(this)
.setDataType(DataType.TYPE_CALORIES_EXPENDED)
.setStreamName(TAG + " - calories")
.setType(DataSource.TYPE_RAW)
.build();
// Create a data set
DataSet dataSet = DataSet.create(dataSource);
// For each data point, specify a start time, end time, and the data value -- in this case,
// the number of new steps.
DataPoint dataPoint = dataSet.createDataPoint()
.setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS);
dataPoint.getValue(Field.FIELD_CALORIES).setFloat(0.0f);
dataSet.add(dataPoint);
Log.i(TAG, "Updating the dataset in the History API.");
DataUpdateRequest request = new DataUpdateRequest.Builder()
.setDataSet(dataSet)
.setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
return request;
}
private class UpdateQuery extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
DataUpdateRequest calorieDataSet=ResetCaloriesCount();
com.google.android.gms.common.api.Status updateStatus =
Fitness.HistoryApi.updateData(mApiClient, calorieDataSet)
.await(1, TimeUnit.MINUTES);
// Before querying the data, check to see if the update succeeded.
if (!updateStatus.isSuccess()) {
Log.i(TAG, "There was a problem updating the dataset.");
}
// At this point the data has been updated and can be read.
Log.i(TAG, "Data update was successful.");
return null;
}
}
I receive success but calories count not reflected.
Note : all data insert by my app as i don't have any other fit app
Upvotes: 1
Views: 1088
Reputation: 13469
Based from this documentation, values for step/distance/active time/calories do not match those of Fit app because of how Google Fit updates are released, it may be possible that the Google Fit app has a more recent version of our data analysis code than Google Play Services, which is released less often.
Our backends, however, always have the most recent version of the data, and everything gets processed there eventually and should match up after a couple of cloud syncs. Let us know if that’s not the case.
We’re trying to make this situation better by making it possible for apps to request that we process a cloud sync instantly so that everything is in sync, and also by making it so that syncs happen automatically when significant data is entered or changes.
This could also be caused by not reading the proper data sources. To match the value of Google Fit, you should be reading data like this:
DataReadRequest readRequest = new DataReadRequest.Builder()
...
.aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED)
...
.build();
Upvotes: 0