Colin White
Colin White

Reputation: 1116

Google Fit: Create a Session from data collected using the Recording API

In the Android Google Fit SDK, is it possible to create a Session from data already collected using the Recording API (i.e. already in the fitness store)?

I've tried using the DataSource from a DataSet from a DataReadRequest, but that causes the SessionInsertRequest to fail with a 5015 error:

Status{statusCode=unknown status code: 5015, resolution=null}

Here is my read request:

DataReadResult result = Fitness.HistoryApi.readData(apiClient,
        new DataReadRequest.Builder()
                .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
                .bucketByActivitySegment(1, TimeUnit.SECONDS)
                .setTimeRange(startTime, endTime, TimeUnit.SECONDS)
                .build())
        .await();

Here is my session insert request:

SessionInsertRequest insertRequest = new SessionInsertRequest.Builder()
            .setSession(session)
            .addDataSet(result.getBuckets().get(0).getDataSets().get(0))
            .build();

The root problem: I need to attach some metadata to each of the user's activities and thought Sessions would fit this problem best. If there's a better way please let me know.

Upvotes: 0

Views: 849

Answers (1)

Matthew Woo
Matthew Woo

Reputation: 1377

So after a little digging I found out what the 5015 error means. It's documented in the FitnessStatusCodes:

public static final int INCONSISTENT_PACKAGE_NAME

Status code denotes that app attempted to insert data for a DataSource that does not match the app's package name.

Constant Value: 5015

So it looks like if you want to make your app have metadata connected to activities, you would have to subscribe to data from your own app.

EDIT: The below suggestion was attempted, but didn't work. I leave it here because we don't know exactly why it didn't work and so to what extent it is wrong is unsure and how to interpret storing data "on behalf of your app" is left open.

I previously thought you needed to make your own DataSource, but according to the RecordingApi.subscribe(GoogleApiClient, DataType) you can specify the DataType and it'll use the default DataSource. Moreover, the Recording API Guide seems to indicate that it will be added to the Fitness Store with your app's name on it ("on behalf of your app"). So it would just be

Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_ACTIVITY_SEGMENT);

Though, also in the subscribe documentation it says

If the requested subscription already exists, the request will be a no-op and SUCCESS_ALREADY_SUBSCRIBED will be returned.

So I'm not quite sure what that means for two different apps subscribing to data and if both app package names are stored in the DataSource

Upvotes: 1

Related Questions