【问题标题】:React Native - Google Fit API can't get stepsReact Native - Google Fit API 无法获取步骤
【发布时间】:2020-08-27 09:45:45
【问题描述】:

我使用 react-native-google-fit 包来获取用户的步数数据。在我的情况下,这个包在某些设备上运行良好,但在其他一些设备中无法从 google-fit API 获取任何数据或响应。

我不明白是什么问题,因为我知道 google-fit 的步骤记录 api 没有像在 IOS(health-kit)中那样默认实现。当我调用 google-fit API 来获取步骤样本时,我实现了 bugnsag 来跟踪步骤和过程卡住。没有抛出任何错误或异常,感觉就像挂了。

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

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 权限

<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"/>

调用 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)
}

【问题讨论】:

    标签: android react-native google-play-services google-fit


    【解决方案1】:

    好吧,我解决了这个问题.. 仍然是非常奇怪的问题,但首先当然是我的错,没有再次检查文档。秒其糟糕的文档和命名.. 就像您如何编写 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 可能会失败,我们可以抓住错误,明白是权限不足的问题。

    【讨论】:

    • 你能帮帮我吗?我有一个问题。
    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多