Search code examples
androidgoogle-fit

IllegalArgumentException when reading data from DataReadResult


I am currently working on an Android app that uses Google's Fit APIs. However, when I read data from the DataReadResult,DataSet ds = result.getDataSet(DataType.TYPE_STEP_COUNT_DELTA); I get:

IllegalArgumentException: Attempting to read data for com.google.step_count.delta, which was not requested

This is my AsyncTask that I get the DataReadResult from:

public static class GetReadResultTask extends AsyncTask<Void, Void, DataReadResult> {

    protected DataReadResult doInBackground(Void... voids) {
        Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        long endTime = cal.getTimeInMillis();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        long startTime = cal.getTimeInMillis();

        DataReadRequest readRequest = new DataReadRequest.Builder()
                .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
                .bucketByTime(1, TimeUnit.HOURS)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();
        DataReadResult result =
                Fitness.HistoryApi.readData(mClient, readRequest).await(1, TimeUnit.MINUTES);
        return result;
    }
}

How can I fix this? Any help would be appreciated.


Solution

  • I figured out what I did wrong.

    Aggregate data is returned in Buckets, not DataSets, so instead of calling result.getDataSets(DataType.TYPE_STEP_COUNT_DELTA); , I have to do List<Buckets> buckets = result.getBuckets(), then iterate through the buckets and get data sets using

    buckets.get(currentIndex).getDataSet(DataType.AGGREGATE_STEP_COUNT_DELTA);