简体   繁体   中英

Google Fit History API returns wrong time buckets

The request is supposed to query Google Fit for aggregated heart rate data for the past 3 months, grouped by 1-day time buckets. What I get, however, is three data points, including two for a single day (May 29th). The expected result is two data points in total: one for May 25th and one for May 29th. What's the reason for this and how can I solve the problem?

This is the code:

DateTime now = new DateTime();
DateTime start = now.minusMonths(3);

DataReadRequest readRequest = new DataReadRequest.Builder()
    .aggregate(DataType.TYPE_HEART_RATE_BPM, DataType.AGGREGATE_HEART_RATE_SUMMARY)
    .bucketByTime(1, TimeUnit.DAYS)
    .enableServerQueries()
    .setTimeRange(start.getMillis(), now.getMillis(), TimeUnit.MILLISECONDS)
    .build();

Fitness.HistoryApi.readData(googleApiClient, readRequest)
    .setResultCallback(new ResultCallback<DataReadResult>() {
      @Override public void onResult(@NonNull DataReadResult dataReadResult) {
        ArrayList<DataPoint> dataPoints = new ArrayList<>();
        for (Bucket bucket : dataReadResult.getBuckets()) {
          List<DataSet> dataSets = bucket.getDataSets();
          for (DataSet dataSet : dataSets) {
            Timber.d("dataSet.dataType: %s", dataSet.getDataType().getName());

            for (DataPoint dp : dataSet.getDataPoints()) {
              dataPoints.add(dp);
              Timber.d("Data point:");
              Timber.d("Type: %s", dp.getDataType().getName());
              Timber.d("Time: %s",
                  new DateTime(dp.getTimestamp(TimeUnit.MILLISECONDS)).toString());
              for (Field field : dp.getDataType().getFields()) {
                Timber.d("Field: %s. Value: %s", field.getName(), dp.getValue(field));
              }
            }
          }
        }
        //...
      }
    });

This the output:

05-30 19:34:05.516 13518-13518/com.test D/TestFragment$1$override: Data point:
05-30 19:34:05.516 13518-13518/com.test D/TestFragment$1$override: Type: com.google.heart_rate.summary
05-30 19:34:05.516 13518-13518/com.test D/TestFragment$1$override: Time: 2016-05-25T22:22:58.526+02:00
05-30 19:34:05.517 13518-13518/com.test D/TestFragment$1$override: Field: average. Value: 65.0
05-30 19:34:05.517 13518-13518/com.test D/TestFragment$1$override: Field: max. Value: 65.0
05-30 19:34:05.517 13518-13518/com.test D/TestFragment$1$override: Field: min. Value: 65.0
05-30 19:34:05.517 13518-13518/com.test D/TestFragment$1$override: Data point:
05-30 19:34:05.518 13518-13518/com.test D/TestFragment$1$override: Type: com.google.heart_rate.summary
05-30 19:34:05.518 13518-13518/com.test D/TestFragment$1$override: Time: 2016-05-29T20:33:37.170+02:00
05-30 19:34:05.519 13518-13518/com.test D/TestFragment$1$override: Field: average. Value: 90.80258
05-30 19:34:05.519 13518-13518/com.test D/TestFragment$1$override: Field: max. Value: 164.0
05-30 19:34:05.519 13518-13518/com.test D/TestFragment$1$override: Field: min. Value: 55.0
05-30 19:34:05.519 13518-13518/com.test D/TestFragment$1$override: Data point:
05-30 19:34:05.520 13518-13518/com.test D/TestFragment$1$override: Type: com.google.heart_rate.summary
05-30 19:34:05.520 13518-13518/com.test D/TestFragment$1$override: Time: 2016-05-29T20:48:37.321+02:00
05-30 19:34:05.520 13518-13518/com.test D/TestFragment$1$override: Field: average. Value: 86.78551
05-30 19:34:05.521 13518-13518/com.test D/TestFragment$1$override: Field: max. Value: 106.0
05-30 19:34:05.521 13518-13518/com.test D/TestFragment$1$override: Field: min. Value: 78.0

As far as I know, it is important to specify the data source.

I've read in FAQ - Values for step/distance/active time/calories do not match those of Fit app , data differences could be caused by not reading the proper data source. As given, to match the value of Google Fit, you should be reading data like this:

DataSource ESTIMATED_STEP_DELTAS = new DataSource.Builder()
       .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
       .setType(DataSource.TYPE_DERIVED)
       .setStreamName("estimated_steps")
       .setAppPackageName("com.google.android.gms")
       .build();
DataReadRequest readRequest = new DataReadRequest.Builder()
       .aggregate(ESTIMATED_STEP_DELTAS,    DataType.AGGREGATE_STEP_COUNT_DELTA)
       .aggregate(DataType.TYPE_DISTANCE_DELTA, DataType.AGGREGATE_DISTANCE_DELTA)
       .aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED)
       .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
       .bucketByTime(1, TimeUnit.DAYS)
       .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
       .build();

I hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM