• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

StasDoskalenko/react-native-google-fit: A React Native bridge module for interac ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

StasDoskalenko/react-native-google-fit

开源软件地址(OpenSource Url):

https://github.com/StasDoskalenko/react-native-google-fit

开源编程语言(OpenSource Language):

Java 81.1%

开源软件介绍(OpenSource Introduction):

react-native-google-fit

Gitter Group - ask questions, answer questions!

npm version Downloads

A React Native bridge module for interacting with Google Fit

Quick Links

Requirement

If you didn't set fitnessVersion manually, you can simply skip this part.
Note that 0.16.1 require fitness version above 20.0.0
Please read https://developers.google.com/fit/improvements why we made the changes.

Android 11

For Android 11, If you want to interact with Google Fit App.
For example, use openFit(),isAvailable(callback). Otherwise ignore it.
Add the following queries into your AndroidManifest.xml

<queries>
    <package android:name="com.google.android.apps.fitness" />
</queries>

USAGE

  1. import GoogleFit, { Scopes } from 'react-native-google-fit'

2. Authorize:

To check whethere GoogleFit is already authorized, simply use a function, then you can refer to the static property GoogleFit.isAuthorized

GoogleFit.checkIsAuthorized().then(() => {
    console.log(GoogleFit.isAuthorized) // Then you can simply refer to `GoogleFit.isAuthorized` boolean.
})

or with async/await syntax

await checkIsAuthorized();
console.log(GoogleFit.isAuthorized);
// The list of available scopes inside of src/scopes.js file
const options = {
  scopes: [
    Scopes.FITNESS_ACTIVITY_READ,
    Scopes.FITNESS_ACTIVITY_WRITE,
    Scopes.FITNESS_BODY_READ,
    Scopes.FITNESS_BODY_WRITE,
  ],
}
GoogleFit.authorize(options)
  .then(authResult => {
    if (authResult.success) {
      dispatch("AUTH_SUCCESS");
    } else {
      dispatch("AUTH_DENIED", authResult.message);
    }
  })
  .catch(() => {
    dispatch("AUTH_ERROR");
  })

// ...
// Call when authorized
GoogleFit.startRecording((callback) => {
  // Process data from Google Fit Recording API (no google fit app needed)
});

Note: If you are using the recording API for location/ distance data, you have to request the location-permission in your app's AndroidManifest.xml: <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Alternatively you can use event listeners (deprecated)

 GoogleFit.onAuthorize(() => {
   dispatch('AUTH SUCCESS')
 })

 GoogleFit.onAuthorizeFailure(() => {
   dispatch('AUTH ERROR')
 })

3. Retrieve Steps For Period

const opt = {
  startDate: "2017-01-01T00:00:17.971Z", // required ISO8601Timestamp
  endDate: new Date().toISOString(), // required ISO8601Timestamp
  bucketUnit: BucketUnit.DAY, // optional - default "DAY". Valid values: "NANOSECOND" | "MICROSECOND" | "MILLISECOND" | "SECOND" | "MINUTE" | "HOUR" | "DAY"
  bucketInterval: 1, // optional - default 1. 
};

GoogleFit.getDailyStepCountSamples(opt)
 .then((res) => {
     console.log('Daily steps >>> ', res)
 })
 .catch((err) => {console.warn(err)});

// or with async/await syntax
async function fetchData() {
  const res = await GoogleFit.getDailyStepCountSamples(opt)
  console.log(res);
}

// shortcut functions, 
// return weekly or daily steps of given date
// all params are optional, using new Date() without given date, 
// adjustment is 0 by default, determine the first day of week, 0 == Sunday, 1==Monday, etc.
GoogleFit.getDailySteps(date).then().catch()
GoogleFit.getWeeklySteps(date, adjustment).then().catch()

Response:

[
  { source: "com.google.android.gms:estimated_steps", steps: [
    {
      "date":"2019-06-29","value":2328
    },
    {
      "date":"2019-06-30","value":8010
      }
    ]
  },
  { source: "com.google.android.gms:merge_step_deltas", steps: [
    {
      "date":"2019-06-29","value":2328
    },
    {
      "date":"2019-06-30","value":8010
      }
    ]
  },
  { source: "com.xiaomi.hm.health", steps: [] }
];

