Access Raw Sensor Data

The Sensors API lets you to 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 SensorsApi.findDataSources method:

// Note: Fitness.SensorsApi.findDataSources() requires the ACCESS_FINE_LOCATION permission.
Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder()
        // At least one datatype must be specified.
        .setDataTypes(DataType.TYPE_LOCATION_SAMPLE)
        // Can specify whether data type is raw or derived.
        .setDataSourceTypes(DataSource.TYPE_RAW)
        .build())
        .setResultCallback(new ResultCallback<DataSourcesResult>() {
            @Override
            public void onResult(DataSourcesResult dataSourcesResult) {
                Log.i(TAG, "Result: " + dataSourcesResult.getStatus().toString());
                for (DataSource dataSource : dataSourcesResult.getDataSources()) {
                    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);
                    }
                }
            }
        });

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 SensorsApi.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.SensorsApi.add(
        mClient,
        new SensorRequest.Builder()
                .setDataSource(dataSource) // Optional but recommended for custom data sets.
                .setDataType(dataType) // Can't be omitted.
                .setSamplingRate(10, TimeUnit.SECONDS)
                .build(),
        mListener)
        .setResultCallback(new ResultCallback<Status>() {
            @Override
            public void onResult(Status status) {
                if (status.isSuccess()) {
                    Log.i(TAG, "Listener registered!");
                } else {
                    Log.i(TAG, "Listener not registered.");
                }
            }
        });

Remove a listener

To remove a listener from raw data updates, use the SensorsApi.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.SensorsApi.remove(
        mClient,
        mListener)
        .setResultCallback(new ResultCallback<Status>() {
            @Override
            public void onResult(Status status) {
                if (status.isSuccess()) {
                    Log.i(TAG, "Listener was removed!");
                } else {
                    Log.i(TAG, "Listener was not removed.");
                }
            }
        });

Send feedback about...