googlearchive / android-fit

Migrated:

Home Page:https://github.com/android/fit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Data differs from the actual Google Fit App

sushantgiri opened this issue · comments

I am building a fitness application where I pull certain data (Steps and Calories) from google fit. I am fetching a week data. The output of the google fit api doesn't match with the google fit data. The output is so random and doesn't have a definite pattern.

Steps From Google Fit API:
Code Used:
private DataReadRequest queryFitnessData()
{
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.WEEK_OF_YEAR, -1);
long startTime = cal.getTimeInMillis();
java.text.DateFormat dateFormat = getDateInstance();
Timber.e("Range Start: " + dateFormat.format(startTime));
Timber.e("Range End: " + dateFormat.format(endTime));
return new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
}

public Observable getStepsCount(GoogleApiClient googleApiClient) {
return Observable.create(subscriber -> {
DataReadResult result =
Fitness.HistoryApi.readData(googleApiClient, queryFitnessData()).await(1, TimeUnit.MINUTES);
if (result.getStatus().isSuccess()) {
subscriber.onNext(result);
} else {
subscriber.onError(new Exception("Exception"));
}
});
}

Output
Start Date: 17th August 2017 End Date: 24th August 2017

17-18 -> 4080
18-19 -> 3837
19-19 -> 7
20-21 -> 2212
21-22 -> 4665
23-23 -> 2949
23-24 -> 6131

Any help would be greatly appreciated. If required, apk can also be shared.

I found that when requesting DataSets from the HistoryAPI work best when supplying the date and time at the start of a day (midnight). It would then return the buckets with more reliable data. Every time the data was refreshed the times were shifted because the examples use Date now = new Date() which of course is different each time.
(I used JodaTime to simplify the start and end times)

// Setting a start and end date using a range of 1 week working backwards using today's
// start of the day (midnight). This ensures that the buckets are in line with the days.
DateTime dt = new DateTime().withTimeAtStartOfDay();
long endTime = dt.getMillis();
long startTime = dt.minusWeeks(1).getMillis();

return new DataReadRequest.Builder()
        .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
        .bucketByTime(1, TimeUnit.DAYS)
        .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
        .build();

Without setting the time to the start of the day:
screenshot_20180305-152838

With setting the time to the start of the day:
screenshot_20180305-152752

Hi joostfunkekupper ,

Can u please share your code.

Thankyou

Hi @rduvvu
Yeah sure, I haven't run it since March 2018, but should be fine still.
Just uploaded it to a new public repo https://github.com/joostfunkekupper/google-fit-android
Cheers

@rduvvu I'm not a contributor to this repository, and probably don't have the time right now to make a PR. As @yoshimaa said, the Google Fit app will always result in slightly different values anyway; https://developers.google.com/fit/faq#get-step-count
To me it sounds like you have a different problem not related to this issue #25

I am closing this issue/PR, as it has been migrated to the new repo linked above in the comments. Thank you!