I'm trying to query google fit activities that where started by the user going into google fit pressing + -> start activity -> start, as opposed to the usually passively tracked activity, I originally thought this was what the sessions api was for but I think that may be for something else.
private SessionReadRequest querySessionData() {
// Set a start and end time for our query, using a start time of 4 week before this moment.
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.WEEK_OF_YEAR, -4);
long startTime = cal.getTimeInMillis();
// Build a session read request
return new SessionReadRequest.Builder()
.setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
.read(DataType.TYPE_SPEED)
//.setSessionName()
.build();
}
I was then hoping to get activity data historically but then when I got no results back from the query I presumed I was using the wrong api.
Ok so eventually I worked out that I can achieve what I wanted using the history api and the key is to BUCKET BY SESSION, so first create the request like this:
private DataReadRequest querySessionData() {
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
//query 30 days in the past
cal.add(Calendar.DAY_OF_MONTH, - 30);
long startTime = cal.getTimeInMillis();
return new DataReadRequest.Builder()
.aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
.aggregate(DataType.TYPE_DISTANCE_DELTA, DataType.AGGREGATE_DISTANCE_DELTA)
.aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED)
.aggregate(DataType.TYPE_SPEED, DataType.AGGREGATE_SPEED_SUMMARY)
.aggregate(DataType.TYPE_HEART_RATE_BPM, DataType.AGGREGATE_HEART_RATE_SUMMARY)
.bucketBySession(1, TimeUnit.MINUTES)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
}
Then to use that request use the history api like this:
final DataReadRequest readRequest = querySessionData();
Fitness.HistoryApi.readData(mClient, readRequest).setResultCallback(new ResultCallback<DataReadResult>() {
@Override
public void onResult(@NonNull DataReadResult dataReadResult) {
//do your stuff!!
}
});
}