Fitness

public class Fitness extends Object

The main entry point to Google Fit APIs.

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 live 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 in Google Fit.
  • BleApi can be used to work with Bluetooth Low Energy devices.
  • ConfigApi can be used to access custom data types and settings.
  • GoalsApi can be used to read fitness goals created by users in Google Fit.
Most API methods require a data type. Each data type operation requires the user to have granted the app permission to access and store fitness data for the given data type.

Authorization

When connecting to the Google Fit API, apps should specify the necessary scopes and the user account. Apps can use GoogleApiClient.Builder.addScope(Scope) to add the necessary scopes, which should be selected from the SCOPE_XXX constants in this class. To use a specific user account, apps can use GoogleApiClient.Builder.setAccountName(String), or use GoogleApiClient.Builder.useDefaultAccount() to use the default account. The specified account and scopes will be used to acquire the necessary OAuth tokens on behalf of the app.

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 FitnessStatusCodes.NEEDS_OAUTH_PERMISSIONS. In this case, the app should use ConnectionResult.startResolutionForResult(Activity, int) to get the necessary OAuth permissions.

The first connection to Fit API may require a network connection to verify the account and scopes associated with it. In case no network connection is available, Google Fit will send back a result with status code set to CommonStatusCodes.NETWORK_ERROR.

Sample usage of Google Fit Client:

     public class MyActivity extends FragmentActivity
             implements ConnectionCallbacks, OnConnectionFailedListener, OnDataPointListener {
        private static final int REQUEST_OAUTH = 1001;
        private GoogleApiClient mGoogleApiClient;

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Create a Google Fit Client instance with default user account.
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Fitness.SENSORS_API)  // Required for SensorsApi calls
                    // Optional: specify more APIs used with additional calls to addApi
                    .useDefaultAccount()
                    .addScope(Fitness.SCOPE_ACTIVITY_READ_WRITE)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            mGoogleApiClient.connect();
        }

        @Override
        public void onConnected(Bundle connectionHint) {
            // Connected to Google Fit Client.
            Fitness.SensorsApi.add(
                    mGoogleApiClient,
                    new SensorRequest.Builder()
                            .setDataType(DataType.STEP_COUNT_DELTA)
                            .build(),
                    this);
        }

        @Override
        public void onDataPoint(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 && resultCode == RESULT_OK) {
                mGoogleApiClient.connect();
            }
        }
     }
 

Intents

Google Fit supports different intents in order to help fitness apps collaborate. To that effect, different intent actions are defined in this class: Different objects in the Fitness data model can be used in intents, including sessions, data sources, data types, and activities. The documentation for each intent action specifies the different attributes of the intent, including actions, MIME types, and extras. It also specifies how the intents can be built and parsed using methods in the API.

Constant Summary

String ACTION_TRACK Broadcast action: the user has requested that an application start or stop tracking their activity.
String ACTION_VIEW Broadcast action: the user has requested that an application show the value of a particular fitness data type.
String ACTION_VIEW_GOAL Broadcast action: the user has requested to view their current fitness goal.
String EXTRA_END_TIME Name for the long intent extra containing the end time.
String EXTRA_START_TIME Name for the long intent extra containing the start time.

Field Summary

