1

I am using Ionic React to build an application that communicates to the fitness data of the user using the ionic Cordova health plugin, but does not seems to work.

import { Health } from "@ionic-native/health/ngx";


const Tab1: React.FC = () => {
  let health = new Health();
  const healtChanger = () => {
        health.isAvailable()
        .then((available:boolean) => {
          console.log(health.isAuthorized(['steps']));
      if(health.isAuthorized(['steps']))
      {
        console.log("Already Authorised");
        health.queryAggregated({
          startDate: new Date(new Date().getTime() - 3 * 24 * 60 * 60 * 1000), // three days ago
          endDate: new Date(), // now
          dataType: 'steps',
          bucket: 'day'
        })
        .then(res => console.log(res))
        .catch(e => console.log(e));
      }
      else {
        health.requestAuthorization([
          'distance', 'nutrition',  //read and write permissions
          {
            read: ['steps'],       //read only permission
          }
        ])
        .then(res => console.log(res))
        .catch(e => console.log(e));
      }
    })
    .catch(e => console.log(e)); 
  };

The code seems to have authorisation but when I try execution it gives an error cannot connect to google fit. The output in chrome inspect for android device is as follows:

main.871c1aec.chunk.js:1 Promise {<pending>}
main.871c1aec.chunk.js:1 Already Authorised
main.871c1aec.chunk.js:1 Cannot connect to Google Fit

Any help shall be greatly appreciated.

4

1 回答 1

1

Before you request access to user fitness data from Google fit app you need to get your sha1 key of your debug. keystore authorised from the Google fit api page and generate a 0auth screen for requesting access. To get your api access and Shaq key kindly visit thisGoogle fit page and follow steps as shown. After you have authorised your sha1 key and package name to get access, you can now remove ur android folder from your project, re-add it and rebuild the apk from Android studio and once you deploy to your device then you will be asked proper access Auth screen to grant access and you will be able to see the data for the same.

requestAuthorization() method must be called before using the query and store methods, even if the authorization has already been given at some point in the past. if we don't call them prior it may cause app crash and google fit will give error cannot connect to google fit.

In your code inside the if statement you need to call requestAuthorization() before health.queryAggregated or health.query :-

    if (health.isAuthorized(["steps"])) {
          console.log("Already Authorised");
          health.requestAuthorization([
              "distance",
              "nutrition", 
              {
                read: ["steps", "height", "weight"], 
                write: ["height", "weight"], 
              },
            ])
            .then((res) => console.log("response " + res))
            .catch((e) => console.log("error " + e));

          health.queryAggregated({
              startDate: new Date(new Date().getTime() - 3 * 24 * 60 * 60 * 1000), 
              endDate: new Date(), // now
              dataType: 'steps',
              bucket: 'day'
            })
            .then(res => console.log(res))
            .catch(e => console.log(e));
        } else {
          health
            .requestAuthorization([
              "distance",
              "nutrition", 
              {
                read: ["steps"], 
                write: ["height", "weight"], 
              },
            ])
            .then((res) => console.log(res))
            .catch((e) => console.log(e));
        }
      })
      .catch((e) => console.log(e));
  };
于 2020-05-07T08:43:54.397 回答