Android 权限请求总是被拒绝

Android permission request always denied

我正在 Android Studio 中使用 Java 构建一个 Android 应用程序,需要蓝牙权限才能运行。
我的问题是,每次我请求所需的权限时,我的请求都会被自动拒绝。此外,发出请求后没有出现权限对话框。

我请求的权限是 BLUETOOTH_ADVERTISEBLUETOOTH_CONNECTBLUETOOTH。 这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">

<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH" />

<uses-feature
    android:name="android.hardware.bluetooth_le"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.MyApp">
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这是 build.gradle 文件。我不知道它是否重要,但我包含它只是为了确定。

plugins {
    id 'com.android.application'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.myapp"
        minSdk 23
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 
            'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    implementation "androidx.fragment:fragment:1.4.0"
    implementation "androidx.activity:activity:1.4.0"
    testImplementation 'junit:junit:'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

这里是检查和请求许可的代码:

/*
    Returns true if all the permissions are already granted.
    Returns false if there are missing permissions.
 */
@RequiresApi(api = Build.VERSION_CODES.S)
private boolean checkPermissions() {
    System.out.println("Checking permissions");

    ArrayList<String> permissions = new ArrayList<>();

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADVERTISE)
        == PackageManager.PERMISSION_DENIED) {
        permissions.add(Manifest.permission.BLUETOOTH_ADVERTISE);
    }
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH)
        == PackageManager.PERMISSION_DENIED) {
        permissions.add(Manifest.permission.BLUETOOTH);
    }
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT)
        == PackageManager.PERMISSION_DENIED) {
        permissions.add(Manifest.permission.BLUETOOTH_CONNECT);
    }

    if (permissions.size() != 0) {
        System.out.println("Missing the following permissions: " + permissions.toString());
        requestPermissionLauncher.launch(permissions.toArray(new String[0]));
        //ActivityCompat.requestPermissions(MainActivity.this, permissions.toArray(new String[0]), 0);
        return false;
    }
    return true;
}

这是应该请求权限的请求权限启动器:

@RequiresApi(api = Build.VERSION_CODES.S)
private final ActivityResultLauncher<String[]> requestPermissionLauncher =
    registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), results -> {
        // Ensure all permissions have been granted
        boolean isGranted = true;
        for (boolean entry : results.values()) {
            if (!entry) {
                isGranted = false;
                break;
            }
        }

        if (isGranted) {
            Toast.makeText(getApplicationContext(), "Permissions Granted", Toast.LENGTH_SHORT).show();
            System.out.println("Permission granted");
        } else {
            Toast.makeText(getApplicationContext(), "Permissions Denied", Toast.LENGTH_SHORT).show();
            System.out.println("Permission denied");
            Toast.makeText(this, "You need to grant all permissions to use this application", Toast.LENGTH_LONG).show();
        }
    });

几天来我一直在努力寻找解决方案,但没有成功。我读过有关即时应用程序无法访问确定权限的信息,但我不认为这是我的情况:我不是在构建即时应用程序。
这个问题可能与我的 Android 设备和 API 版本有关吗?我的设备是 Samsung Galaxy A5 2017 运行 Android 版本 8.0.0 (Oreo)。
感谢您的宝贵时间。

这个问题是我的一个愚蠢的错误。我使用的权限在我的测试 Android 设备上不可用。
我通过使用 BLUETOOTH_ADMIN 权限而不是 BLUETOOTH_ADVERTISEBLUETOOTH_CONNECT.

解决了这个问题