Note: bucket Config for step reflects on rawStep entity.

Response:

// {bucketInterval: 15, bucketUnit: BucketUnit.MINUTE}
[
  { source: "com.google.android.gms:estimated_steps", 
    steps: [
    {
      "date":"2019-07-06","value": 135
    },
    ],
    rawSteps: [
      {"endDate": 1594012101944, "startDate": 1594012041944, "steps": 13}, 
      {"endDate": 1594020600000, "startDate": 1594020596034, "steps": 0}, 
      {"endDate": 1594020693175, "startDate": 1594020600000, "steps": 24}, 
      {"endDate": 1594068898912, "startDate": 1594068777409, "steps": 53}, 
      {"endDate": 1594073158830, "startDate": 1594073066166, "steps": 45}
    ]
  },
]

// {bucketInterval: 1, bucketUnit: BucketUnit.DAY}
[
    { source: "com.google.android.gms:estimated_steps",
        ...
      rawSteps: [
       {"endDate": 1594073158830, "startDate": 1594012041944, "steps": 135}
      ]
    }
]

4. Retrieve Weights

const opt = {
  unit: "pound", // required; default 'kg'
  startDate: "2017-01-01T00:00:17.971Z", // required
  endDate: new Date().toISOString(), // required
  bucketUnit: BucketUnit.DAY, // optional - default "DAY". Valid values: "NANOSECOND" | "MICROSECOND" | "MILLISECOND" | "SECOND" | "MINUTE" | "HOUR" | "DAY"
  bucketInterval: 1, // optional - default 1. 
  ascending: false // optional; default false
};

GoogleFit.getWeightSamples(opt).then((res)=> {
  console.log(res)
});
// or with async/await syntax
async function fetchData() {
  const res = await GoogleFit.getWeightSamples(opt)
  console.log(res);
}

Response:

[
  {
    "addedBy": "app_package_name",
    "value":72,
    "endDate":"2019-06-29T15:02:23.413Z",
    "startDate":"2019-06-29T15:02:23.413Z",
    "day":"Sat"
  },
  {
    "addedBy": "app_package_name",
    "value":72.4000015258789,
    "endDate":"2019-07-26T08:06:42.903Z",
    "startDate":"2019-07-26T08:06:42.903Z",
    "day":"Fri"
  }
]

5. Retrieve Heights

const opt = {
  startDate: "2017-01-01T00:00:17.971Z", // required
  endDate: new Date().toISOString(), // required
};

GoogleFit.getHeightSamples(opt).then((res)=> {
  console.log(res);
});

Response:

[
  {
    "addedBy": "app_package_name",
    "value":1.7699999809265137, // Meter
    "endDate":"2019-06-29T15:02:23.409Z",
    "startDate":"2019-06-29T15:02:23.409Z",
    "day":"Sat"
  }
]

6. 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";
});

7. Blood pressure and Heart rate methods (since version 0.8)

Heartrate Scopes: 
    [
        Scopes.FITNESS_ACTIVITY_READ,
        Scopes.FITNESS_ACTIVITY_WRITE,
        Scopes.FITNESS_HEART_RATE_READ,
        Scopes.FITNESS_HEART_RATE_WRITE,
    ];
Blood pressure: 
    [
        FITNESS_BLOOD_PRESSURE_READ,
        FITNESS_BLOOD_PRESSURE_WRITE,
        FITNESS_BLOOD_GLUCOSE_READ,
        FITNESS_BLOOD_GLUCOSE_WRITE,
    ];
const options = {
  startDate: "2017-01-01T00:00:17.971Z", // required
  endDate: new Date().toISOString(), // required
  bucketUnit: BucketUnit.DAY, // optional - default "DAY". Valid values: "NANOSECOND" | "MICROSECOND" | "MILLISECOND" | "SECOND" | "MINUTE" | "HOUR" | "DAY"
  bucketInterval: 1, // optional - default 1. 
}

