julianemue
julianemue

Reputation: 41

Google Fit History API - incorrect values for steps

I'm trying to get the walked steps from today. Therefor I've found 2 solutions. 1)

private void getStepsToday() {
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 00);
    long startTime = cal.getTimeInMillis();

    final DataReadRequest readRequest = new DataReadRequest.Builder()
            .read(DataType.TYPE_STEP_COUNT_DELTA)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

    DataReadResult dataReadResult =
            Fitness.HistoryApi.readData(mGoogleApiFitnessClient, readRequest).await(1, TimeUnit.MINUTES);

    DataSet stepData = dataReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);

    int totalSteps = 0;

    for (DataPoint dp : stepData.getDataPoints()) {
        for(Field field : dp.getDataType().getFields()) {
            int steps = dp.getValue(field).asInt();

            totalSteps += steps;

        }
    }}

2)

private void getStepsToday() {
    PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(mGoogleApiFitnessClient, DataType.TYPE_STEP_COUNT_DELTA);
    DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
    if (totalResult.getStatus().isSuccess()) {
        DataSet totalSet = totalResult.getTotal();
        int total = totalSet.isEmpty()
                ? 0
                : totalSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
        publishTodaysStepData(total);
    } else {
        publishTodaysStepData(0);
    }
}

By using the first one I get for example 27 and by using the second one 1425 steps as the answer. The right one (after comparing with google fit app) should be 1425. Thus why is the first one not working?

I also have the same problem by asking for steps from last week. By using method 1 for steps from last week I realized I do get steps for the right days (sometimes even the right ones), but whenever the steps value is more than 50 (I think) the value is not correct.

Does anyone has an answer to this strange behavior?

Upvotes: 2

Views: 2994

Answers (4)

Paul Charlton
Paul Charlton

Reputation: 521

I know this is a bit old but stumbled here and eventually found this reference page. I'm not sure if the packages have been upated but it says that the getDailyTotal() is the equvaliant to:

   readData(client, new DataReadRequest.Builder()
       .setTimeRange(midnight.getMillis(), now.getMillis(), TimeUnit.MILLISECONDS)
       .bucketByTime(1, TimeUnit.DAYS)
       .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
       .build());

Upvotes: 0

Ramya Ramesh
Ramya Ramesh

Reputation: 309

If you need step counts from readDailyTotal() and readData() to be equal, you have to specify the dataSource as:

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();

This count will match the count from Google FIT app. See here

Upvotes: 2

SpiritCrusher
SpiritCrusher

Reputation: 21043

Try this

public int getStepsCount(long startTime, long endTime) {
    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();
    PendingResult<DataReadResult> pendingResult = Fitness.HistoryApi
            .readData(
                    fitnessClient,
                    new DataReadRequest.Builder()
                            .aggregate(ESTIMATED_STEP_DELTAS,
                                    DataType.AGGREGATE_STEP_COUNT_DELTA)
                            .bucketByTime(1, TimeUnit.HOURS)
                            .setTimeRange(startTime, endTime,
                                    TimeUnit.MILLISECONDS).build());
    int steps = 0;
    DataReadResult dataReadResult = pendingResult.await();
    if (dataReadResult.getBuckets().size() > 0) {
        //Log.e("TAG", "Number of returned buckets of DataSets is: "
                //+ dataReadResult.getBuckets().size());
        for (Bucket bucket : dataReadResult.getBuckets()) {
            List<DataSet> dataSets = bucket.getDataSets();
            for (DataSet dataSet : dataSets) {
                for (DataPoint dp : dataSet.getDataPoints()) {
                    for (Field field : dp.getDataType().getFields()) {
                        steps += dp.getValue(field).asInt();
                    }
                }
            }
        }
    } else if (dataReadResult.getDataSets().size() > 0) {
        for (DataSet dataSet : dataReadResult.getDataSets()) {
            for (DataPoint dp : dataSet.getDataPoints()) {
                for (Field field : dp.getDataType().getFields()) {
                    steps += dp.getValue(field).asInt();
                }
            }
        }
    }
    return steps;
}

Upvotes: 2

stkent
stkent

Reputation: 20128

Are you sure that TYPE_STEP_COUNT_DELTA is the correct DataType to request? From the docs (their emphasis):

In the com.google.step_count.delta data type, each data point represents the number of steps taken since the last reading. When using this data type, each step is only ever reported once, in the reading immediately succeeding the step. By adding all of the values together for a period of time, the total number of steps during that period can be computed.

As an example, if a user walked a total of 5 steps, with 3 different readings, the values for each reading might be [1, 2, 2].

Do you get the expected results if you use AGGREGATE_STEP_COUNT_DELTA instead?

Upvotes: 1

Related Questions