fit-samples
fit-samples copied to clipboard
How to read steps session wise.
Issue by krunal93
Monday Jul 02, 2018 at 13:53 GMT
Originally opened as https://github.com/googlesamples/android-fit/issues/37
I am in need to show steps time line wise like below.
Time 10 am - 56 steps Time 11 am - 156 steps Time 4.30 pm - 236 steps
I don't want to aggregate by hour or anything , i just want the actual data that is currently google fit app is showing in timeline using steps filter.
However i am able to retrieve steps using bucketBySession that entered manually into google fit app but not able to retrieve those steps history which is generated automatically.
Please let me know how to overcome this.
Below is my sample code.
// Build a session read request
SessionReadRequest readRequest = new SessionReadRequest.Builder()
.setTimeInterval(times[0], times[1], TimeUnit.MILLISECONDS)
.readSessionsFromAllApps()
.read(DataType.TYPE_STEP_COUNT_DELTA)
.enableServerQueries()
.build();
Fitness.getSessionsClient(mContext, GoogleSignIn.getLastSignedInAccount(mContext))
.readSession(readRequest)
.addOnSuccessListener(new OnSuccessListener<SessionReadResponse>() {
@Override
public void onSuccess(SessionReadResponse sessionReadResponse) {
// Get a list of the sessions that match the criteria to check the result.
List<Session> sessions = sessionReadResponse.getSessions();
Log.i(TAG, "Session read was successful. Number of returned sessions is: "
+ sessions.size());
for (Session session : sessions) {
// Process the session
dumpSession(session);
// Process the data sets for this session
List<DataSet> dataSets = sessionReadResponse.getDataSet(session);
for (DataSet dataSet : dataSets) {
dumpDataSet(dataSet);
}
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i(TAG, "Failed to read session");
}
});
final DataReadRequest req = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
.enableServerQueries()
.bucketBySession(1, TimeUnit.MINUTES)
.setTimeRange(times[0], times[1], TimeUnit.MILLISECONDS)
.build();
Fitness.getHistoryClient(mContext, GoogleSignIn.getLastSignedInAccount(mContext))
.readData(req)
.addOnSuccessListener(
dataReadResponse -> readData(dataReadResponse, MESSAGE_HEALTH_DATA_LIST_KEY))
.addOnFailureListener(
e -> {
});
I am trying with the both client , session as well as history but didn't get any suceess. thanks, Krunal