async function fetchData() {
  const heartrate = await GoogleFit.getHeartRateSamples(opt)
  console.log(heartrate);

  const bloodpressure = await GoogleFit.getBloodPressureSamples(opt)
  console.log(bloodpressure);
}

Response:

// heart rate
[
  {
    "value":80,
    "endDate":"2019-07-26T10:19:21.348Z",
    "startDate":"2019-07-26T10:19:21.348Z",
    "day":"Fri"
  }
]

// blood pressure
[
  {
    "systolic":120,
    "diastolic":80,
    "endDate":"2019-07-26T08:39:28.493Z",
    "startDate":"1970-01-01T00:00:00.000Z",
    "day":"Thu"
  }
]

8. Get all activities


Require scopes: Scopes.FITNESS_ACTIVITY_READ & Scopes.FITNESS_LOCATION_READ
Add <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> to AndroidManifest.xml

  let opt = {
    startDate: "2017-01-01T00:00:17.971Z", // required
    endDate: new Date().toISOString(), // required
    bucketUnit: BucketUnit.DAY, // optional - default "DAY". Valid values: "NANOSECOND" | "MICROSECOND" | "MILLISECOND" | "SECOND" | "MINUTE" | "HOUR" | "DAY"
    bucketInterval: 1, // optional - default 1. 
  };

  GoogleFit.getActivitySamples(opt).then((res)=> {
    console.log(res)
  });
  // or with async/await syntax
  async function fetchData() {
    const res = await GoogleFit.getActivitySamples(opt)
    console.log(res);
  }

Response:

 [ { 
  sourceName: 'Android',
  device: 'Android',
  sourceId: 'com.google.android.gms',
  calories: 764.189208984375,
  quantity: 6,
  end: 1539774300992,
  tracked: true,
  activityName: 'still',
  start: 1539727200000 },
{ sourceName: 'Android',
  device: 'Android',
  sourceId: 'com.google.android.gms',
  calories: 10.351096153259277,
  quantity: 138,
  end: 1539774486088,
  tracked: true,
  distance: 88.09545135498047,
  activityName: 'walking',
}]

Where:

sourceName = device - 'Android' or 'Android Wear' string
sourceId - return a value of dataSource.getAppPackageName(). For more info see: https://developers.google.com/fit/android/data-attribution
start/end - timestamps of activity in format of milliseconds since the Unix Epoch
tracked - bool flag, is this activity was entered by user or tracked by device. Detected by checking milliseconds of start/end timestamps. Since when user log activity in googleFit they can't set milliseconds
distance(opt) - A distance in meters.
activityName - string, equivalent one of these https://developers.google.com/fit/rest/v1/reference/activity-types 
calories(opt) - double value of burned Calories in kcal.
quantity(opt) - equivalent of steps number

Note that optional parametrs are not presented in all activities - only where google fit return some results for this field. Like no distance for still activity.

9. Retrieve Calories For Period

  const opt = {
    startDate: "2017-01-01T00:00:17.971Z", // required
    endDate: new Date().toISOString(), // required
    basalCalculation: true, // optional, to calculate or not basalAVG over the week
    bucketUnit: BucketUnit.DAY, // optional - default "DAY". Valid values: "NANOSECOND" | "MICROSECOND" | "MILLISECOND" | "SECOND" | "MINUTE" | "HOUR" | "DAY"
    bucketInterval: 1, // optional - default 1. 
  };

  GoogleFit.getDailyCalorieSamples(opt).then((res) => {
    console.log(res);
  });

Response:

[
  {
    "calorie":1721.948974609375,
    "endDate":"2019-06-27T15:13:27.000Z",
    "startDate":"2019-06-27T15:02:23.409Z",
    "day":"Thu"
  },
  {
    "calorie":1598.25,
    "endDate":"2019-06-28T15:13:27.000Z",
    "startDate":"2019-06-27T15:13:27.000Z",
    "day":"Thu"
  }
]

10. Retrieve Distance For Period:


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap