Giter Club home page Giter Club logo

capacitor-google-fit's Introduction

Google Fit Plugin

Capacitor plugin to retrieve data from Google Fit

Install

npm i --save @perfood/capacitor-google-fit
npx cap sync

Android requirement

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:

1. Get your app's certificate information

  1. Locate your debug keystore file. The file name is debug.keystore

    • macOS : ~/.android
    • Windows : C:\Users\your-user-name.android\
  2. List the SHA-1 fingerprint:

    • macOS
    keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
    
    • Windows
    keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
    

The line that begins with SHA1 contains the certificate's SHA-1 fingerprint.

2. Request an OAuth 2.0 client ID in the Google API Console :
  1. Go to the Google API Console

  2. Create a project or choose existing project

  3. Click Continue to enable the Fitness API.

  4. Click Go to credentials

  5. Now add credential to your project. Note that If this is the first time you configure the project you should "Configure the OAuth consent screen" first Click New credentials, then select OAuth Client ID.

  6. Under Application type select Android.

  7. In the resulting dialog, enter your app's SHA-1 fingerprint and package name. For example:

    BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75
    
    com.example.android.fit-example
    
  8. Click Create. Your new Android OAuth 2.0 Client ID and secret appear in the list of IDs for your project. An OAuth 2.0 Client ID is a string of characters, something like this: 780816631155-gbvyo1o7r2pn95qc4ei9d61io4uh48hl.apps.googleusercontent.com

Set up in Android

Register plugin inside your MainActivity

import com.adscientiam.capacitor.googlefit.GoogleFitPlugin;

...

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    registerPlugin(GoogleFitPlugin.class);
  }

Import plugin

import { Plugins } from '@capacitor/core';
const { GoogleFit } = Plugins;

Supported data types :

Data Type Unit Google Fit equivalent
step count TYPE_STEP_COUNT_DELTA
calories kcal TYPE_CALORIES_EXPENDED
distance m TYPE_DISTANCE_DELTA
weight kg TYPE_WEIGHT
height m TYPE_HEIGHT
activity activityType TYPE_ACTIVITY_SEGMENT

Methods :

connectToGoogleFit()

Plugin method to connect to google fit service using user account. It will first check if the user has permissions to talk to Fitness APIs, otherwise authenticate the user and request required permissions.

getHistory(startTime: string, endTime: string)

A method which retrieve data from a specific time interval provided in parametre:

  • startDate: {type: String}, start date from which to get data
  • endDate: {type: String}, end data to which to get the data Example:
  async getHistory() {
    const today = new Date();
    const lastWeek = new Date(today);
    lastWeek.setDate(lastWeek.getDate() - 7);
    const result = await GoogleFit.getHistory({
      startTime: lastWeek,
      endTime: today
    });
    console.log(result);
  }

getHistoryActivity()

Same as getHistory() method but this time to retrieve activities Returned objects contain a set of fixed fields for each activity:

  • startDate: {type: String} a date indicating when an activity starts
  • endDate: {type: String} a date indicating when an activity ends

capacitor-google-fit's People

Contributors

alemf avatar arnauddrain avatar mahnuh avatar nurizzatiabdharis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

capacitor-google-fit's Issues

connectToGoogleFit Function

Hi, the function "connectToGoogleFit" is a promise, but doesn't wait for a grant/reject status.
How can I override it, and only after permissions granted call the "getHistory"?

Thank

Msg: TypeError: zq.authorize is not a function

Version details:
"@capacitor/core": "^4.6.2",

Steps Follow as per mention in repo.

this is how I call function in app.
await GoogleFit.connectToGoogleFit()

Msg: TypeError: zq.authorize is not a function

after call this function on button click google account selection popup came but after that it close.
google fit permission related dialog with information not coming.

Not installing on Capacitor 5.

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: @capacitor/[email protected]
npm ERR! node_modules/@capacitor/core
npm ERR!   @capacitor/core@"5.0.5" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @capacitor/core@"^3.0.0" from @perfood/[email protected]
npm ERR! node_modules/@perfood/capacitor-google-fit
npm ERR!   @perfood/capacitor-google-fit@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
npm ERR!
npm ERR! For a full report see:
npm ERR! C:\Users\Najam Us Saqib\AppData\Local\npm-cache\_logs\2023-07-28T10_32_21_544Z-eresolve-report.txt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.