java.lang.Object | |
↳ | com.google.android.gms.fitness.Fitness |
The main entry point to Google Fit APIs.
When connecting to the Google Fit API, apps should specify the necessary Google Fit scopes and
the user account. Apps can use addScope(Scope)
to add the
necessary scopes. Apps should add the necessary scopes from FitnessScopes
based on the
data access required. To use a specific user account, apps can use
(String)
or use useDefaultAccount()
to use
default account. Fitness Client will use the specified account and scopes to get necessary
OAuth tokens on behalf of the app to ensure that the calling app has the necessary permissions
to access user's Google Fit data.
In case the app does not have the needed OAuth permissions for the requested scopes,
Google Fit will send back a result with status code set
to NEEDS_OAUTH_PERMISSIONS
. In this case, the app should use
startResolutionForResult(Activity, int)
to get the necessary OAuth permissions.
Sample usage of Google Fit Client:
public class MyActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener, DataSourceListener { private static final int REQUEST_OAUTH = 1001; private GoogleApiClient mGoogleApiClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create a Google Fit Client instance with default user account. mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Fitness.API) .useDefaultAccount() .addScope(new Scope(Scopes.FITNESS)) .addOnConnectionsCallbacks(this) .addOnConnectionFailedListener(this) .build(); mGoogleApiClient.connect(); } @Override public void onConnected(Bundle connectionHint) { // Connected to Google Fit Client. Fitness.SensorsApi.register( mGoogleApiClient, new SensorRequest.Builder() .setDataType(DataTypes.STEP_COUNT_DELTA) .build(), this); } @Override public void onEvent(DataPoint dataPoint) { // Do cool stuff that matters. } @Override public void onConnectionSuspended(int cause) { // The connection has been interrupted. Wait until onConnected() is called. } @Override public void onConnectionFailed(ConnectionResult result) { // Error while connecting. Try to resolve using the pending intent returned. if (result.getErrorCode() == FitnessStatusCodes.NEEDS_OAUTH_PERMISSIONS) { try { result.startResolutionForResult(this, REQUEST_OAUTH); } catch (SendIntentException e) { } } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_OAUTH) { if (resultCode == RESULT_OK) { mGoogleApiClient.connect(); } } }The Google Fit APIs help app developers collect and use fitness-related sensor data in their applications. There are several different APIs, each solving a different problem:
SensorsApi
exposes a unified view of sensor streams on the local
device and connected devices, and delivers real-time events to listeners.
RecordingApi
enables low-battery, always-on background collection
of sensor data into the Google Fit store.
SessionsApi
lets apps create and manage sessions of user activity.
HistoryApi
allows querying and insertion of data into the Google Fit store.
BleApi
can be used to work with Bluetooth Low Energy devices.
data type
. The list of supported data types can
be found in DataTypes
. Each data type operation requires the user to have granted the
app permission to access and store fitness data for the given data type.
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
API | Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>) to enable Google Fit services. |
||||||||||
BleApi | Entry point to the Google Fit BLE API . |
||||||||||
ConfigApi | Entry point to the Google Fit History API . |
||||||||||
HistoryApi | Entry point to the Google Fit History API . |
||||||||||
RecordingApi | Entry point to the Google Fit Recording API . |
||||||||||
SensorsApi | Entry point to the Google Fit Sensors API . |
||||||||||
SessionsApi | Entry point to the Google Fit Sessions API . |
[Expand]
Inherited Methods | |||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Token to pass to addApi(Api extends Api.ApiOptions.NotRequiredOptions>)
to enable Google Fit services.