public static final Void API This field is deprecated. In favor of granular API tokens.
public static final Api<Api.ApiOptions.NoOptions> BLE_API This field is deprecated. Use BluetoothManager directly.
public static final BleApi BleApi This field is deprecated. Use BluetoothManager directly.
public static final Api<Api.ApiOptions.NoOptions> CONFIG_API This field is deprecated. For reading and writing historical fitness data, use Health Connect instead.
public static final ConfigApi ConfigApi This field is deprecated. For reading and writing historical fitness data, use Health Connect instead.
public static final Api<Api.ApiOptions.NoOptions> GOALS_API This field is deprecated. For reading and writing historical fitness data, use Health Connect instead.
public static final GoalsApi GoalsApi This field is deprecated. For reading and writing historical fitness data, use Health Connect instead.
public static final Api<Api.ApiOptions.NoOptions> HISTORY_API This field is deprecated. For reading and writing historical fitness data, use Health Connect instead.
public static final HistoryApi HistoryApi This field is deprecated. For reading and writing historical fitness data, use Health Connect instead.
public static final Api<Api.ApiOptions.NoOptions> RECORDING_API This field is deprecated. Use RecordingClient, created via getRecordingClient(Activity, GoogleSignInAccount).
public static final RecordingApi RecordingApi This field is deprecated. Use RecordingClient, created via getRecordingClient(Activity, GoogleSignInAccount).
public static final Scope SCOPE_ACTIVITY_READ Scope for read access to activity-related data types in Google Fit, which include:
public static final Scope SCOPE_ACTIVITY_READ_WRITE Scope for read/write access to activity-related data types in Google Fit, which include:
public static final Scope SCOPE_BODY_READ Scope for read access to the biometric data types in Google Fit, which include:
public static final Scope SCOPE_BODY_READ_WRITE Scope for read/write access to biometric data types in Google Fit, which include:
public static final Scope SCOPE_LOCATION_READ Scope for read access to location-related data types in Google Fit, which include:
public static final Scope SCOPE_LOCATION_READ_WRITE Scope for read/write access to location-related data types in Google Fit, which include:
public static final Scope SCOPE_NUTRITION_READ Scope for read access to the nutrition and hydration data types in Google Fit, which include:
public static final Scope SCOPE_NUTRITION_READ_WRITE Scope for read/write access to nutrition and hydration data types in Google Fit, which include:
public static final Api<Api.ApiOptions.NoOptions> SENSORS_API This field is deprecated. For apps targeting Wear OS 3, use Health Services instead. Otherwise for access to live sensor and location data use SensorManager and FusedLocationProviderClient, respectively.
public static final Api<Api.ApiOptions.NoOptions> SESSIONS_API This field is deprecated. For reading and writing historical fitness data, use Health Connect instead. For recording sessions on Wear OS 3 devices, use Health Services instead.
public static final SensorsApi SensorsApi This field is deprecated. For apps targeting Wear OS 3, use Health Services instead. Otherwise for access to live sensor and location data use SensorManager and FusedLocationProviderClient, respectively.
public static final SessionsApi SessionsApi This field is deprecated. For reading and writing historical fitness data, use Health Connect instead. For recording sessions on Wear OS 3 devices, use Health Services instead.

Public Method Summary

static BleClient
getBleClient(Activity activity, GoogleSignInAccount account)
This method is deprecated. Use BluetoothManager directly.
static BleClient
getBleClient(Context context, GoogleSignInAccount account)
This method is deprecated. Use BluetoothManager directly.
static ConfigClient
getConfigClient(Context context, GoogleSignInAccount account)
This method is deprecated. For reading and writing historical fitness data, use Health Connect instead.
static ConfigClient
getConfigClient(Activity activity, GoogleSignInAccount account)
This method is deprecated. For reading and writing historical fitness data, use Health Connect instead.
static long
getEndTime(Intent intent, TimeUnit timeUnit)
Retrieves the end time extra from the given intent.
static GoalsClient
getGoalsClient(Context context, GoogleSignInAccount account)
This method is deprecated. For reading and writing historical fitness data, use Health Connect instead.
static GoalsClient
getGoalsClient(Activity activity, GoogleSignInAccount account)
This method is deprecated. For reading and writing historical fitness data, use Health Connect instead.
static HistoryClient
getHistoryClient(Activity activity, GoogleSignInAccount account)
This method is deprecated. For reading and writing historical fitness data, use Health Connect instead.
static HistoryClient
getHistoryClient(Context context, GoogleSignInAccount account)
This method is deprecated. For reading and writing historical fitness data, use Health Connect instead.
static RecordingClient
getRecordingClient(Activity activity, GoogleSignInAccount account)
Create a new instance of RecordingClient for use in an Activity.
static RecordingClient
getRecordingClient(Context context, GoogleSignInAccount account)
Create a new instance of RecordingClient for use in a non-activity Context.
static SensorsClient
getSensorsClient(Activity activity, GoogleSignInAccount account)
This method is deprecated. For apps targeting Wear OS 3, use Health Services instead. Otherwise for access to live sensor and location data use SensorManager and FusedLocationProviderClient, respectively.
static SensorsClient
getSensorsClient(Context context, GoogleSignInAccount account)
This method is deprecated. For apps targeting Wear OS 3, use Health Services instead. Otherwise for access to live sensor and location data use SensorManager and FusedLocationProviderClient, respectively.
static SessionsClient
getSessionsClient(Activity activity, GoogleSignInAccount account)
This method is deprecated. For reading and writing historical fitness data, use Health Connect instead. For recording sessions on Wear OS 3 devices, use Health Services instead.
static SessionsClient
getSessionsClient(Context context, GoogleSignInAccount account)
This method is deprecated. For reading and writing historical fitness data, use Health Connect instead. For recording sessions on Wear OS 3 devices, use Health Services instead.
static long
getStartTime(Intent intent, TimeUnit timeUnit)
Retrieves the start time extra from the given intent.

Inherited Method Summary

Constants

public static final String ACTION_TRACK

Broadcast action: the user has requested that an application start or stop tracking their activity. The intent will include the following attributes:

Constant Value: "vnd.google.fitness.TRACK"

public static final String ACTION_VIEW

