If your app needs to capture information that isn't already covered by one of the existing data types on the Google Fit platform, you can create a custom data type.
Creating custom data types
Create, or specify, a custom data type to capture custom data. When you create custom data types, make sure:
- The data type name accurately represents the underlying data.
The prefix of the data type name matches your app's package name.
Android
To create a custom data type for the first time, use the
ConfigClient.createCustomDataType
method:
val request = DataTypeCreateRequest.Builder()
// The prefix of your data type name must match your app's package name
.setName("com.packagename.appname.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()
Fitness.getConfigClient(this, account)
.createCustomDataType(request)
.addOnSuccessListener { dataType ->
// Use this custom data type to insert data into your app.
Log.d(TAG, "Created data type: ${dataType.name}")
}
REST
Data types are a property of data sources, in the REST API. To capture custom data, you need to create a data source and then specify the data type:
- Call the REST API to create a new data source. For example,
FlexibilityMeasure
. Give the data type a unique name, that closely represents the data it's capturing.
Specify the fields of the data type, and their formats.
HTTP method
POST
Request URL
https://www.googleapis.com/fitness/v1/users/me/dataSources
Request body
{
"dataStreamName": "FlexibilityMeasure",
"type": "raw",
"application": {
"detailsUrl": "http://recoveryapps.com",
"name": "Stretch Flex",
"version": "1"
},
"dataType": {
"name": "com.recoveryapps.stretchflex.flexibility",
"field": [
{
"name": "ankle_range_degrees",
"format": "integer"
},
{
"name": "wrist_range_degrees",
"format": "integer",
"optional": true
}
]
}
}
Response
If your data source was created successfully, you'll get a 200 OK
HTTP
response status code. The response body contains a JSON representation of
the data source, including a datasource.dataStreamId
property. Use this ID
as the dataSourceId
to add data.
Using custom data types
Android
Convert your custom data type's name from a string
(com.packagename.appname.custom_data_type) into a DataType
object using the
ConfigClient.readDataType
method. Use the returned object to insert
and read custom data.
Fitness.getConfigClient(this, account)
.readDataType("com.packagename.appname.custom_data_type")
.addOnSuccessListener { dataType ->
// Use this custom data type to insert data into your app.
Log.d(TAG, "Retrieved data type: ${dataType.name}")
}
REST
To add or read custom data using your custom data types, you need their data
sources. To check the data sources of a custom data type, send a GET
request to the REST API.
Adding custom data
To insert custom data, create a dataset with new data points. Specify the data source you created for your custom data type. The data points need to have all the correct fields and formats specified in your custom data type.
Reading custom data
To read custom data, specify the data source you created for your custom data type when you retrieve data points from the Google Fit platform.