We've made some navigation improvements. Information on Sessions and Data types is under the Guides > Key concepts section.

Getting Started on Android

This guide explains how to start developing with Google Fit on Android. Google Fit is part of Google Play services.

Setup

Get a Google Account

To use the Google Fit APIs, you need a Google Account. If you already have an account, then you're all set. You may also want to create a separate Google Account for testing purposes.

Get Google Play services

Google Fit is available on Android devices with Google Play services 7.0 or higher. Devices running Android 2.3 or higher that have the Google Play Store app automatically receive updates to Google Play services.

To check which version of Google Play services is installed on your device, go to Settings > Apps > Google Play services.

Ensure that you have the latest client library for Google Play services on your development host:

  1. Open the Android SDK Manager.
  2. Under SDK Tools, find Google Play services.
  3. If the status for these packages is different than Installed, select them both and click Install Packages.

Get an OAuth 2.0 client ID

If you haven't already enabled the Fitness API and obtained an OAuth 2.0 client ID, follow the instructions in Get an OAuth 2.0 Client ID to do so now.

Configure your project

Android Studio is the recommended development environment for building an app with the Fitness API. To create a new project, follow the steps described in Creating a Project.

In the IDE, open the build.gradle file for your module and add the Google Play services client library as a dependency:

apply plugin: 'com.android.application'

...

dependencies {
    implementation 'com.google.android.gms:play-services-fitness:18.0.0'
    implementation 'com.google.android.gms:play-services-auth:17.0.0'
}

Connect to the fitness service

To connect to the fitness service before you can use it, complete these steps:

  1. Get the necessary Android permissions: Depending on which data types you plan to use, you might need to get the appropriate Android permissions. For example, if you want to record steps using the com.google.step_count.delta data type, you need to get the Android ACTIVITY_RECOGNITION permission. Read the Authorization guide to check if you need Android permissions.

  2. Get the necessary OAuth authorization: For each data type that you wish to read or write, the user must grant permission. Google Fit asks the user to grant permission to your app to access their fitness data. For more information about scopes and authorization, see the Authorization guide.

  3. Create an API client and use APIs: Having obtained all the necessary permissions, use the appropriate client:

Example

Create the API client as follows:

  1. Create a FitnessOptions instance, declaring the data types and access type (read and/or write) your app needs:

    FitnessOptions fitnessOptions = FitnessOptions.builder()
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .build();
    
  2. Get an instance of the Account object to use with the API:

    GoogleSignInAccount account = GoogleSignIn.getAccountForExtension(this, fitnessOptions);
    
  3. Check if the user has previously granted the necessary data access, and if not, initiate the authorization flow:

    if (!GoogleSignIn.hasPermissions(account, fitnessOptions)) {
        GoogleSignIn.requestPermissions(
                this, // your activity
                GOOGLE_FIT_PERMISSIONS_REQUEST_CODE, // e.g. 1
                account,
                fitnessOptions);
    } else {
        accessGoogleFit();
    }
    
  4. If the authorization flow is required, handle the user's response:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == GOOGLE_FIT_PERMISSIONS_REQUEST_CODE) {
                accessGoogleFit();
            }
        }
    }
    
  5. After the user has authorized access to the data requested, create a fitness client (for example, a HistoryClient to read and/or write historic fitness data) based on your app's purpose and needs:

    private void accessGoogleFit() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.YEAR, -1);
        long startTime = cal.getTimeInMillis();
    
        DataReadRequest readRequest = new DataReadRequest.Builder()
                .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .bucketByTime(1, TimeUnit.DAYS)
                .build();
    
        GoogleSignInAccount account = GoogleSignIn
                .getAccountForExtension(this, fitnessOptions);
    
        Fitness.getHistoryClient(this, account)
                .readData(readRequest)
                .addOnSuccessListener(response -> {
                    // Use response data here
    
                    Log.d(TAG, "OnSuccess()");
                })
                .addOnFailureListener(e -> {
                    Log.d(TAG, "OnFailure()", e);
                });
    }