Broadcast action: the user has requested that an application show the value of a particular fitness data type. This could be an intent to visualize the current value of a data type (such as the current heart rate), or the value of a data type over a period of time. The extras will determine what the particular intent is.

The intent will include the following attributes:

Constant Value: "vnd.google.fitness.VIEW"

public static final String ACTION_VIEW_GOAL

Broadcast action: the user has requested to view their current fitness goal.

Constant Value: "vnd.google.fitness.VIEW_GOAL"

public static final String EXTRA_END_TIME

Name for the long intent extra containing the end time. It can be extracted using getEndTime(Intent, TimeUnit)

Constant Value: "vnd.google.fitness.end_time"

public static final String EXTRA_START_TIME

Name for the long intent extra containing the start time. It can be extracted using getStartTime(Intent, TimeUnit).

Constant Value: "vnd.google.fitness.start_time"

Fields

public static final Void API

This field is deprecated.
In favor of granular API tokens.

Instead of API, you now need to use the specific API for the calls you're making, for example,

 // Create a Google Fit Client instance with default user account.
 mGoogleApiClient = new GoogleApiClient.Builder(this)
         .addApi(Fitness.SENSORS_API)    // Required only if you're making SensorsApi calls
         .addApi(Fitness.RECORDING_API)  // Required only if you're making RecordingApi calls
         .addApi(Fitness.HISTORY_API)    // Required only if you're making HistoryApi calls
         .addApi(Fitness.SESSIONS_API)   // Required only if you're making SessionsApi calls
         // Optional: request more APIs with additional calls to addApi
         .useDefaultAccount()
         .addScope(Fitness.SCOPE_ACTIVITY_READ_WRITE)
         .addOnConnectionsCallbacks(this)
         .addOnConnectionFailedListener(this)
         .build();
 

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

This field is deprecated.
Use BluetoothManager directly.

Token to pass to GoogleApiClient.Builder.addApi(Api ) to enable BleApi.

public static final BleApi BleApi

This field is deprecated.
Use BluetoothManager directly.

Entry point to the Google Fit BLE API.

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

This field is deprecated.
For reading and writing historical fitness data, use Health Connect instead.

public static final ConfigApi ConfigApi

This field is deprecated.
For reading and writing historical fitness data, use Health Connect instead.

Entry point to the Google Fit Config API.

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

This field is deprecated.
For reading and writing historical fitness data, use Health Connect instead.

Token to pass to GoogleApiClient.Builder.addApi(Api ) to enable GoalsApi.

public static final GoalsApi GoalsApi

This field is deprecated.
For reading and writing historical fitness data, use Health Connect instead.

Entry point to the Google Fit Goals API.

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

This field is deprecated.
For reading and writing historical fitness data, use Health Connect instead.

public static final HistoryApi HistoryApi

This field is deprecated.
For reading and writing historical fitness data, use Health Connect instead.

Entry point to the Google Fit History API.

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

public static final RecordingApi RecordingApi

This field is deprecated.
Use RecordingClient, created via getRecordingClient(Activity, GoogleSignInAccount).

Entry point to the Google Fit Recording API.

public static final Scope SCOPE_BODY_READ

Scope for read access to the biometric data types in Google Fit, which include:

public static final Scope SCOPE_BODY_READ_WRITE

Scope for read/write access to biometric data types in Google Fit, which include:

public static final Scope SCOPE_LOCATION_READ

Scope for read access to location-related data types in Google Fit, which include:

public static final Scope SCOPE_LOCATION_READ_WRITE

Scope for read/write access to location-related data types in Google Fit, which include:

public static final Scope SCOPE_NUTRITION_READ

Scope for read access to the nutrition and hydration data types in Google Fit, which include:

public static final Scope SCOPE_NUTRITION_READ_WRITE

Scope for read/write access to nutrition and hydration data types in Google Fit, which include:

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

This field is deprecated.
For apps targeting Wear OS 3, use Health Services instead. Otherwise for access to live sensor and location data use SensorManager and FusedLocationProviderClient, respectively.

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

This field is deprecated.
For reading and writing historical fitness data, use Health Connect instead. For recording sessions on Wear OS 3 devices, use Health Services instead.

public static final SensorsApi SensorsApi

This field is deprecated.
For apps targeting Wear OS 3, use Health Services instead. Otherwise for access to live sensor and location data use SensorManager and FusedLocationProviderClient, respectively.

Entry point to the Google Fit Sensors API.

public static final SessionsApi SessionsApi

This field is deprecated.
For reading and writing historical fitness data, use Health Connect instead. For recording sessions on Wear OS 3 devices, use Health Services instead.

Entry point to the Google Fit Sessions API.

Public Methods

public static BleClient getBleClient (Activity activity, GoogleSignInAccount account)

This method is deprecated.
Use BluetoothManager directly.

