Android: Couldn't ask permissions

Viewed 63

I learned how to code in Android long time ago and haven't touched it in a while. Now I wanna control a motor with a Bluetooth module (HC-05) and was relearning Android with Android Studio. Buuuut....

Apparently they changed how to get permissions and for whatever reason its super complicated now. I did some research but I still can't get my phone to bring me a pop up for "This app is asking these permissions". Can you help me out with that? Here is my most basic code to request permissions:

" Android Studio Bumblebee | 2021.1.1 Patch 2 Build #AI-211.7628.21.2111.8193401, built on February 17, 2022 Runtime version: 11.0.11+9-b60-7590822 amd64 VM: OpenJDK 64-Bit Server VM by Oracle Corporation Windows 10 10.0 GC: G1 Young Generation, G1 Old Generation Memory: 1280M Cores: 12 Registry: external.system.auto.import.disabled=true "

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bluetoothandpermissions">
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"/>
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
    <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.BluetoothAndPermissions">
        <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>

</manifest>

MainActivity.java

package com.example.bluetoothandpermissions;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {
    
    ActivityResultLauncher<String[]> nPermissionResultLauncher;
    
    private boolean isBluetoothPermissionGranted = false;
    private boolean isBluetoothAdminPermissionGranted = false;
    private boolean isBluetoothConnectPermissionGranted = false;
    private boolean isBluetoothScanPermissionGranted = false;
    private boolean isBluetoothAdvertisePermissionGranted = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nPermissionResultLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
            @Override
            public void onActivityResult(Map<String, Boolean> result) {
                if(result.get(Manifest.permission.BLUETOOTH) != null){
                    isBluetoothPermissionGranted = result.get(Manifest.permission.BLUETOOTH);
                }
                if(result.get(Manifest.permission.BLUETOOTH_ADMIN) != null){
                    isBluetoothAdminPermissionGranted = result.get(Manifest.permission.BLUETOOTH_ADMIN);
                }
                if(result.get(Manifest.permission.BLUETOOTH_CONNECT) != null){
                    isBluetoothConnectPermissionGranted = result.get(Manifest.permission.BLUETOOTH_CONNECT);
                }
                if(result.get(Manifest.permission.BLUETOOTH_SCAN) != null){
                    isBluetoothScanPermissionGranted = result.get(Manifest.permission.BLUETOOTH_SCAN);
                }
                if(result.get(Manifest.permission.BLUETOOTH_ADVERTISE) != null){
                    isBluetoothAdvertisePermissionGranted = result.get(Manifest.permission.BLUETOOTH_ADVERTISE);
                }

            }
        });
        
        requestPermission();
        
    }

    private void requestPermission(){

        isBluetoothPermissionGranted = ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.BLUETOOTH
        ) == PackageManager.PERMISSION_GRANTED;

        isBluetoothAdminPermissionGranted = ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.BLUETOOTH_ADMIN
        ) == PackageManager.PERMISSION_GRANTED;

        isBluetoothConnectPermissionGranted = ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.BLUETOOTH_CONNECT
        ) == PackageManager.PERMISSION_GRANTED;

        isBluetoothScanPermissionGranted = ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.BLUETOOTH_SCAN
        ) == PackageManager.PERMISSION_GRANTED;

        isBluetoothAdvertisePermissionGranted = ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.BLUETOOTH_ADVERTISE
        ) == PackageManager.PERMISSION_GRANTED;

        List<String> permissionRequest = new ArrayList<String>();

        if (!isBluetoothPermissionGranted){
            permissionRequest.add(Manifest.permission.BLUETOOTH);
        }
        if (!isBluetoothAdminPermissionGranted){
            permissionRequest.add(Manifest.permission.BLUETOOTH_ADMIN);
        }
        if (!isBluetoothConnectPermissionGranted){
            permissionRequest.add(Manifest.permission.BLUETOOTH_CONNECT);
        }
        if (!isBluetoothScanPermissionGranted){
            permissionRequest.add(Manifest.permission.BLUETOOTH_SCAN);
        }
        if (!isBluetoothAdvertisePermissionGranted){
            permissionRequest.add(Manifest.permission.BLUETOOTH_ADVERTISE);
        }

        if (!permissionRequest.isEmpty()){
            nPermissionResultLauncher.launch(permissionRequest.toArray(new String[0]));
        }
    }
}

I tried what I saw on youtube videos https://www.youtube.com/watch?v=nkayHRT8D_w

Seen this but it wasn't really an example code so didn't help me much[I am dumb and lazy so I need examples]: https://developer.android.com/training/permissions/requesting

This website https://www.geeksforgeeks.org/android-how-to-request-permissions-in-android-application/

Checked other people here having similar problems Why does my Android app not request permissions on install?

I wanna get something like this: enter image description hereBut I always have 0 permissions on the settings: enter image description here

0 Answers
Related