Search code examples
javaandroidgoogle-fit

Caused by: java.lang.IllegalStateException: Must specify a valid bucketing strategy while requesting aggregation


I get this error while I am creating a read request object DataReadRequest class.

java.lang.IllegalStateException: Must specify a valid bucketing strategy while requesting aggregation

I did refer to above question but it is not helpful for me as I am not using the above code.

My code is as follows where I am seeing this crash :

private void accessGoogleFit() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.YEAR, -1);
        long startTime = cal.getTimeInMillis();

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

I also referred "https://plus.google.com/105817403737304061447/posts/5Bo6qMYRAM9" but it is also not helpful.

The above code is directly from "https://developers.google.com/fit/android/get-started".

Thanks in advance.


Solution

  • As mentioned the bucketByTime method (use .bucketByTime(1, TimeUnit.DAYS as I imagine you want to track steps over a day) aggregates data according to the period of time you're asking for and so this bucketing strategy must be specified to avoid throwing IllegalStateException.