Ein React Native Bridge-Modul für die Interaktion mit Google Fit

Blog

Ein React Native Bridge-Modul für die Interaktion mit Google Fit

Ein React Native Bridge-Modul für die Interaktion mit Google Fit

reagieren-native-google-fit

Gitter-Gruppe - Fragen stellen, Fragen beantworten!

Ein React Native Bridge-Modul für die Interaktion mit Google Fit

VERWENDUNGSZWECK

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

  2. Autorisieren:

    Um zu prüfen, ob GoogleFit bereits autorisiert ist, nutzen Sie einfach eine Funktion

    GoogleFit.checkIsAuthorized()

    Dann können Sie sich einfach auf |_+_| . beziehen boolesch.

    GoogleFit.isAuthorized

    Alternativ können Sie Ereignis-Listener verwenden (veraltet)

    So melden Sie sich ohne Telefonnummer bei der Cash-App an
    // The list of available scopes inside of src/scopes.js file const options = { scopes: [ Scopes.FITNESS_ACTIVITY_READ_WRITE, Scopes.FITNESS_BODY_READ_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) });
  3. Schritte für Periode abrufen

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

    Antwort:

    const options = { startDate: '2017-01-01T00:00:17.971Z', // required ISO8601Timestamp endDate: new Date().toISOString() // required ISO8601Timestamp }; GoogleFit.getDailyStepCountSamples(options) .then((res) => { console.log('Daily steps >>> ', res) }) .catch((err) => {console.warn(err)}) // 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()
  4. Gewichte abrufen

    [ { 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: [] } ];

    Antwort:

    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); });
  5. Höhen abrufen

    [ { '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' } ]

    Antwort:

    const opt = { startDate: '2017-01-01T00:00:17.971Z', // required endDate: new Date().toISOString(), // required }; GoogleFit.getHeightSamples(opt, (err, res) => { console.log(res); });
  6. Gewichte speichern

    [ { 'addedBy': 'app_package_name', 'value':1.7699999809265137, 'endDate':'2019-06-29T15:02:23.409Z', 'startDate':'2019-06-29T15:02:23.409Z', 'day':'Sat' } ]
  7. Blutdruck- und Herzfrequenzmethoden (seit Version 0.8)

    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'; });

    Antwort:

    const options = { startDate: '2017-01-01T00:00:17.971Z', // required endDate: new Date().toISOString(), // required } const callback = ((error, response) => { console.log(error, response) }); GoogleFit.getHeartRateSamples(options, callback) GoogleFit.getBloodPressureSamples(options, callback)
  8. Alle Aktivitäten abrufen

    // heart rate [ { 'value':80, 'endDate':'2019-07-26T10:19:21.348Z', 'startDate':'2019-07-26T10:19:21.348Z', 'day':'Fri' } ] // blood pressure [ { 'value':120, 'value2':80, 'endDate':'2019-07-26T08:39:28.493Z', 'startDate':'1970-01-01T00:00:00.000Z', 'day':'Thu' } ]

    Antwort:

    Dynamische Dropdown-Liste für Google-Formulare
    let options = { startDate: new Date(2018, 9, 17).valueOf(), // simply outputs the number of milliseconds since the Unix Epoch endDate: new Date(2018, 9, 18).valueOf() }; GoogleFit.getActivitySamples(options, (err, res) => { console.log(err, res) });

    Woher:

    [ { 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', }]

    Beachten Sie, dass optionale Parameter nicht in allen Aktivitäten angezeigt werden - nur wenn google fit einige Ergebnisse für dieses Feld zurückgibt. Wie keine Distanz für stille Aktivität.

  9. Abrufen von Kalorien für die Periode

    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

    Antwort:

    Crashkurs Lineare Algebra
    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 }; GoogleFit.getDailyCalorieSamples(opt, (err, res) => { console.log(res); });
  10. Abrufen der Entfernung für den Zeitraum:

[ { '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' } ]

Antwort:

```js const opt = { startDate: '2017-01-01T00:00:17.971Z', // required endDate: new Date().toISOString(), // required }; GoogleFit.getDailyDistanceSamples(opt, (err, res) => { console.log(res); }); ```
  1. Abrufen der täglichen Nährwertdaten für die Periode:
```js [ { 'distance':2254.958251953125, 'endDate':'2019-06-30T15:45:32.987Z', 'startDate':'2019-06-29T16:57:01.047Z', 'day':'Sat' }, { 'distance':3020.439453125, 'endDate':'2019-07-01T13:08:31.332Z', 'startDate':'2019-06-30T16:58:44.818Z', 'day':'Sun' } ] ```

Antwort:

```js const opt = { startDate: '2017-01-01T00:00:17.971Z', // required endDate: new Date().toISOString(), // required }; GoogleFit.getDailyNutritionSamples(opt, (err, res) => { console.log(res); }); ```
  1. Hydratation abrufen
```js [ { 'nutrients':{'sugar':14,'sodium':1,'calories':105,'potassium':422}, 'date':'2019-07-02' }, { 'nutrients':{'sugar':36,'iron':0,'fat.saturated':3.6000001430511475,'sodium':0.13500000536441803,'fat.total':6,'calories':225,'fat.polyunsaturated':0,'carbs.total':36,'potassium':0.21000000834465027,'cholesterol':0.029999999329447746,'protein':9.299999237060547}, 'date':'2019-07-25' } ] ```

Antwort:

You need to add `FITNESS_NUTRITION_READ_WRITE` scope to your authorization to work with hydration. ```js const startDate = '2020-01-05T00:00:17.971Z'; // required const endDate = new Date().toISOString(); // required oogleFit.getHydrationSamples(startDate, endDate, (err, res) => { console.log(res); }); ```
  1. Hydratation speichern
```js [ { 'addedBy': 'app_package_name', 'date': '2020-02-01T00:00:00.000Z', 'waterConsumed': '0.225' }, { 'addedBy': 'app_package_name', 'date': '2020-02-02T00:00:00.000Z', 'waterConsumed': '0.325' }, ] ```
  1. Hydratation löschen
This method can update hydration data. An app cannot update data inserted by other apps. ```js const hydrationArray = [ { date: Date.parse('2020-02-01'), // required, timestamp waterConsumed: 0.225, // required, hydration data for a 0.225 liter drink of water }, { date: Date.parse('2020-02-02'), waterConsumed: 0.325, }, ]; GoogleFit.saveHydration(hydrationArray, (err, res) => { if (err) throw 'Cant save data to the Google Fit'; }); ```
  1. Andere Methoden:
An app cannot delete data inserted by other apps. startDate and endDate MUST not be the same. ```js const options = { startDate: '2020-01-01T12:33:18.873Z', // required, timestamp or ISO8601 string endDate: new Date().toISOString(), // required, timestamp or ISO8601 string }; GoogleFit.deleteHydration(options, (err, res) => { console.log(res); }); ```

Download-Details:

Autor: StasDoskalenko

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

#reagieren-nativ #reagieren #mobile-apps