Device scan: Fix permission check on Android 11 and below (#3659)

Reviewed-on: https://codeberg.org/Freeyourgadget/Gadgetbridge/pulls/3659
Co-authored-by: Daniel Dakhno <dakhnod@gmail.com>
Co-committed-by: Daniel Dakhno <dakhnod@gmail.com>
This commit is contained in:
Daniel Dakhno 2024-04-06 10:46:55 +00:00 committed by José Rebelo
parent a37f0c89bb
commit 9502a0dcfb
1 changed files with 11 additions and 1 deletions

View File

@ -326,10 +326,20 @@ public class BLEScanService extends Service {
unregisterReceiver(bluetoothStateChangedReceiver);
}
private boolean hasBluetoothPermission(){
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.R){
// workaround. Cannot give bluetooth permission on Android O
LOG.warn("Running on android 11, skipping bluetooth permission check");
return ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
return ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED;
}
private void restartScan(boolean applyFilters) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
if (!hasBluetoothPermission()) {
// this should never happen
LOG.error("No BLUETOOTH_SCAN permission");
updateNotification("Missing Bluetooth scan permissions");
return;
}