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.