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

问题描述

我使用 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)
}

解决方法

好了,我解决了这个问题。仍然是非常奇怪的问题,但是,首先,我的错是不再次检查文档。其次,它的文档和命名也很糟糕。就像您可以编写READ_WRITE一样,它仅意味着WRITE吗?

const paginationPlugin = (schema,options) => {
  options = options || {}
  schema.query.paginate = async function(params) {
    const pagination = {
      limit: options.limit || 10,page: 1,count: 0
    }
    pagination.limit = parseInt(params.limit) || pagination.limit
    const page = parseInt(params.page)
    pagination.page = page > 0 ? page : pagination.page
    const offset = (pagination.page - 1) * pagination.limit

    const [data,count] = await Promise.all([
      this.limit(pagination.limit).skip(offset),this.model.countDocuments(this.getQuery())
    ]);
    pagination.count = count;
    return { data,pagination }
  }
}

mySchema.plugin(PaginatePlugin,{ limit: DEFAULT_LIMIT })

// using async/await
const { data,pagination } = await MyModel.find(...)
  .populate(...)
  .sort(...)
  .paginate({ page: 1,limit: 10 })

// or using Promise
MyModel.find(...).paginate(req.query)
  .then(({ data,pagination }) => {

  })
  .catch(err => {

  })

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

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

所以这个小问题使我花了好几个星期的时间,但这实际上仍然是不合逻辑的,因为它只在一半的设备中使用了两个权限。捕获该错误并了解它是缺少权限的问题。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...