/ Miscellaneous

A React Native bridge module for interacting with Google Fit

A React Native bridge module for interacting with Google Fit

react-native-google-fit

A React Native bridge module for interacting with Google Fit.

Getting started

$ npm install react-native-google-fit --save

Enable Google Fitness API for your application

In order for your app to communicate properly with the Google Fitness API you need to enable Google Fit API in your Google API Console.
Also you need to generate new client ID for your app and provide both debug and release SHA keys.
Another step is to configure the consent screen, etc.

More detailed info available at
https://developers.google.com/fit/android/get-api-key

1. In order for the library to work correctly, you'll need following SDK setups:
   
   Android Support Repository
   Android Support Library
   Google Play services
   Google Repository
   Google Play APK Expansion Library
   
2. In order for your app to communicate properly with the Google Fitness API,
   you need to provide the SHA1 sum of the certificate used for signing your
   application to Google. This will enable the GoogleFit plugin to communicate
   with the Fit application in each smartphone where the application is installed.
   https://developers.google.com/fit/android/get-api-key

Mostly Automatic installation

$ react-native link react-native-google-fit

then pass your package name to the module in MainApplication.java (google fit requires package name to save data)

new GoogleFitPackage(BuildConfig.APPLICATION_ID)
Note: Do not change BuildConfig.APPLICATION_ID - it's a constant value.

Manual installation

  1. Open up android/app/src/main/java/[...]/MainApplication.java

    • Add import com.reactnative.googlefit.GoogleFitPackage; to the imports at the top of the file
    • Add new GoogleFitPackage(BuildConfig.APPLICATION_ID), to the list returned by the getPackages() method.
      Note: Do not change BuildConfig.APPLICATION_ID - it's a constant value.
  2. Append the following lines to android/settings.gradle:

    include ':react-native-google-fit'
    project(':react-native-google-fit').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-google-fit/android')
    
  3. Insert the following lines inside the dependencies block in android/app/build.gradle:

      compile project(':react-native-google-fit')
    

USAGE

  1. import GoogleFit from 'react-native-google-fit';

  2. Authorize:

    GoogleFit.onAuthorize(() => {
      dispatch('AUTH SUCCESS');
    });
    
    GoogleFit.onAuthorizeFailure(() => {
      dispatch('AUTH ERROR');
    });
    
    GoogleFit.authorize();
    
    // ...
    // Call when authorized
    GoogleFit.startRecording((callback) => {
      // Process data from Google Fit Recording API (no google fit app needed)
    });
    
  3. Retrieve Steps For Period

    const options = {
      startDate: "2017-01-01T00:00:17.971Z", // required ISO8601Timestamp
      endDate: new Date().toISOString() // required ISO8601Timestamp
    };
    
    GoogleFit.getDailyStepCountSamples(options, (err, res) => {
      if (err) {
        throw err;
      }
    
      console.log("Daily steps >>>", res);
    });
    

Response:

[
  { source: "com.google.android.gms:estimated_steps", steps: [] },
  { source: "com.google.android.gms:merge_step_deltas", steps: [] },
  { source: "com.xiaomi.hm.health", steps: [] }
];
  1. Retrieve Weights

    const opt = {
      unit: "pound", // required; default 'kg'
      startDate: "2017-01-01T00:00:17.971Z", // required
      endDate: new Date().toISOString(), // required
      ascending: false // optional; default false
    };
    
    GoogleFit.getWeightSamples(opt, (err, res) => {
      console.log(res);
    });
    
  2. Save Weights

    const opt = {
      value: 200,
      date: new Date().toISOString(),
      unit: "pound"
    };
    
    GoogleFit.saveWeight(opt, (err, res) => {
      if (err) throw "Cant save data to the Google Fit";
    });
    
  3. Other methods:

    observeSteps(callback); // On Step Changed Event
    
    unsubscribeListeners(); // Put into componentWillUnmount() method to prevent leaks
    
    getDailyCalorieSamples(options, callback); // method to get calories per day
    
    getDailyDistanceSamples(options, callback); // method to get daily distance
    
    isAvailable(callback); // Checks is GoogleFit available for current account / installed on device
    
    isEnabled(callback); // Checks is permissions granted
    
    deleteWeight(options, callback); // method to delete weights by options (same as in save weights)
    

GitHub