Matt McMinn
Matt McMinn

Reputation: 16311

Getting Google Fit data that matches Google Fit

I am trying to get step count data on a day by day basis from Google Fit to display in my application. It seems like the code I'm using is correct, as I am getting some data, but the numbers are way off when I compare the data that I get to the Google Fit app. For example, in Google Fit (both on the web and in the Android app) for March 19 it shows 2269 steps. In my app for March 19 it shows 64 steps. The data that I get in my app is consistently lower by a power of 10. Am I requesting the data incorrectly? Relevant code below.

Client builder code

GoogleApiClient.Builder builder = new GoogleApiClient.Builder(Main.instance)
                .addApi(Fitness.API)
                .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ));

Request builder code

private DataReadRequest queryFitnessData(long date)
{
    // Build a single day range.
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(date);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    long startTime = cal.getTimeInMillis();
    cal.add(Calendar.DATE, 1);
    cal.add(Calendar.MILLISECOND, -1);
    long endTime = cal.getTimeInMillis();

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

Performing the query

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

if(dataReadResult.getBuckets().size() > 0)
{
    Bucket bucket = dataReadResult.getBuckets().get(0);
    for(DataSet dataSet : bucket.getDataSets())
    {
        for(DataPoint dp : dataSet.getDataPoints())
        {
            if(dp.getDataType().equals(DataType.TYPE_STEP_COUNT_DELTA))
            {
                for(Field field : dp.getDataType().getFields())
                {
                    if(field.getName().equals(Field.FIELD_STEPS.getName()))
                    {
                        if(listener != null)
                        {
                            listener.onStepCount(dp.getValue(field).asInt());
                        }
                    }
                }
            }
        }
    }
}

Upvotes: 2

Views: 1059

Answers (1)

Longmang
Longmang

Reputation: 1663

The first thing I would probably try is to get rid of enableServerQueries() and query the local data, this may be a sync time issue.

Outside of that difficult to tell without seeing some of the other data that comes back in dataReadResult. I'd be tempted to put a few Log statements in to see what is happening.

My code is shown below, which works for me

for (Bucket bucket : dataReadResult.getBuckets()) {

    Log.d(TAG, "Bucket start time: " + bucket.getStartTime(TimeUnit.SECONDS));

    List<DataSet> dataSets = bucket.getDataSets();

    totalStepsValue = 0;

    for (DataSet dataSet : dataSets) {
        for (DataPoint dp : dataSet.getDataPoints()) {
            for (Field field : dp.getDataType().getFields()) {
                if (field.getName().equals("steps")) {
                    totalStepsValue += dp.getValue(field).asInt();
                } 
            }
        }
    }

    Log.d(TAG, "totalStepsValue: " + totalStepsValue);
}

Also try getting a days data using 30 minute buckets and see if you get anything different.

Upvotes: 0

Related Questions