Because I am not yet well-versed in Kotlin (and I expect you to be able to easily convert the Java codes into Kotlin), I'll just show you how to request the dangerous Manifest.permission.BLUETOOTH_CONNECT permission every time we want to access the list of paired Bluetooth devices:
public static final int PERMISSIONS_REQUEST_BLUETOOTH_CONNECT = 101; //the int value must be unique
public boolean checkBluetoothConnectPermission(Activity activity) {
boolean permitted = false;
Boolean bluetoothAlreadyGranted = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ?
ActivityCompat.checkSelfPermission(activity,
Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED: true;
Log.i(IMapView.LOGTAG,
String.format("PERMISSION: checkBluetoothPermission(%s) bluetoothAlreadyGranted=%s",
activity.getClass().getSimpleName(),
bluetoothAlreadyGranted));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Check for permission
if (!bluetoothAlreadyGranted.booleanValue()) {
bluetoothAlreadyGranted = ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.BLUETOOTH_CONNECT);
// Check for permission
if (bluetoothAlreadyGranted.booleanValue()) {
new AlertDialog.Builder(MainApp.getCurrentActivity(), R.style.AppCompatAlertDialogStyle)
.setTitle(activity.getString(R.string.bluetooth_connect_rational_title,
activity.getString(R.string.app_name_flavor)))
.setMessage(activity.getString(R.string.bluetooth_connect_rational_message,
activity.getString(R.string.app_name_flavor)))
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.BLUETOOTH_CONNECT},
PERMISSIONS_REQUEST_BLUETOOTH_CONNECT);
}
})
.create()
.show();
} else {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.BLUETOOTH_CONNECT},
PERMISSIONS_REQUEST_BLUETOOTH_CONNECT);
}
} else {
permitted = true;
}
} else {
permitted = true;
}
return permitted;
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
// Check if permission was granted
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//boolean granted = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED; //@TODO you need to check which permission from the array to get the granted status for the matched request code...
switch (requestCode) {
case PERMISSIONS_REQUEST_BLUETOOTH_CONNECT: {
//do whatever you want here
}
break;
}
}
In the Android manifest, the following entry shall be provided:
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
The same shall apply for the dangerous Manifest.permission.BLUETOOTH_SCAN permission with a different intent request code.