始终检查权限 returns "denied"。反应本机

Checking for permissions always returns "denied". React Native

在物理设备上调试 React Native 应用程序的过程中 (Android),当我检查位置权限时,它总是被阻止,即使我在设置中授予了权限。我必须指出,我之前无法请求“请求许可”window,所以我无法以任何方式阻止它。另外,我尝试删除并让应用程序重新安装。

这是我检查位置权限的代码(我也试过其他代码)。我在那里使用 react-native-permissions 但是,如果我使用 react-native 中的 PermissionsAndroid,行为是相同的。

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

async function checkPermissions() {
    let response = await check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION); // <-- always blocked
    let isPermissionsGranted = false;

    if (response === RESULTS.GRANTED) {
        isPermissionsGranted = true;
    } else if (response === RESULTS.DENIED) {
        response = request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION, {
            title: "FreeLine requires permission",
            message: "FreeLine needs access to your location so you can see your position",
            buttonPositive: "Ok",
            buttonNegative: "Don't show my position",
        });

        if (response === RESULTS.GRANTED) {
            isPermissionsGranted = true;
        } else if (response === RESULTS.DENIED) {
            await openSettings();
        }
    }

    return isPermissionsGranted;
}

我还没有找到任何可以解释这一点的信息。我想可能是调试的时候请求不到权限吧

最终,我找到了问题的根源。最有趣的是它与我的代码没有任何关系。问题是由清单引起的,更准确地说是由我包含的规则引起的。

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-feature android:name="android.permission.BLUETOOTH" android:required="true"/>
<!-- Only foreground ble access -->
<uses-feature android:name="android.permission.ACCESS_FINE_LOCATION" android:required="true"/>

从上面的代码片段可以看出,我使用了use-feature。文档是这样说的:

Google Play uses the elements declared in your app manifest to filter your app from devices that do not meet its hardware and software feature requirements.

除了上面的规则,还要加上uses-permission,像这样:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />