I'm trying to request runtime user permission in an Android program using the ActivityResultLauncher#launch() function. It's supposed to make a permissions dialogue requesting the user to grant the requested permission. But in my program, the dialogue disappears in less than half a second. Could you please point out what I got wrong?
I requested the permission in the main activity using this code snippet:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Bluetooth scan permission denied. Requesting...");
requestPermissionLauncher.launch(Manifest.permission.BLUETOOTH_SCAN);
Log.i(TAG, "Permission requested. Awaiting user response");
return;
}
requestPermissionLauncher is defined in the activity as fallows:
private ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
Log.i(TAG, "Requested permission granted!");
scanLeDevice();
} else {
Log.e(TAG, "Requested permission denied!");
Toast.makeText(this, "Permission denied! Sorry, we can't continue.", Toast.LENGTH_LONG);
}
});
Some selected Logcat messages are as fallows:
---------------------------- PROCESS STARTED (4871) for package com.example.ble_imu_data_viewer ----------------------------
...
2023-06-11 19:57:01.623 4871-4871 important_info com.example.ble_imu_data_viewer I Bluetooth scan permission denied. Requesting...
2023-06-11 19:57:01.648 4871-4871 important_info com.example.ble_imu_data_viewer I Permission requested. Awaiting user response
2023-06-11 19:57:01.709 4871-4871 ActivityThread com.example.ble_imu_data_viewer D add activity client record, r= ActivityRecord{af555b6 token=android.os.BinderProxy@5980385 {com.example.ble_imu_data_viewer/com.example.ble_imu_data_viewer.MainActivity}} token= android.os.BinderProxy@5980385
2023-06-11 19:57:01.720 4871-4871 ZrHung.AppEyeUiProbe com.example.ble_imu_data_viewer D notify runnable to start.
2023-06-11 19:57:01.737 4871-4871 OpenGLRenderer com.example.ble_imu_data_viewer D Skia GL Pipeline
2023-06-11 19:57:01.750 4871-4871 HwAppInnerBoostImpl com.example.ble_imu_data_viewer D set config for com.example.ble_imu_data_viewer BOOST_FLAG=false REPORT_DURATION_CLICK=1000 REPORT_TIMES_CLICK=3 REPORT_DURATION_SLIDE=5000 REPORT_TIMES_SLIDE=16
2023-06-11 19:57:01.753 4871-4871 OpenGLRenderer com.example.ble_imu_data_viewer D HWUI Binary is enabled
2023-06-11 19:57:01.753 4871-4871 OpenGLRenderer com.example.ble_imu_data_viewer D disableOutlineDraw is true
2023-06-11 19:57:01.792 1263-1506 HiDATA_HiNetwork system_server E monitor handleActivityChange curPkgName is com.example.ble_imu_data_viewer curUid is 10174 lastPkgName is :com.huawei.android.launcher
2023-06-11 19:57:01.796 4871-4871 ZrHung.AppEyeUiProbe com.example.ble_imu_data_viewer D stop checker.
2023-06-11 19:57:01.802 1263-1506 HiDATA_HiNetwork system_server E onAppStart packageName is com.example.ble_imu_data_viewer
2023-06-11 19:57:01.803 1263-1506 HiDATA_HiNetwork system_server E contain db file packageName: com.example.ble_imu_data_viewer iscanacce:-1
2023-06-11 19:57:01.806 1730-10535 DollieAdapterService com.huawei.systemserver E notifyActivityState pkg:com.example.ble_imu_data_viewer/com.example.ble_imu_data_viewer.MainActivity state:2 fg:true mUid:10174
...
2023-06-11 19:57:11.906 4871-4871 important_info com.example.ble_imu_data_viewer E Requested permission denied!
2023-06-11 19:57:11.919 4871-4871 ZrHung.AppEyeUiProbe com.example.ble_imu_data_viewer D notify runnable to start.
2023-06-11 19:57:11.936 4871-4871 InputMethodManager com.example.ble_imu_data_viewer W startInputReason = 1
2023-06-11 19:57:11.937 1263-1506 HiDATA_HiNetwork system_server E monitor handleActivityChange curPkgName is com.example.ble_imu_data_viewer curUid is 10174 lastPkgName is :com.example.ble_imu_data_viewer
2023-06-11 19:57:11.938 1730-10535 DollieAdapterService com.huawei.systemserver E notifyActivityState pkg:com.example.ble_imu_data_viewer/com.example.ble_imu_data_viewer.MainActivity state:2 fg:true mUid:10174
...
I have declared the requested permission, BLUETOOTH_SCAN, in the Manifest file via
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" tools:remove="android:maxSdkVersion"/>
The entire sorce code and complete Logcat content are available below:
I was eventually able to solve this myself.
Whilst I declared the BLUETOOTH_SCAN and BLUETOOTH_CONNECT permissions in my manifest file, they are not the right permissions to ask in my context. These two permissions are only suitable for Android 12 (API level 31) or higher, but my app runs on Android 9 (API level 28). For Android versions equal to or lower than 11 (API level 30), the app needs to request the ACCESS_FINE_LOCATION (or ACCESS_COARSE_LOCATION for API level 28 or lower) permission at runtime instead.
For more detailed (and important) information, please refer to Bluetooth permissions | Android Developers