java.lang.Object | ||
↳ | com.google.android.gms.common.api.GoogleApi<com.google.android.gms.fitness.FitnessOptions> | |
↳ | com.google.android.gms.fitness.SensorsClient |
Client which exposes different sources of fitness data in local and connected devices, and delivers live events to listeners.
The Client 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(DataSourcesRequest)
The Client supports adding and removing listeners to live data streams from any available data
source. It also allows for listening on a DataType
, in which case the best available data
source (or a combination of them) is used.
The Sensors Client should be used whenever live updates from a sensor stream need to be pushed
to the application (for instance, to update a UI). The History Client
can
be used to query historical data in a pull-based model for scenarios where latency isn't
critical.
The Sensors Client should be accessed from the Fitness
entry point. Example:
GoogleSignInOptionsExtension extension = FitnessOptions.builder() .addDataType(DataTypes.TYPE_STEP_COUNT_DELTA, FitnessOptions.READ) .build() GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .addExtension(extension) .build(); Task<GoogleSignInAccount> task = GoogleSignIn.getClient(this, signInOptions) .silentSignIn(); GoogleSignInAccount googleSigninAccount = Tasks.await(task); Task<Void> response = Fitness.getSensorsClient(this, googleSigninAccount) .add(new SensorRequest.Builder() .setDataType(DataType.TYPE_STEP_COUNT_DELTA) .setSamplingDelay(1, TimeUnit.MINUTES) // sample once per minute .build(), myStepCountListener);If the application doesn't need live sensor updates, but instead wants persistent recording of historical sensor data, for batch querying, the
Recording
and History Clients
and should be used instead.
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Adds a data point listener to a sensor data source.
| |||||||||||
Adds a
PendingIntent listener to a sensor data source. | |||||||||||
Finds all available data sources, on the device and remotely.
| |||||||||||
Removes PendingIntent listener from a sensor data source.
| |||||||||||
Removes a listener from a sensor data source.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Adds a data point listener to a sensor data source. This method can be called to listen on live updates from a particular data source, or data type (in which case a default data source is used).
Once the add request succeeds, 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 Client
. When the application is closing, or once live data is no longer
needed, the listener should be removed. If necessary, the Recording
Client
can be used to persist data for later querying when the application is re-opened in a
battery-efficient manner.
This method can be called several times with the same listener to change the desired sampling rate.
Parameters | |
---|---|
request |
SensorRequest : request specifying the desired data source or data type, as well as the desired
parameters for the add request |
listener |
OnDataPointListener : 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 live events are no longer
needed |
Returns | |
---|---|
Task<Void> |
Throws | |
---|---|
SecurityException |
if a required permission is missing for the requested DataType or DataSource .
|
Adds a PendingIntent
listener to a sensor data source. This method can be called to
listen on live updates from a particular data source, or a data type (in which case a default
data source is used).
Once the add request succeeds, 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 Client
.
Unlike add(SensorRequest, OnDataPointListener)
, 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 for an always-on service.
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.
Any previously registered requests that have the same PendingIntent (as defined by equals(Object)
) will be replaced by this request.
Parameters | |
---|---|
request |
SensorRequest : request specifying the desired data source or data type, as well as the desired
parameters for the add request |
intent |
PendingIntent : a callback intent to be sent for each new data points. |
Returns | |
---|---|
Task<Void> |
Throws | |
---|---|
SecurityException |
if a required permission is missing for the requested DataType or DataSource .
|
Finds all available data sources, on the device and remotely. Results are returned
asynchronously as a task
.
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, add(SensorRequest, OnDataPointListener)
can be used with a generic data type
.
Parameters | |
---|---|
request |
DataSourcesRequest : a built request specifying the data sources we’re interested in finding |
Returns | |
---|---|
Task<List<DataSource>> |
a task containing the found data sources.
|
Removes PendingIntent listener from a sensor data source. Should be called whenever live updates are no longer needed.
Parameters | |
---|---|
pendingIntent |
PendingIntent : the PendingIntent that was used in the add request or is equal as defined by equals(Object) . |
Returns | |
---|---|
Task<Void> |
Throws | |
---|---|
IllegalStateException |
if client is not connected |
Removes a listener from a sensor data source. Should be called whenever live updates are no longer needed, such as when the activity that displays live data is paused, stopped, or destroyed.
Parameters | |
---|---|
listener |
OnDataPointListener : the listener that was used in the add request. |
Returns | |
---|---|
Task<Boolean> |
Throws | |
---|---|
IllegalStateException |
if client is not connected |