简体   繁体   中英

Android permission request always denied

I'm building an Android application using Java in Android Studio that requires Bluetooth permissions to operate.
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 . 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. 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? My device is a Samsung Galaxy A5 2017 running Android version 8.0.0 (Oreo).
Thanks for your time.

The issue was a stupid error of mine. I was using permissions not available on my testing Android device.
I solved this by using BLUETOOTH_ADMIN permission instead of BLUETOOTH_ADVERTISE and BLUETOOTH_CONNECT .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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