The Sensors API lets you read raw sensor data in your app in real time. Use this API to:
- List data sources available on the device and on companion devices.
- Register listeners to receive raw sensor data.
- Unregister listeners to stop receiving raw sensor data.
The Sensors API does not automatically store sensor readings in the fitness store and sensor registrations created with the Sensors API are not persisted when the system restarts. You typically use the Recording API to record data in the background with persistent subscriptions and you use the Sensors API to display or process sensor readings in real time. In many cases, you use both of these APIs in your app.
List available data sources
To obtain a list of all available data sources on the device and on companion
devices, use the
SensorsClient.findDataSources
method:
Fitness.getSensorsClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) .findDataSources( DataSourcesRequest.Builder() .setDataTypes(DataType.TYPE_LOCATION_SAMPLE) .setDataSourceTypes(DataSource.TYPE_RAW) .build()) .addOnSuccessListener { dataSources -> dataSources.forEach { Log.i(TAG, "Data source found: ${it.streamIdentifier}") Log.i(TAG, "Data Source type: ${it.dataType.name}") if (it.dataType == DataType.TYPE_LOCATION_SAMPLE) { Log.i(TAG, "Data source for LOCATION_SAMPLE found!") ... } } } .addOnFailureListener { e -> Log.e(TAG, "Find data sources request failed", e) }
To get information about the device for a data source, use the
DataSource.getDevice
method. The device information is useful to distinguish from similar
sensors on different devices, show the device information from a sensor to the
user, or process data differently depending on the device. For example, you may
be interested in reading data specifically from the sensor on a wearable
device but not from the same type of sensor on the phone.
To get a Device
instance for the device that is running your activity, use the
Device.getLocalDevice
static method. This is useful when you want to check if a data source is on the
same device that your app is running on.
Add a listener
To add a listener to receive raw data of a particular fitness data type or from
a specific data source, use the
SensorsClient.add
method:
val listener = OnDataPointListener { dataPoint -> for (field in dataPoint.dataType.fields) { val value = dataPoint.getValue(field) Log.i(TAG, "Detected DataPoint field: ${field.name}") Log.i(TAG, "Detected DataPoint value: $value") } } Fitness.getSensorsClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) .add( SensorRequest.Builder() .setDataSource(dataSource) // Optional but recommended for custom data sets. .setDataType(dataType) // Can't be omitted. .setSamplingRate(10, TimeUnit.SECONDS) .build(), listener ) .addOnCompleteListener { task -> when (task.isSuccessful) { true -> Log.i(TAG, "Listener registered!") else -> Log.e(TAG, "Listener not registered.", task.exception) } }
Remove a listener
To remove a listener from raw data updates, use the
SensorsClient.remove
method:
Fitness.getSensorsClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) .remove(listener) .addOnCompleteListener { task -> if (task.isSuccessful && task.result!!) { Log.i(TAG, "Listener was removed!") } else { Log.w(TAG, "Listener was not removed.") } }