Android APIs
public class

Fitness

extends Object
java.lang.Object
   ↳ com.google.android.gms.fitness.Fitness

Class Overview

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 three 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.
  • HistoryApi allows querying and insertion of data into the Google Fit store.
Most API methods require a 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.

Summary

Nested Classes
class Fitness.Intents Useful constants and methods for dealing with intents in the Fitness platform. 
Fields
public static final Api<Api.ApiOptions.NoOptions> API Token to pass to addApi(Api) to enable Google Fit services.
public static final BleApi BleApi Entry point to the Fitness BLE API.
public static final HistoryApi HistoryApi Entry point to the Fitness History API.
public static final RecordingApi RecordingApi Entry point to the Fitness Recording API.
public static final SensorsApi SensorsApi Entry point to the Fitness Sensors API.
[Expand]
Inherited Methods
From class java.lang.Object

Fields

public static final Api<Api.ApiOptions.NoOptions> API

Token to pass to addApi(Api) to enable Google Fit services.

public static final BleApi BleApi

Entry point to the Fitness BLE API.

public static final HistoryApi HistoryApi

Entry point to the Fitness History API.

public static final RecordingApi RecordingApi

Entry point to the Fitness Recording API.

public static final SensorsApi SensorsApi

Entry point to the Fitness Sensors API.