Access Raw Sensor Data

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:

// Note: Fitness.SensorsApi.findDataSources() requires the ACCESS_FINE_LOCATION permission.
Fitness.getSensorsClient(this, GoogleSignIn.getLastSignedInAccount(this))
    .findDataSources(
        new DataSourcesRequest.Builder()
            .setDataTypes(DataType.TYPE_LOCATION_SAMPLE)
            .setDataSourceTypes(DataSource.TYPE_RAW)
            .build())
    .addOnSuccessListener(
        new OnSuccessListener<List<DataSource>>() {
          @Override
          public void onSuccess(List<DataSource> dataSources) {
            for (DataSource dataSource : dataSources) {
              Log.i(TAG, "Data source found: " + dataSource.toString());
              Log.i(TAG, "Data Source type: " + dataSource.getDataType().getName());

              // Let's register a listener to receive Activity data!
              if (dataSource.getDataType().equals(DataType.TYPE_LOCATION_SAMPLE)
                  && mListener == null) {
                Log.i(TAG, "Data source for LOCATION_SAMPLE found!  Registering.");
                registerFitnessDataListener(dataSource, DataType.TYPE_LOCATION_SAMPLE);
              }
            }
          }
        })
    .addOnFailureListener(
        new OnFailureListener() {
          @Override
          public void onFailure(@NonNull Exception e) {
            Log.e(TAG, "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 a 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:

mListener =
    new OnDataPointListener() {
      @Override
      public void onDataPoint(DataPoint dataPoint) {
        for (Field field : dataPoint.getDataType().getFields()) {
          Value val = dataPoint.getValue(field);
          Log.i(TAG, "Detected DataPoint field: " + field.getName());
          Log.i(TAG, "Detected DataPoint value: " + val);
        }
      }
    };

Fitness.getSensorsClient(this, GoogleSignIn.getLastSignedInAccount(this))
    .add(
        new SensorRequest.Builder()
            .setDataSource(dataSource) // Optional but recommended for custom data sets.
            .setDataType(dataType) // Can't be omitted.
            .setSamplingRate(10, TimeUnit.SECONDS)
            .build(),
        mListener)
    .addOnCompleteListener(
        new OnCompleteListener<Void>() {
          @Override
          public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
              Log.i(TAG, "Listener registered!");
            } else {
              Log.e(TAG, "Listener not registered.", task.getException());
            }
          }
        });

Remove a listener

To remove a listener from raw data updates, use the SensorsClient.remove method:

// Waiting isn't actually necessary as the unregister call will complete regardless,
// even if called from within onStop, but a callback can still be added in order to
// inspect the results.
Fitness.getSensorsClient(this, GoogleSignIn.getLastSignedInAccount(this))
    .remove(mListener)
    .addOnCompleteListener(
        new OnCompleteListener<Boolean>() {
          @Override
          public void onComplete(@NonNull Task<Boolean> task) {
            if (task.isSuccessful() && task.getResult()) {
              Log.i(TAG, "Listener was removed!");
            } else {
              Log.i(TAG, "Listener was not removed.");
            }
          }
        });

Send feedback about...