public interface

SensorsApi

com.google.android.gms.fitness.SensorsApi

Class Overview

The Google Fit Sensors API provides methods for finding and registering to real time data streams of sensor data.

The API exposes data sources from hardware sensors in the local device and in companion devices. It also exposes data sources from applications. Data sources can be queried via findDataSources(GoogleApiClient, com.google.android.gms.fitness.request.DataSourcesRequest)

The API supports registering and unregistering to real time data streams from any available data source. It also allows for registering based only on the DataType, in which case the best available data source (or a combination of them) is used.

The Sensors API should be accessed from the Fitness entry point. Example:

     GoogleApiClient client = new GoogleApiClient.Builder(context)
         .addApi(Fitness.API)
         ...
         .build();
     client.connect();

     PendingResult<Status> pendingResult = Fitness.SensorsApi.register(
         client,
         new SensorRequest.Builder()
             .setDataType(DataTypes.STEP_COUNT_DELTA)
             .setSamplingDelay(1, TimeUnit.MINUTES)  // sample once per minute
             .build(),
         myStepCountListener);
 
If the application doesn't need real-time sensor updates, but instead wants persistent recording of historical sensor data, for batch querying, the Recording and History APIs and should be used instead.

Summary

Public Methods
abstract PendingResult<DataSourcesResult> findDataSources(GoogleApiClient client, DataSourcesRequest request)
Finds all available data sources, on the device and remotely.
abstract PendingResult<Status> register(GoogleApiClient client, SensorRequest request, DataSourceListener listener)
Register for real time updates from a sensor data source.
abstract PendingResult<Status> register(GoogleApiClient client, SensorRequest request, PendingIntent intent)
Register for real time updates from a sensor data source using a PendingIntent.
abstract PendingResult<Status> unregister(GoogleApiClient client, DataSourceListener listener)
Unregister from real-time updates from a sensor data source.
abstract PendingResult<Status> unregister(GoogleApiClient client, PendingIntent pendingIntent)
Unregister from updates from a sensor data source with a PendingIntent.

Public Methods

public abstract PendingResult<DataSourcesResult> findDataSources (GoogleApiClient client, DataSourcesRequest request)

Finds all available data sources, on the device and remotely. Results are returned asynchronously as a PendingResult.

It's not necessary to call this method if an application is interested only in getting the best available data for a data type, regardless of source. In this case, register(GoogleApiClient, com.google.android.gms.fitness.request.SensorRequest, com.google.android.gms.fitness.request.DataSourceListener) can be used with a generic data type.

Parameters
client an existing GoogleApiClient. It does not need to be connected at the time of this call, but the find operation will be delayed until the connection is complete.
request a built request specifying the data sources we’re interested in finding
Returns
  • a pending result containing the found data sources.

public abstract PendingResult<Status> register (GoogleApiClient client, SensorRequest request, DataSourceListener listener)

Register for real time updates from a sensor data source. This method can be called to register for a particular data source, or for a data type (in which case a default data source is used).

After the registration is successful, new data points in the data stream are delivered to the specified listener. Historic data is not delivered, but can be queried via the History API. When the application is closing, or once real-time data is no longer needed, the registration should be removed. If necessary, the Recording API can be used to persist data for later querying when the application is re-opened.

Applications can call this function several times with the same listener to change the desired sampling rate.

Parameters
client an existing GoogleApiClient. It does not need to be connected at the time of this call, but the register operation will be delayed until the connection is complete
request request specifying the desired data source or data type, as well as the desired parameters for the registration
listener the listener that will be used to respond to events. The listener object should be saved, since it can be used to remove the registration when real time events are no longer needed

public abstract PendingResult<Status> register (GoogleApiClient client, SensorRequest request, PendingIntent intent)

Register for real time updates from a sensor data source using a PendingIntent. This method can be called to register for a particular data source, or for a data type (in which case a default data source is used).

After the registration is successful, new data points in the data stream are delivered to the specified intent. Historic data is not delivered, but can be queried via the History API.

Unlike register(GoogleApiClient, SensorRequest, DataSourceListener), which takes a listener and is intended for fast sampling rates while the application is on the foreground, this method is intended for slower sampling rates without the need to have a service always on in the background.

The application specifies a PendingIntent callback (typically an IntentService) which will be called when new data points are available in the requested stream. When the PendingIntent is called, the application can use extract(android.content.Intent) to extract the DataPoint from the intent. See the documentation of PendingIntent for more details.

Applications can call this function several times with the same intent to change the desired sampling rate.

Parameters
client an existing GoogleApiClient. It does not need to be connected at the time of this call, but the register operation will be delayed until the connection is complete
request request specifying the desired data source or data type, as well as the desired parameters for the registration
intent a callback intent to be sent for each new data points.

public abstract PendingResult<Status> unregister (GoogleApiClient client, DataSourceListener listener)

Unregister from real-time updates from a sensor data source. Should be called whenever real-time data is no longer needed, such as when the activity that displays real-time data is paused, stopped, or destroyed.

Parameters
client an existing GoogleApiClient. Must be connected at the time of this call.
listener the listener that was used for registering to updates in the register request.
Throws
IllegalStateException if client is not connected

public abstract PendingResult<Status> unregister (GoogleApiClient client, PendingIntent pendingIntent)

Unregister from updates from a sensor data source with a PendingIntent. Should be called whenever updates are no longer needed.

Parameters
client an existing GoogleApiClient. Must be connected at the time of this call.
pendingIntent the intent that was used for registering to updates in the register request.
Throws
IllegalStateException if client is not connected