dan.lee
dan.lee

Reputation: 695

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.

Upvotes: 1

Views: 239

Answers (2)

dan.lee
dan.lee

Reputation: 695

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

Upvotes: 1

pelotasplus
pelotasplus

Reputation: 10052

You need to add read() to DataReadRequest.Builder(), no?

This is how i read data:

    DataReadRequest readRequest = new DataReadRequest.Builder()
            .read(DataType.TYPE_ACTIVITY_SEGMENT)
            .read(DataType.TYPE_CALORIES_CONSUMED)
            .setTimeRange(today.startTime, today.endTime, TimeUnit.MILLISECONDS)
            .build();

And in your case, there is no .read(DataType.TYPE_STEP_COUNT_DELTA) or something similar ...

Upvotes: 0

Related Questions