It is probably a newbie question but I have lost a one day to figure out w/o success. I'm using Google Fit API into my Android app and I need to show some of its data like running and walking daily steps. I have managed to show data but in time unit (f.e running in x min). I need to show it in steps unit.
The snipped below code shows how I retrieved data as time unit (in milliseconds):
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(start, end, TimeUnit.MILLISECONDS)
.build();
Fitness.HistoryApi.readData(client, readRequest).setResultCallback(new ResultCallback<DataReadResult>() {
@Override
public void onResult(DataReadResult dataReadResult) {
if (dataReadResult.getBuckets().size() > 0) {
display.show("Bucket DataSet.size(): "
+ dataReadResult.getBuckets().size());
retrieveHistoryData(dataReadResult, client.getContext());
}
}
.....
private void retrieveHistoryData(DataReadResult dataReadResult, Context context) {
int walking = 0;
int running = 0;
int biking = 0;
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets();
for (DataSet dataSet : dataSets) {
display.show("dataSet.dataType: " + dataSet.getDataType().getName());
for (DataPoint dp : dataSet.getDataPoints()) {
walking += getBucketData(dp, FitnessActivities.WALKING);
running += getBucketData(dp, FitnessActivities.RUNNING);
biking += getBucketData(dp, FitnessActivities.BIKING);
}
}
}
Log.d(TAG, Constants.KEY_WALKING_STEPS + ": " + walking);
Log.d(TAG, Constants.KEY_RUNNING_STEPS + ": " + running);
Log.d(TAG, Constants.KEY_BIKING_STEPS + ": " + biking);
}
private int getBucketData(DataPoint dp, String activityName) {
int count = 0;
for (Field field : dp.getDataType().getFields()) {
Log.d(TAG, "Field " + field.getName() + " " + FitnessActivities.getName(dp.getValue(field).asInt()) + " = " + dp.getValue(field).asInt() + " field.describeContents()" + field.describeContents());
if ("activity".equals(field.getName()) && activityName.contentEquals(FitnessActivities.getName(dp.getValue(field).asInt()))) {
count += dp.getValue(field).asInt();
}
}
return count;
}
Tnx in advance.
Best regards
MB
You can get steps for various activities for each day by using the following code
where ESTIMATED_STEP_DELTAS is defined as
You can run this code in a loop to get data for as many days as you want. It will return data buckets for all activities that have steps involved and you can fetch activity name from the bucket by this method that returns activity name as String
Hope it helps! :)