INTRO

This project contains a Cordova plugin for the GoogleFit platform. Any pull request considered ;)

If you seek a plugin that can 'talk' to both Android and iOS please check this work done by a colleague of mine. It also operates with HealthKit, which makes it less GoogleFit oriented... but more Cordova style!

Caveats

The plugin is still in development and is provided "AS IS". It works only to read certain data from the GoogleFit API and is not yet ready to store any data.

Content

Here the breakdown of the content

Using the plugin

You can install the plugin by using the cordova npm repository.

cordova plugin add org.velardo.cordova-plugin-googlefit

To use the plugin you need to call the specific functions.

var startTime = new Date().getTime() - 3 * 24 * 60 * 60 * 1000; // three days ago
var endTime = new Date().getTime(); // now
window.plugins.googlefit.getData(
                  startTime,        // Start time in milliseconds
                  endTime,          // End time in milliseconds
                  datatypes,        // Datatypes under the URL format specified by GoogleFit
                  function(data) {
                    // Success callback. The data object is a JSON that follows
                    // the structure of GoogleFit data structures
                  },
                  function(e) {
                    // The error e is returned in case of problems with the query
                  });

window.plugins.googlefit.getAggregateData(
                  startTime,        // Start time in milliseconds
                  endTime,          // End time in milliseconds
                  datatypes,        // Datatypes under the URL format specified by GoogleFit
                  dataaggregations, // Aggregate datatypes under the URL format specified by GoogleFit
                  1,                // Duration value of the databucket
                  "DAYS",           // TimeUnit that quantify the duration unit (DAYS, HOURS, MINUTES, SECONDS)
                  0,                // Type of the Buckets (0: ByTime, 1: ByActivityType, 2: ByActivitySegment)
                  function(data) {
                    // Success callback. The data object is a JSON that follows
                    // the structure of GoogleFit data structures
                  },
                  function(e) {
                    // The error e is returned in case of problems with the query
                  });

Valid DataTypes

At the moment the datatypes that are readable from the GoogleFit API are listed below.

Use them by placing the corresponding GoogleFit URL notation in the arrays passed to the getData and getAggregateData JavaScript calls.

DataType URL format
DataType.TYPE_STEP_COUNT_CADENCE com.google.step_count.cadence
DataType.TYPE_STEP_COUNT_DELTA com.google.step_count.delta
DataType.AGGREGATE_STEP_COUNT_DELTA com.google.step_count.delta
DataType.TYPE_CALORIES_EXPENDED com.google.calories.expended
DataType.AGGREGATE_CALORIES_EXPENDED com.google.calories.expended
DataType.TYPE_CALORIES_CONSUMED (deprecated) com.google.calories.consumed
DataType.AGGREGATE_CALORIES_CONSUMED (deprecated) com.google.calories.consumed
DataType.TYPE_NUTRITION com.google.nutrition
DataType.AGGREGATE_NUTRITION_SUMMARY com.google.nutrition.summary
DataType.TYPE_DISTANCE_DELTA com.google.distance.delta
DataType.AGGREGATE_DISTANCE_DELTA com.google.distance.delta
DataType.TYPE_HEIGHT com.google.height
DataType.TYPE_SPEED com.google.speed
DataType.AGGREGATE_SPEED_SUMMARY com.google.speed.summary
DataType.TYPE_ACTIVITY_SAMPLE com.google.activity.sample
DataType.TYPE_ACTIVITY_SEGMENT com.google.activity.segment
DataType.AGGREGATE_ACTIVITY_SUMMARY com.google.activity.summary
DataType.TYPE_WORKOUT_EXERCISE com.google.activity.exercise
DataType.TYPE_WEIGHT com.google.weight
DataType.AGGREGATE_WEIGHT_SUMMARY com.google.weight.summary
DataType.TYPE_BODY_FAT_PERCENTAGE com.google.body.fat.percentage
DataType.AGGREGATE_BODY_FAT_PERCENTAGE_SUMMARY com.google.body.fat_percentage.summary
DataType.TYPE_HEART_RATE_BPM com.google.heart_rate.bpm
DataType.AGGREGATE_HEART_RATE_SUMMARY com.google.heart_rate.summary
DataType.TYPE_POWER_SAMPLE com.google.power.sample
DataType.AGGREGATE_POWER_SUMMARY com.google.power.summary
DataType.TYPE_CYCLING_PEDALING_CADENCE com.google.cycling.pedaling.cadence
DataType.TYPE_CYCLING_PEDALING_CUMULATIVE com.google.cycling.pedaling.cumulative
DataType.TYPE_CYCLING_WHEEL_REVOLUTION com.google.cycling.wheel_revolution.cumulative
DataType.TYPE_CYCLING_WHEEL_RPM com.google.cycling.wheel_revolution.rpm
DataType.TYPE_LOCATION_SAMPLE com.google.location.sample
DataType.AGGREGATE_LOCATION_BOUNDING_BOX com.google.location.bounding_box

Test the GoogleFit Cordova plugin

SDK requirements for compiling the plugin

In order for the plugin to compile you need to install the

Android Support Repository
Android Support Library
Google Play services
Google Repository
Google Play APK Expansion Library

Image of Android SDK configuration

How to enable the Google Fitness API for your application

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.

To do this:

Online resources for GoogleFit

Data Types

Activity Types