# Access raw sensor data

The Sensors API lets you read raw sensor data in your app in real time. Use
this API to do the following:

- List data sources that are available on the device and on companion devices.
- Register listeners to receive raw sensor data.
- Unregister listeners so that they no longer receive raw sensor data.

> [!CAUTION]
> **Caution:** The Sensors API doesn't automatically store sensor readings in the fitness store, and sensor registrations created with the Sensors API aren't persisted when the system restarts. It's typical to use the [Recording API](https://developers.google.com/fit/android/record) to record data in the background with persistent subscriptions; use the Sensors API to display or process sensor readings in real time. In many cases, both of these APIs need to be used in an app.

> [!NOTE]
> **Note:** For best practices when you manage user data, see [Google Fit Developer and User Data Policy](https://developers.google.com/fit/policy).

## List available data sources

To obtain a list of all available data sources on the device and on companion
devices, use the
[`SensorsClient.findDataSources`](https://developers.google.com/android/reference/com/google/android/gms/fitness/SensorsClient#public-tasklistdatasource-finddatasources-datasourcesrequest-request)
method:

### Kotlin

```kotlin
private val fitnessOptions = FitnessOptions.builder().addDataType(DataType.TYPE_STEP_COUNT_DELTA).build()

// Note: Fitness.SensorsApi.findDataSources() requires the
// ACCESS_FINE_LOCATION permission.
Fitness.getSensorsClient(requireContext(), GoogleSignIn.getAccountForExtension(requireContext(), fitnessOptions))
    .findDataSources(
        DataSourcesRequest.Builder()
            .setDataTypes(DataType.TYPE_STEP_COUNT_DELTA)
            .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_STEP_COUNT_DELTA) {
                Log.i(TAG, "Data source for STEP_COUNT_DELTA found!")

                ...
            }
        }
    }
    .addOnFailureListener { e ->
        Log.e(TAG, "Find data sources request failed", e)
    }
```

### Java

```java
FitnessOptions fitnessOptions = FitnessOptions.builder().addDataType(DataType.TYPE_STEP_COUNT_DELTA).build();

// Note: Fitness.SensorsApi.findDataSources() requires the
// ACCESS_FINE_LOCATION permission.
Fitness.getSensorsClient(getApplicationContext(), GoogleSignIn.getAccountForExtension(getApplicationContext(), fitnessOptions))
        .findDataSources(
                new DataSourcesRequest.Builder()
                        .setDataTypes(DataType.TYPE_STEP_COUNT_DELTA)
                        .setDataSourceTypes(DataSource.TYPE_RAW)
                        .build())
        .addOnSuccessListener(dataSources -> {
            dataSources.forEach(dataSource -> {
                Log.i(TAG, "Data source found: ${it.streamIdentifier}");
                Log.i(TAG, "Data Source type: ${it.dataType.name}");

                if (dataSource.getDataType() == DataType.TYPE_STEP_COUNT_DELTA) {
                    Log.i(TAG, "Data source for STEP_COUNT_DELTA 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`](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/DataSource#public-device-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 based on the device. For example, you might
be interested in reading data specifically from the sensor on a wearable
device but not from the same type of sensor on a phone.

To get a [`Device`](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Device)
instance for the device that's running your activity, use the
[`Device.getLocalDevice`](https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Device#public-static-device-getlocaldevice-context-context)
method. This is useful when you want to check whether 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`](https://developers.google.com/android/reference/com/google/android/gms/fitness/SensorsClient#public-taskvoid-add-sensorrequest-request,-ondatapointlistener-listener)
method:

### Kotlin

```kotlin
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
    )
    .addOnSuccessListener {
    	Log.i(TAG, "Listener registered!")
    }
    .addOnFailureListener {
    	Log.e(TAG, "Listener not registered.", task.exception)
    }
```

### Java

```java
OnDataPointListener listener = dataPoint -> {
    for (Field field : dataPoint.getDataType().getFields()) {
        Value value = dataPoint.getValue(field);
        Log.i(TAG, "Detected DataPoint field: ${field.getName()}");
        Log.i(TAG, "Detected DataPoint value: $value");
    }
};
Fitness.getSensorsClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
        .add(
                new SensorRequest.Builder()
                        .setDataSource(dataSource) // Optional but recommended
                        // for custom data sets.
                        .setDataType(dataType) // Can't be omitted.
                        .setSamplingRate(10, TimeUnit.SECONDS)
                        .build(),
                listener
        )
        .addOnSuccessListener(unused ->
                Log.i(TAG, "Listener registered!"))
        .addOnFailureListener(task ->
    Log.e(TAG, "Listener not registered.", task.getCause()));
}
```

## Remove a listener

To remove a listener from raw data updates, use the
[`SensorsClient.remove`](https://developers.google.com/android/reference/com/google/android/gms/fitness/SensorsClient#public-taskboolean-remove-ondatapointlistener-listener)
method:

### Kotlin

```kotlin
Fitness.getSensorsClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
    .remove(listener)
    .addOnSuccessListener {
    	Log.i(TAG, "Listener was removed!")
    }
    .addOnFailureListener {
    	Log.i(TAG, "Listener was not removed.")
    }
```

### Java

```java
Fitness.getSensorsClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
        .remove(listener)
        .addOnSuccessListener(unused ->
                Log.i(TAG, "Listener was removed!"))
        .addOnFailureListener(e ->
                Log.i(TAG, "Listener was not removed."));
```