Skip to content

Commit

Permalink
Fix android bluetooth permission request above api version 30
Browse files Browse the repository at this point in the history
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 Baseflow#884
  • Loading branch information
stephane-arista committed Feb 10, 2023
1 parent 2e47f05 commit 168dabd
Showing 1 changed file with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,36 @@ private int checkNotificationPermissionStatus(Context context) {
}

private int checkBluetoothPermissionStatus(Context context) {
List<String> 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<String> names = PermissionUtils.getManifestNames(context, permission);
return names != null || !names.isEmpty();
}
}
}

0 comments on commit 168dabd

Please sign in to comment.