Create a new instance of BleClient for use in an Activity. Error resolutions will be automatically launched from the provided Activity, displaying UI when necessary.

public static BleClient getBleClient (Context context, GoogleSignInAccount account)

This method is deprecated.
Use BluetoothManager directly.

Create a new instance of BleClient for use in a non-activity Context. Error resolutions will be automatically launched from the provided Context, displaying system tray notifications when necessary.

public static ConfigClient getConfigClient (Context context, GoogleSignInAccount account)

This method is deprecated.
For reading and writing historical fitness data, use Health Connect instead.

Create a new instance of ConfigClient for use in a non-activity Context. Error resolutions will be automatically launched from the provided Context, displaying system tray notifications when necessary.

public static ConfigClient getConfigClient (Activity activity, GoogleSignInAccount account)

This method is deprecated.
For reading and writing historical fitness data, use Health Connect instead.

Create a new instance of ConfigClient for use in an Activity. Error resolutions will be automatically launched from the provided Activity, displaying UI when necessary.

public static long getEndTime (Intent intent, TimeUnit timeUnit)

Retrieves the end time extra from the given intent.

Parameters
intent The intent to extract the end time from.
timeUnit The desired time unit for the returned end time.
Returns
  • The end time, in time unit since epoch, or -1 if not found.

public static GoalsClient getGoalsClient (Context context, GoogleSignInAccount account)

This method is deprecated.
For reading and writing historical fitness data, use Health Connect instead.

Create a new instance of GoalsClient for use in a non-activity Context. Error resolutions will be automatically launched from the provided Context, displaying system tray notifications when necessary.

public static GoalsClient getGoalsClient (Activity activity, GoogleSignInAccount account)

This method is deprecated.
For reading and writing historical fitness data, use Health Connect instead.

Create a new instance of GoalsClient for use in an Activity. Error resolutions will be automatically launched from the provided Activity, displaying UI when necessary.

public static HistoryClient getHistoryClient (Activity activity, GoogleSignInAccount account)

This method is deprecated.
For reading and writing historical fitness data, use Health Connect instead.

Create a new instance of HistoryClient for use in an Activity. Error resolutions will be automatically launched from the provided Activity, displaying UI when necessary.

public static HistoryClient getHistoryClient (Context context, GoogleSignInAccount account)

This method is deprecated.
For reading and writing historical fitness data, use Health Connect instead.

Create a new instance of HistoryClient for use in a non-activity Context. Error resolutions will be automatically launched from the provided Context, displaying system tray notifications when necessary.

public static RecordingClient getRecordingClient (Activity activity, GoogleSignInAccount account)

Create a new instance of RecordingClient for use in an Activity. Error resolutions will be automatically launched from the provided Activity, displaying UI when necessary.

public static RecordingClient getRecordingClient (Context context, GoogleSignInAccount account)

Create a new instance of RecordingClient for use in a non-activity Context. Error resolutions will be automatically launched from the provided Context, displaying system tray notifications when necessary.

public static SensorsClient getSensorsClient (Activity activity, GoogleSignInAccount account)

This method is deprecated.
For apps targeting Wear OS 3, use Health Services instead. Otherwise for access to live sensor and location data use SensorManager and FusedLocationProviderClient, respectively.

Create a new instance of SensorsClient for use in an Activity. Error resolutions will be automatically launched from the provided Activity, displaying UI when necessary.

public static SensorsClient getSensorsClient (Context context, GoogleSignInAccount account)

This method is deprecated.
For apps targeting Wear OS 3, use Health Services instead. Otherwise for access to live sensor and location data use SensorManager and FusedLocationProviderClient, respectively.

Create a new instance of SensorsClient for use in a non-activity Context. Error resolutions will be automatically launched from the provided Context, displaying system tray notifications when necessary.

public static SessionsClient getSessionsClient (Activity activity, GoogleSignInAccount account)

This method is deprecated.
For reading and writing historical fitness data, use Health Connect instead. For recording sessions on Wear OS 3 devices, use Health Services instead.

Create a new instance of SessionsClient for use in an Activity. Error resolutions will be automatically launched from the provided Activity, displaying UI when necessary.

public static SessionsClient getSessionsClient (Context context, GoogleSignInAccount account)

This method is deprecated.
For reading and writing historical fitness data, use Health Connect instead. For recording sessions on Wear OS 3 devices, use Health Services instead.

Create a new instance of SessionsClient for use in a non-activity Context. Error resolutions will be automatically launched from the provided Context, displaying system tray notifications when necessary.

public static long getStartTime (Intent intent, TimeUnit timeUnit)

Retrieves the start time extra from the given intent.

Parameters
intent The intent to extract the start time from.
timeUnit The desired time unit for the returned start time.
Returns
  • The start time, in time unit since epoch, or -1 if not found.