0

我使用 Google fit 通过 react-native-google-fit 跟踪用户步骤。在获得跟踪活动和身体的用户权限后,我收到一个错误,我需要向活动识别用户请求权限。

这是我添加的xml(也尝试添加LOCATION):

  <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
  <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
 <!-- <uses-permission android:name="com.google.android.gms.permission.LOCATION"/> --> <---- tried with this as well

这是 gradle.build 中的 sdk 版本:

  buildToolsVersion = "28.0.3"
  minSdkVersion = 21
  compileSdkVersion = 28
  targetSdkVersion = 29

还添加(在 gradle.build 中):

    implementation 'com.google.android.gms:play-services-fitness:20.0.0'
    implementation 'com.google.android.gms:play-services-location:18.0.0'
    implementation 'com.google.android.gms:play-services-auth:19.2.0'

这是代码(Google Fit)执行的地方(HomePage.js - 用户登录后):

useEffect(() => {
        const options = {
            scopes: [
                Scopes.FITNESS_ACTIVITY_READ,
                Scopes.FITNESS_ACTIVITY_WRITE,
                Scopes.FITNESS_BODY_READ,
                Scopes.FITNESS_BODY_WRITE,
                Scopes.FITNESS_LOCATION_READ, <---- also tried without
                Scopes.FITNESS_LOCATION_WRITE, <---- also tried without
            ],
        };

        GoogleFit.authorize(options)
            .then(authResult => {
                if (authResult.success) {
                    alert('AUTH_SUCCESS'); <------ alert success
                        GoogleFit.startRecording((callback) => { <----- application crash (see attached image)
                        });
            
                } else {
                    alert('AUTH_DENIED', authResult.message);
                }
            })
            .catch(() => {
                alert('AUTH_ERROR');
            });
    }, []);

它不会在模拟器上崩溃,只会在物理设备上崩溃。我使用的物理设备是带有 android 10 的 android。我需要适应版本 >= 8。

任何帮助将不胜感激!

在此处输入图像描述

4

1 回答 1

0

所以谁有兴趣。我们设法通过使用库来解决它:react-native-permissions 所以:

import { check, PERMISSIONS, request, RESULTS } from 'react-native-permissions';


check(PERMISSIONS.ANDROID.ACTIVITY_RECOGNITION).then(result => {
    switch (result) {
        case RESULTS.UNAVAILABLE:
            console.log('This feature is not available (on this device / in this context)');
            break;
        case RESULTS.DENIED:
            console.log('The permission has not been requested / is denied');
            request(PERMISSIONS.ANDROID.ACTIVITY_RECOGNITION).then(() => {
                GoogleFit.startRecording(callback => {
                    console.log(callback);
                });
            });

    ... rest of code....

于 2021-08-25T13:51:36.913 回答