justyn
justyn

Reputation: 11

Automatically recorded activities missing aggregated activity summary from google fit api

Walking/Running/Biking are examples of activities automatically recorded on android phones with google fit installed. I've been trying obtain these activities from the Fit API for Android with the following query...

DataReadRequest readRequest = new DataReadRequest.Builder()
    .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
    .bucketBySession(1, TimeUnit.MINUTES)
    .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
    .build();

DataReadResult dataReadResult = Fitness.HistoryApi
    .readData(googleApiClient, readRequest)
    .await(1, TimeUnit.MINUTES);

The results of this query provides a list of activities with the start time and duration of that activity. This is exactly what I am expecting but for some reason it doesn't return the data that is automatically recorded by the google fit application. It seems to only return items that fall under one of these conditions:

Question

Is anyone aware of how I might need to modify my query to obtain data that is automatically recorded by the google fit application on my device? It might be the case that this specific data just isn't available for use (which would be strange because I can get the steps that are recorded automatically).

Upvotes: 1

Views: 1956

Answers (2)

Matthew Woo
Matthew Woo

Reputation: 1378

I get activity segment data just fine. My read request is different from yours so I copied yours and read a week's worth of data and received no data sets. In this past week I have not manually inserted any data sessions. My belief is that the bucketBySession is your issue. I ran 3 tests, each DataReadRequest described below assuming a time range is also set (1 week for me):

  1. read(DataType.TYPE_ACTIVITY_SEGMENT); no aggregation, no bucketing. Result: Good data.
  2. aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY) with bucketBySession(1, TimeUnit.MINUTES). Result: No data sets or buckets.
  3. aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY) with bucketByTime(1, TimeUnit.DAYS). Result: Good data

In my opinion the bucketBySession documentation isn't very clear that it would exclusively return data that is part of a Session, but that seems to explain the results from my 3 tests as well as your list of conditions that you noticed.

Note also that I have the Google Fit app installed and I'm not sure if this same data would be automatically recorded/subscribed to if it were not.

Upvotes: 1

Colin White
Colin White

Reputation: 1116

Have you set up a subscription to passively save the data? Here's how:

Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_ACTIVITY_SEGMENT)
    .setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(Status status) {
            if (status.isSuccess()) {
                if (status.getStatusCode()
                        == FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
                    Log.i(TAG, "Existing subscription for activity detected.");
                } else {
                    Log.i(TAG, "Successfully subscribed!");
                }
            } else {
                Log.i(TAG, "There was a problem subscribing.");
            }
        }
});

Upvotes: 1

Related Questions