From 168dabd8c554a4bb3e69a3fbbbd1927199d7d78d Mon Sep 17 00:00:00 2001 From: Stephane Rufer Date: Fri, 10 Feb 2023 10:54:47 -0800 Subject: [PATCH] Fix android bluetooth permission request above api version 30 The BLUETOOTH permission was removed in api version 30 in favor of BLUETOOTH_SCAN, BLUETOOTH_ADVERTISE and BLUETOOTH_CONNECT. This means that manifests targetting api version above 30 will not have this permission set. For these versions the manifest should be checked for the new permissions. This change ensures that the documented behavior of the bluetooth permission always returning `true` is true. Fixes issues #884 --- .../permissionhandler/PermissionManager.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/permission_handler_android/android/src/main/java/com/baseflow/permissionhandler/PermissionManager.java b/permission_handler_android/android/src/main/java/com/baseflow/permissionhandler/PermissionManager.java index 2ab48d991..560919f0a 100644 --- a/permission_handler_android/android/src/main/java/com/baseflow/permissionhandler/PermissionManager.java +++ b/permission_handler_android/android/src/main/java/com/baseflow/permissionhandler/PermissionManager.java @@ -481,12 +481,36 @@ private int checkNotificationPermissionStatus(Context context) { } private int checkBluetoothPermissionStatus(Context context) { - List names = PermissionUtils.getManifestNames(context, PermissionConstants.PERMISSION_GROUP_BLUETOOTH); - boolean missingInManifest = names == null || names.isEmpty(); - if (missingInManifest) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + // BLUETOOTH permission was removed in Android S in favor of BLUETOOTH_SCAN, + // BLUETOOTH_ADVERTISE and BLUETOOTH_CONNECT. + // This means above version 30 we need to check if any one of those permissions + // are present instead. + boolean scanPermission = checkPermissionStatus(context, + PermissionConstants.PERMISSION_GROUP_BLUETOOTH_SCAN); + boolean advertisePermission = checkPermissionStatus(context, + PermissionConstants.PERMISSION_GROUP_BLUETOOTH_ADVERTISE); + boolean connectPermission = checkPermissionStatus(context, + PermissionConstants.PERMISSION_GROUP_BLUETOOTH_CONNECT); + + if (!scanPermission && !advertisePermission && !connectPermission) { + Log.d(PermissionConstants.LOG_TAG, "Of the bluetooth permissions (BLUETOOTH_SCAN, BLUETOOTH_ADVERTISE or BLUETOOTH_CONNECT) missing in manifest"); + return PermissionConstants.PERMISSION_STATUS_DENIED; + } + return PermissionConstants.PERMISSION_STATUS_GRANTED; + } + // legacy check for BLUETOOTH permission + boolean bluetoothPermission = checkPermissionStatus(context, PermissionConstants.PERMISSION_GROUP_BLUETOOTH); + if (!bluetoothPermission) { Log.d(PermissionConstants.LOG_TAG, "Bluetooth permission missing in manifest"); return PermissionConstants.PERMISSION_STATUS_DENIED; } return PermissionConstants.PERMISSION_STATUS_GRANTED; } + + private boolean checkPermissionStatus(Context context, @PermissionConstants.PermissionGroup int permission) { + List names = PermissionUtils.getManifestNames(context, permission); + return names != null || !names.isEmpty(); + } +} }