-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Bluetooth.isSupported()
function
- Loading branch information
Showing
8 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.juul.kable.bluetooth | ||
|
||
import android.content.pm.PackageManager.FEATURE_BLUETOOTH_LE | ||
import com.juul.kable.applicationContext | ||
import com.juul.kable.getBluetoothAdapterOrNull | ||
|
||
internal actual suspend fun isSupported(): Boolean = | ||
applicationContext.packageManager.hasSystemFeature(FEATURE_BLUETOOTH_LE) && | ||
getBluetoothAdapterOrNull() != null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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 permissions dialog. | ||
// https://chrismaddern.com/determine-whether-bluetooth-is-enabled-on-ios-passively/ | ||
private val options = mapOf(CBCentralManagerOptionShowPowerAlertKey to false) as Map<Any?, *>? | ||
|
||
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 (until no longer needed). | ||
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. | ||
} | ||
} | ||
}.also { delegateRef = it } | ||
CBCentralManager(delegate, null, options).also { managerRef = it } | ||
awaitClose { | ||
managerRef = null | ||
delegateRef = null | ||
} | ||
}.first { it.isDetermined } | ||
|
||
private val CBManagerState.isDetermined: Boolean | ||
get() = this != CBManagerStateUnknown && this != CBManagerStateResetting |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.juul.kable.bluetooth | ||
|
||
internal expect suspend fun isSupported(): Boolean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.juul.kable.bluetooth | ||
|
||
import com.juul.kable.InternalException | ||
import com.juul.kable.bluetoothOrNull | ||
import js.errors.JsError | ||
import js.errors.TypeError | ||
import kotlinx.coroutines.await | ||
|
||
internal actual suspend fun isSupported(): Boolean { | ||
val bluetooth = bluetoothOrNull() ?: return false | ||
val promise = try { | ||
bluetooth.getAvailability() | ||
} catch (e: TypeError) { | ||
// > TypeError: navigator.bluetooth.getAvailability is not a function | ||
return false | ||
} | ||
return try { | ||
promise.await() | ||
} catch (e: JsError) { | ||
throw InternalException("Failed to get bluetooth availability", e) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
kable-core/src/jvmMain/kotlin/com/juul/kable/bluetooth/IsSupported.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.juul.kable.bluetooth | ||
|
||
import com.juul.kable.jvmNotImplementedException | ||
|
||
internal actual suspend fun isSupported(): Boolean = jvmNotImplementedException() |