Reputation: 7161
I want to checkSelfPermission
in multiple places in my same file. I tried to make a function and check the condition, but it still complains the error.
fun checkPermissionTwo(): Boolean {
return permissionsCheck().all { permission ->
ActivityCompat.checkSelfPermission(
application.applicationContext, permission
) == PackageManager.PERMISSION_GRANTED
}
}
internal fun permissionsCheck(): List<String> {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
listOf(
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
)
} else {
listOf(
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
)
}
}
I am trying to check checkPermissionTwo()
in where errors
fun stopScan(adapter: BluetoothAdapter) {
if (isBluetoothStateOn(adapter), checkPermissionTwo()) {
bluetoothLeScanner?.stopScan(leScanCallback)
}
}
Image
Error
Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException
Upvotes: 0
Views: 611
Reputation: 1037
This seems to be a limitation on Android Studio, you need to directly wrap your function call with the corresponding permission.
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_ADMIN) == PackageManager.PERMISSION_GRANTED) {
bluetoothLeScanner?.stopScan(leScanCallback)
}
For details you may refer to this previous post.
Upvotes: 2