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



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

我不明白问题出在哪里,因为我知道谷歌fit的步骤记录api并没有像在IOS(健康工具包(中那样默认实现。当我调用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)
}

我很好地解决了这个问题。。这仍然是一个非常奇怪的问题,但首先当然是我的错,没有再次检查文档。秒它糟糕的文档和命名。。比如你如何写READ_write,它只能意味着写?

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

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

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

因此,这个小问题花了我大约几周的时间,但实际上仍然不合乎逻辑,因为它在一半的设备中只使用了两个权限。。此外,我觉得API有问题,因为在这种情况下,这个API可能会失败,我们可以发现错误,并理解这是缺乏权限的问题。

最新更新