Custom data types

Google Fit lets you create custom data types for your app and use them to store fitness data. When you create custom data types, make sure:

  • Google Fit does not already provide a similar data type
  • The data type name is clear
  • The data type name accurately represents the underlying data
  • The prefix of the data type name matches your app's package name

Create a custom data type

Android

To create a custom data type for the first time, use the ConfigClient.createCustomDataType method:

// 1. Build a request to create a new data type
DataTypeCreateRequest request = new DataTypeCreateRequest.Builder()
    // The prefix of your data type name must match your app's package name
    .setName("com.app.custom_data_type")
    // Add some custom fields, both int and float
    .addField("field1", Field.FORMAT_INT32)
    .addField("field2", Field.FORMAT_FLOAT)
    // Add some common fields
    .addField(Field.FIELD_ACTIVITY)
    .build();


// 2. Invoke the Config API with:
// - The Google API client object
// - The create data type request
PendingResult<DataTypeResult> pendingResult =
    ConfigApi.createCustomDataType(mClient, request);


// 3. Check the result asynchronously
// (The result may not be immediately available)
pendingResult.setResultCallback(
    new ResultCallback<DataTypeResult>() {
        @Override
        public void onResult(DataTypeResult dataTypeResult) {
            // Retrieve the created data type
            DataType customType = dataTypeResult.getDataType();
            // Use this custom data type to insert data in your app
            // (see other examples)
            ...
        }
    }
);

REST

To create a custom data type, create a new data source specifying the name of the data type and its field definitions.

For example, define a custom data type in a data source as follows:

{
"dataStreamId": "exampleDataSourceId",
...
"dataType": {
  "field": [
    {
      "name": "myFieldA",
      "format": "integer"
    },
    {
      "name": "myFieldB",
      "format": "integer"
    },
  ],
  "name": "com.example.myapp.mycustomtype"
},
...
}

Use your custom data types

Android

To access a custom data type, use the ConfigClient.readDataType method:

// 1. Invoke the Config API with:
// - The Google API client object
// - The custom data type name
PendingResult<DataTypeResult> pendingResult =
    ConfigApi.readDataType(mClient, "com.app.custom_data_type");

// 2. Check the result asynchronously
// (The result may not be immediately available)
pendingResult.setResultCallback(
    new ResultCallback<DataTypeResult>() {
        @Override
        public void onResult(DataTypeResult dataTypeResult) {
            // Retrieve the custom data type
            DataType customType = dataTypeResult.getDataType();
            // Use this custom data type to insert data in your app
            // (see other examples)
            ...
        }
    }
);

REST

To insert fitness data of your custom type, specify the data source you created for your data type when you create a dataset with new data points. The data points must have the same number of components and types as those specified by your custom data type.

To read fitness data of your custom data type, specify the data source you created for your data type when you retrieve data points from the fitness store.