React Native - Google Fit API 无法获取步骤

React Native - Google Fit API can't get steps

我使用 react-native-google-fit 包来获取有关用户的步数数据。在我的例子中,这个包在某些设备上工作正常,但在其他一些设备上无法从 google-fit API.

获得任何数据或响应

我不明白这是什么问题,据我所知 google-fit 的步骤记录 api 在默认情况下并未实现,因为它在 IOS(health-kit ).我实施了 bugnsag 来跟踪步骤,当我调用 google-fit API 获取步骤样本时,过程卡住了。没有错误或异常抛出,感觉就像挂了。

如果有人对此有任何想法或解决方案,我会很高兴听到。

package.json

"dependencies": {
    "@bugsnag/react-native": "^7.3.2",
    "@react-native-community/art": "^1.2.0",
    "@react-native-community/async-storage": "^1.8.1",
    "@react-native-community/cli-platform-ios": "^4.10.1",
    "@react-native-community/datetimepicker": "^3.0.1",
    "@react-native-community/masked-view": "^0.1.7",
    "@react-native-community/viewpager": "^4.1.0",
    "@react-native-firebase/app": "^7.1.0",
    "@react-native-firebase/messaging": "^7.1.0",
    "@react-navigation/native": "^5.0.9",
    "@react-navigation/stack": "^5.1.1",
    "axios": "^0.19.2",
    "dayjs": "^1.8.33",
    "firebase": "^7.14.6",
    "localized-strings": "^0.2.4",
    "react": "16.9.0",
    "react-native": "^0.61.5",
    "react-native-device-info": "^5.5.7",
    "react-native-document-picker": "^3.5.4",
    "react-native-dots-pagination": "^0.1.9",
    "react-native-fs": "^2.16.6",
    "react-native-gesture-handler": "^1.6.0",
    "react-native-get-random-values": "^1.3.1",
    "react-native-gifted-chat": "^0.16.1",
    "react-native-google-fit": "^0.13.0",
    "react-native-highlight-words": "^1.0.1",
    "react-native-image-picker": "^2.3.1",
    "react-native-onesignal": "^3.9.0",
    "react-native-progress": "^4.1.2",
    "react-native-reanimated": "^1.7.0",
    "react-native-safe-area-context": "^0.7.3",
    "react-native-screens": "^2.3.0",
    "react-native-vector-icons": "^6.6.0",
    "react-native-video": "^4.4.5",
    "react-native-webview": "^9.1.4",
    "react-navigation": "^4.2.2",
    "react-navigation-stack": "^2.2.3",
    "react-navigation-tabs": "^2.8.13",
    "react-redux": "^7.2.0",
    "redux": "^4.0.5",
    "redux-logger": "^3.0.6",
    "redux-saga": "^1.1.3",
    "rn-apple-healthkit": "^0.8.0"
  },

android/build.gradle

buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 16
        compileSdkVersion = 28
        targetSdkVersion = 28
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:3.4.2")
        classpath('com.google.gms:google-services:4.3.3')
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node_modules/jsc-android/dist")
        }

        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

android/app/build.gradle

defaultConfig {
        applicationId "com.xx"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 29
        versionName "1.4.18"
        multiDexEnabled true
    }
dependencies {
    implementation project(':react-native-webview')
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-fs')
    implementation 'com.google.firebase:firebase-analytics:17.2.2'
    implementation "com.android.support:support-core-utils:28.0.0"
    implementation "com.android.support:appcompat-v7:28.0.0"
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.facebook.react:react-native:+" 

android.xml permissions

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>

function that calls google-fit-api

function getStepsForAndroid(startDate, endDate) {
    const options = {
        startDate: new Date(startDate).toISOString(), // required ISO8601Timestamp
        endDate: new Date(endDate).toISOString() // required ISO8601Timestamp
    };
    return GoogleFit.getDailyStepCountSamples(options)
}

好吧,我解决了这个问题。这仍然是一个非常奇怪的问题,但是,首先当然是我没有再次检查文档是我的错。秒它糟糕的文档和命名..就像你怎么写 READ_WRITE 它只能意味着 WRITE?

scopes: [
   Scopes.FITNESS_ACTIVITY_READ_WRITE,
   Scopes.FITNESS_BODY_READ_WRITE,
],

这是非常错误的文档,因为在这些示例之后,它显示了如何检索步骤(正如我刚刚遵循的那样)。所以我添加了两个额外的权限而不是顶部,问题就解决了。

 scopes: [
    Scopes.FITNESS_ACTIVITY_READ,
    Scopes.FITNESS_ACTIVITY_READ_WRITE,
    Scopes.FITNESS_BODY_READ,
    Scopes.FITNESS_BODY_READ_WRITE,
 ],

所以这个小问题花了我几周的时间,这实际上仍然不合逻辑,因为它在一半的设备上只使用了两个权限。我 google-fit API 也有问题,因为在这个如果这个 API 可能会失败,我们可以捕获错误并理解它是缺少权限的问题。