17

I have developed an application that needs to display daily steps count. To do this, I used the API available in Google Fit SDK.

All seems to be working properly, but the steps count I get does not match to the one displayed in Google Fit Official Application.

For example, I get 2308 steps when the Google Fit App display 2367 steps.

Is there a reason for this? Does anyone have the same issue? Anyone have a clue?

Akshay Vasu
  • 445
  • 1
  • 12
  • 33
Thomas Thomas
  • 824
  • 1
  • 9
  • 21
  • Is it possible to post some code? Did you use the AsyncTask class for recordin the steps in the background? I have asked this question few days ago,but none answered me.http://stackoverflow.com/questions/30129784/getting-step-count-from-google-fit. – Theo May 11 '15 at 10:34
  • Can you post your code? – António Paulo May 04 '16 at 10:50

5 Answers5

30

I found the solution.

The Fit app does some additional processing on top of the steps. It estimates steps based on the activity when none are recorded.

If it can help someone : You need to use a custom DataSource of the package com.google.android.gms

DataSource ESTIMATED_STEP_DELTAS = new DataSource.Builder()
            .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
            .setType(DataSource.TYPE_DERIVED)
            .setStreamName("estimated_steps")
            .setAppPackageName("com.google.android.gms")
            .build();

And use this in your aggregate method like this :

DataReadRequest readRequest = new DataReadRequest.Builder()
            .aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
            .bucketByTime(1, TimeUnit.DAYS)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();
Thomas Thomas
  • 824
  • 1
  • 9
  • 21
  • This helped me, nice work. How did you get that solution? Any source of that information, or just your hard work? – cyborg86pl Apr 10 '15 at 11:20
  • @Thomas Thomas this is brilliant, can you help me with one thing? DataSource is always null, how can I find out the steps count from running, walking etc ? – ShahrozKhan91 Jun 21 '15 at 07:28
  • 2
    I still don't get the same values with this, most of the days returned more than what Google Fit is reporting! It is actually more inaccurate than the way shown in the sample. – Phuah Yee Keat Jul 12 '15 at 14:45
  • any algorithm on how they "estimate steps based on the activity when none are recorded" ? – AdityaKapreShrewsburyBoston Jul 16 '15 at 15:31
  • I used this method but i am still getting different step count. For 11 th Aug google fit shows 801 while the step count i received using above method is 838. Close but not the same.. – wittyurchin Aug 12 '16 at 09:13
  • 1
    can please give us full code to get values from request as i was not able find it – Sameer Z. Sep 14 '16 at 10:29
  • I still don't get the exact steps count as of Google Fit app. I'm using `Fitness.HistoryApi.readData` method to get the steps of last 1 week. – Aniruddha Sep 21 '16 at 07:19
  • Superb work @Thomas Thomas. I was struggling since 2 days. – Smeet Dec 06 '16 at 07:14
  • 1
    What about calories ? Following code does not returns anything. DataSource ESTIMATED_STEP_DELTAS = new DataSource.Builder() .setDataType(DataType.TYPE_CALORIES_EXPENDED) .setType(DataSource.TYPE_DERIVED) .setStreamName("estimated_calories") .setAppPackageName("com.google.android.gms") .build(); – Smeet Dec 27 '16 at 15:17
  • Perfect solution. Saved my day. Thanks a lot. – Hiren Patel Oct 24 '17 at 16:08
  • @PhuahYeeKeat In my case I'm getting multiple record for same day. Was it the same case for you? It looks bit long before you did, but if you remember it !! – CoDe Apr 13 '18 at 09:10
  • Yeah there are multiple records. I think Google used some AI thing to group them. – Phuah Yee Keat Jun 28 '18 at 06:16
  • @Thomas There are still some difference in steps by this process. Its not working. Do you have any idea – Harsh Trivedi Oct 23 '18 at 07:32
  • 1
    Can anyone help me out to find calories burn same way like this? @Thomas – tarun patel Nov 07 '18 at 04:54
4

Google Play Services 7.3 (released 4/28/2015) added a new method to the HistoryApi.readDailyTotal, which matches the step count on Google Fit official app and is easier to use.

    PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(fitnessApiClient, DataType.AGGREGATE_STEP_COUNT_DELTA);
    DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
    if (totalResult.getStatus().isSuccess()) {
        DataSet totalSet = totalResult.getTotal();
        steps = totalSet.isEmpty() ? -1 : totalSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
    }
Trung
  • 1,655
  • 2
  • 18
  • 26
  • How would I be able to do this without a service? Using await, it gives an error in the main activity code and changing the await to 0 seconds does not get the data. – sirvar Oct 21 '15 at 20:00
  • 1
    You can't do it without Google Play services. You must use await in background thread. If you want to use it in main thread, call result.setResultCallback(...); – Trung Oct 22 '15 at 04:24
  • How do I get distance using Fitness.HistoryApi.readDailyTotal – Query Nov 27 '16 at 11:29
2

As Sameer Z. requested I'm posting full code to get values.

DataSource estimatedSteps = new DataSource.Builder()
    .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
    .setType(DataSource.TYPE_DERIVED)
    .setStreamName("estimated_steps")
    .setAppPackageName("com.google.android.gms")
    .build();

DataReadRequest readRequest = new DataReadRequest.Builder()
        .aggregate(estimatedSteps, DataType.AGGREGATE_STEP_COUNT_DELTA)
        .setTimeRange(startTimeSeconds, endTimeSeconds, TimeUnit.SECONDS)
        .bucketByTime(1, TimeUnit.DAYS)
        .enableServerQueries()
        .build();

PendingResult<DataReadResult> pendingResult = Fitness.HistoryApi.readData(client, readRequest);
pendingResult.setResultCallback(new ResultCallback<DataReadResult>() {
    @Override
    public void onResult(@NonNull DataReadResult dataReadResult) {
        List<Bucket> allBuckets = dataReadResult.getBuckets();

        for (Bucket bucket : allBuckets) {
            long startAtSeconds = bucket.getStartTime(TimeUnit.SECONDS);

            Value stepsValue = getValue(bucket, DataType.TYPE_STEP_COUNT_DELTA, Field.FIELD_STEPS);
            int steps = stepsValue != null ? stepsValue.asInt() : 0;

            Log.d(TAG, String.format("startAtSeconds %s, steps %s", startAtSeconds, steps));
        }
    }
});
Community
  • 1
  • 1
sealskej
  • 7,281
  • 12
  • 53
  • 64
0

Google Fit sdk is a part of Google Play services.. The official Google Fit app also collects the same data from the Google Play services sdk but Google probably has added a few more code into the Google Fit app to make the data more accurate. It could also be a bug in the Google Fit app.

edwinj
  • 430
  • 3
  • 14
0

Create an Android application that uses Google Fit API to access the step count data, process this data, and present it in a user-friendly manner.