Search code examples
androidreact-nativegoogle-play-servicesgoogle-fit

React Native - Google Fit API can't get steps


I use react-native-google-fit package to get the steps data about the user. In my case this package works fine in some devices but in some others cant get any data or response from the google-fit API.

I cant understand what is the problem, as I know step recording api of the google-fit is not implemented in default as it is in IOS(health-kit). I implemented bugnsag to track the steps and the process stucks when I call google-fit API to get step samples. No error or exception thrown it just feels like hanging.

If anyone have some idea or solution about it I would be very happy to hear.

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

Solution

  • Well I solved the issue.. Still its very weird issue but, first of course its my fault to not check documentation again. Seconds its bad documentation and naming.. Like how you can write READ_WRITE and it can only mean WRITE?

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

    Its very wrong documentation because just after these example it shows how to retrieve steps(as I just followed those). So instead of top I added two extra permissions and the problem is solved.

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

    So this small issue cost me like few weeks which is actually still unlogical because it worked with only two permissions in half of the devices.. Also I google-fit API has problem because in this case this API could fail and we could catch the error and understand that its the problem of lack of permissions.