5

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.

nav_jan
  • 2,473
  • 4
  • 24
  • 42
  • 2
    can you try .bucketByTime(1, TimeUnit.DAYS) – Cowboy Farnz Feb 09 '18 at 15:32
  • 1
    It moved ahead thanks! I wonder why they do not include that in get-started page. Although I am now getting other crash which I am looking into now. – nav_jan Feb 09 '18 at 15:40
  • 3
    Something very similar is done [here](https://github.com/googlesamples/android-fit/blob/513f551c632f4671ec3707e2847bf1ccab425d78/BasicHistoryApi/app/src/main/java/com/google/android/gms/fit/samples/basichistoryapi/MainActivity.java#L206). It's strange that the documentation page has broken code on. – Janez Kuhar Feb 09 '18 at 15:55

1 Answers1

6

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.

Cowboy Farnz
  • 329
  • 2
  • 11