Error while searching for Google steps
I am using this code to try and extract steps from the past 14 hours.
YApp myApp = (mYApp) ctx;
mGoogleApiClient = myApp.getMyUser();
mGoogleApiClient.reconnect();
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.HOUR_OF_DAY, -16);
long startTime = cal.getTimeInMillis();
PendingResult<DataReadResult> pendingResult =
Fitness.HistoryApi.readData(mGoogleApiClient, new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build());
DataReadResult dataReadResult = pendingResult.await();
DataSet ds = dataReadResult.getDataSet(DataType.AGGREGATE_STEP_COUNT_DELTA);
My datareadresult is returning success, but when I try to read the results in the dataset I get this error
java.lang.IllegalArgumentException: Attempting to read data for com.google.step_count.delta, which was not requested
I tried all DataTypes but got the same error every time, what am I doing wrong ?!
+3
source to share
2 answers
For everyone who gets upset about this :)
mYApp myApp = (mYApp) ctx;
mGoogleApiClient = myApp.getMyUser();
mGoogleApiClient.reconnect();
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.HOUR_OF_DAY, -12);
long startTime = cal.getTimeInMillis();
PendingResult<DataReadResult> pendingResult =
Fitness.HistoryApi.readData(mGoogleApiClient, new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build());
DataReadResult dataReadResult = pendingResult.await();
if (dataReadResult.getBuckets().size() > 0) {
Log.i(TAG, "Number of returned buckets of DataSets is: "
+ dataReadResult.getBuckets().size());
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets();
for (DataSet dataSet : dataSets) {
Log.i(TAG, "Data returned for Data type: " + dataSet.getDataType().getName());
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
for (DataPoint dp : dataSet.getDataPoints()) {
Log.i(TAG, "Data point:");
Log.i(TAG, "\tType: " + dp.getDataType().getName());
Log.i(TAG, "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
Log.i(TAG, "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)));
for(Field field : dp.getDataType().getFields()) {
Log.i(TAG, "\tField: " + field.getName() +
" Value: " + dp.getValue(field));
}
}
}
}
} else if (dataReadResult.getDataSets().size() > 0) {
Log.i(TAG, "Number of returned DataSets is: "
+ dataReadResult.getDataSets().size());
for (DataSet dataSet : dataReadResult.getDataSets()) {
Log.i(TAG, "Data returned for Data type: " + dataSet.getDataType().getName());
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
for (DataPoint dp : dataSet.getDataPoints()) {
Log.i(TAG, "Data point:");
Log.i(TAG, "\tType: " + dp.getDataType().getName());
Log.i(TAG, "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
Log.i(TAG, "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)));
for (Field field : dp.getDataType().getFields()) {
Log.i(TAG, "\tField: " + field.getName() +
" Value: " + dp.getValue(field));
}
}
}
}
+8
source to share
Just to clarify
PendingResult<DataReadResult> pendingResult =
Fitness.HistoryApi.readData(mGoogleApiClient, new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build());
Returns a DataReadResult with a list of codes.
PendingResult<DataReadResult> pendingResult =
Fitness.HistoryApi.readData(mGoogleApiClient, new DataReadRequest.Builder()
.read(DataType.TYPE_STEP_COUNT_DELTA)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build());
Returns a DataReadResult with a DataSet, which can be retrieved using
dataReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);
+4
source to share