Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make isSupported always true on Apple #752

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 1 addition & 51 deletions kable-core/src/appleMain/kotlin/bluetooth/IsSupported.kt
Original file line number Diff line number Diff line change
@@ -1,53 +1,3 @@
package com.juul.kable.bluetooth

import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.onFailure
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import platform.CoreBluetooth.CBCentralManager
import platform.CoreBluetooth.CBCentralManagerDelegateProtocol
import platform.CoreBluetooth.CBCentralManagerOptionShowPowerAlertKey
import platform.CoreBluetooth.CBManagerState
import platform.CoreBluetooth.CBManagerStateResetting
import platform.CoreBluetooth.CBManagerStateUnknown
import platform.CoreBluetooth.CBManagerStateUnsupported
import platform.darwin.NSObject

// Prevent triggering bluetooth dialog (note that permission dialog will still appear).
// https://chrismaddern.com/determine-whether-bluetooth-is-enabled-on-ios-passively/
// https://stackoverflow.com/a/58600900
private val options = mapOf<Any?, Any>(CBCentralManagerOptionShowPowerAlertKey to false)

private var cachedState: CBManagerState? = null
private val mutex = Mutex()

internal actual suspend fun isSupported() = mutex.withLock {
cachedState ?: awaitState().also { cachedState = it }
} != CBManagerStateUnsupported

// Need to hold strong-reference to CBCentralManager and its delegate while in use.
private var managerRef: NSObject? = null
private var delegateRef: NSObject? = null

private suspend fun awaitState() = callbackFlow {
val delegate = object : NSObject(), CBCentralManagerDelegateProtocol {
override fun centralManagerDidUpdateState(central: CBCentralManager) {
trySend(central.state).onFailure {
// Silently ignore.
}
}
}

delegateRef = delegate
managerRef = CBCentralManager(delegate, null, options)

awaitClose {
managerRef = null
delegateRef = null
}
}.first { it.isDetermined }

private val CBManagerState.isDetermined: Boolean
get() = this != CBManagerStateUnknown && this != CBManagerStateResetting
internal actual suspend fun isSupported() = true
8 changes: 6 additions & 2 deletions kable-core/src/commonMain/kotlin/Bluetooth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@ public object Bluetooth {
public data class Unavailable(val reason: Reason?) : Availability()
}

public val availability: Flow<Availability> = bluetoothAvailability

/**
* Checks if Bluetooth Low Energy is supported on the system. Being supported (a return of
* `true`) does not necessarily mean that bluetooth operations will work. The radio could be off
* or permissions may be denied.
*
* Due to Core Bluetooth limitations (unavoidable dialog upon checking if supported), this
* function **always** returns `true` on Apple (even if Bluetooth is not supported).
*
* This function is idempotent.
*/
@ExperimentalApi // Due to the inability to query Bluetooth support w/o showing a dialog on Apple, this function may be removed.
public suspend fun isSupported(): Boolean = isBluetoothSupported()

public val availability: Flow<Availability> = bluetoothAvailability
}

internal expect val bluetoothAvailability: Flow<Bluetooth.Availability>