Implement hasPermissions API (#201)

* Implement hasPermissions api

* Bump minimum MacOS version to 10.15

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Rohit Sangwan
2025-12-13 02:40:21 +05:30
committed by GitHub
parent 89abb3bf03
commit 97936046e0
23 changed files with 175 additions and 36 deletions
@@ -24,6 +24,18 @@ class PermissionHandler(
) {
private var permissionRequestCallback: ((Result<Unit>) -> Unit)? = null
/**
* Check if we have required permissions
*/
fun hasPermissions(withFineLocation: Boolean): Boolean {
val validationError = validateRequiredPermissions(withFineLocation)
if (validationError != null) {
throw validationError
}
val permissionsToRequest = getRequiredPermissions(withFineLocation)
return permissionsToRequest.isEmpty()
}
/**
* Requests the required Bluetooth permissions based on the manifest and Android version.
*
@@ -510,6 +510,7 @@ private open class UniversalBlePigeonCodec : StandardMessageCodec() {
*/
interface UniversalBlePlatformChannel {
fun getBluetoothAvailabilityState(callback: (Result<Long>) -> Unit)
fun hasPermissions(withAndroidFineLocation: Boolean): Boolean
fun requestPermissions(withAndroidFineLocation: Boolean, callback: (Result<Unit>) -> Unit)
fun enableBluetooth(callback: (Result<Boolean>) -> Unit)
fun disableBluetooth(callback: (Result<Boolean>) -> Unit)
@@ -557,6 +558,23 @@ interface UniversalBlePlatformChannel {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.hasPermissions$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val withAndroidFineLocationArg = args[0] as Boolean
val wrapped: List<Any?> = try {
listOf(api.hasPermissions(withAndroidFineLocationArg))
} catch (exception: Throwable) {
UniversalBlePigeonUtils.wrapError(exception)
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.requestPermissions$separatedMessageChannelSuffix", codec)
if (api != null) {
@@ -95,6 +95,10 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
)
}
override fun hasPermissions(withAndroidFineLocation: Boolean): Boolean {
return permissionHandler?.hasPermissions(withAndroidFineLocation) ?: false
}
override fun requestPermissions(
withAndroidFineLocation: Boolean,
callback: (Result<Unit>) -> Unit,