简体   繁体   English

Android 权限请求总是被拒绝

[英]Android permission request always denied

I'm building an Android application using Java in Android Studio that requires Bluetooth permissions to operate.我正在使用 Android Studio 中的 Java 构建一个 Android 应用程序,该应用程序需要蓝牙权限才能运行。
My issue is, every time I ask for the needed permissions, I get my request automatically denied.我的问题是,每次我请求所需的权限时,都会自动拒绝我的请求。 Moreover, no permission dialog shows up after making the request.此外,发出请求后不会出现权限对话框。

The permissions I'm requesting are BLUETOOTH_ADVERTISE , BLUETOOTH_CONNECT , BLUETOOTH .我请求的权限是BLUETOOTH_ADVERTISEBLUETOOTH_CONNECTBLUETOOTH Here's the manifest file:这是清单文件:

<?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>

Here is the build.gradle file.这是build.gradle文件。 I don't know whether it is important, but I include it just to be sure.我不知道它是否重要,但我将其包括在内只是为了确定。

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

And here is the code that checks and asks for permission:这是检查并请求许可的代码:

/*
    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;
}

This is the request permission launcher that is supposed to ask for permission:这是应该请求许可的请求许可启动器:

@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();
        }
    });

I've been trying to find a solution for days, but without any success.几天来我一直试图找到解决方案,但没有任何成功。 I've read about instant apps not having access to determinate permissions, but I don't think that's my case: I'm not building an instant app.我读过关于即时应用程序无法访问确定权限的信息,但我认为这不是我的情况:我不是在构建即时应用程序。
Could this issue be related to my Android device and API versions?此问题是否与我的 Android 设备和 API 版本有关? My device is a Samsung Galaxy A5 2017 running Android version 8.0.0 (Oreo).我的设备是运行 Android 版本 8.0.0 (Oreo) 的三星 Galaxy A5 2017。
Thanks for your time.谢谢你的时间。

The issue was a stupid error of mine.这个问题是我的一个愚蠢的错误。 I was using permissions not available on my testing Android device.我在测试 Android 设备上使用了不可用的权限。
I solved this by using BLUETOOTH_ADMIN permission instead of BLUETOOTH_ADVERTISE and BLUETOOTH_CONNECT .我通过使用BLUETOOTH_ADMIN权限而不是BLUETOOTH_ADVERTISEBLUETOOTH_CONNECT解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM