diff --git a/library/gradle.properties b/library/gradle.properties index 3d0a4d8..bd11033 100644 --- a/library/gradle.properties +++ b/library/gradle.properties @@ -10,7 +10,7 @@ org.gradle.configureondemand = false android.useAndroidX=true android.enableJetifier=true -version=2.2.0 +version=2.2.1 group=dev.bluefalcon libraryName=blue-falcon kotlinx_coroutines_version=1.9.0 diff --git a/library/src/nativeMain/kotlin/dev/bluefalcon/BlueFalcon.kt b/library/src/nativeMain/kotlin/dev/bluefalcon/BlueFalcon.kt index f83c780..32c2b83 100644 --- a/library/src/nativeMain/kotlin/dev/bluefalcon/BlueFalcon.kt +++ b/library/src/nativeMain/kotlin/dev/bluefalcon/BlueFalcon.kt @@ -39,6 +39,7 @@ actual class BlueFalcon actual constructor( } actual fun disconnect(bluetoothPeripheral: BluetoothPeripheral) { + log?.info("disconnect ${bluetoothPeripheral.uuid}") centralManager.cancelPeripheralConnection(bluetoothPeripheral.bluetoothDevice) } @@ -107,6 +108,9 @@ actual class BlueFalcon actual constructor( bluetoothCharacteristic: BluetoothCharacteristic, notify: Boolean ) { + if (bluetoothPeripheral.bluetoothDevice.delegate == null) { + bluetoothPeripheralManager.setPeripheralDelegate(bluetoothPeripheral) + } log?.info("notifyCharacteristic setNotify for ${bluetoothCharacteristic.uuid} notify: $notify") bluetoothPeripheral.bluetoothDevice.setNotifyValue(notify, bluetoothCharacteristic.characteristic) } diff --git a/library/src/nativeMain/kotlin/dev/bluefalcon/BluetoothPeripheralManager.kt b/library/src/nativeMain/kotlin/dev/bluefalcon/BluetoothPeripheralManager.kt index a46c562..0088c3a 100644 --- a/library/src/nativeMain/kotlin/dev/bluefalcon/BluetoothPeripheralManager.kt +++ b/library/src/nativeMain/kotlin/dev/bluefalcon/BluetoothPeripheralManager.kt @@ -18,6 +18,11 @@ class BluetoothPeripheralManager constructor( internal val delegate = PeripheralDelegate(log, blueFalcon) + fun setPeripheralDelegate(peripheral: BluetoothPeripheral, unset: Boolean = false) { + log?.debug("setPeripheralDelegate ${peripheral.uuid}") + peripheral.bluetoothDevice.delegate = delegate.takeUnless { unset } + } + override fun centralManagerDidUpdateState(central: CBCentralManager) { _managerState.tryEmit(central.state) when (central.state) { @@ -73,6 +78,7 @@ class BluetoothPeripheralManager constructor( blueFalcon.delegates.forEach { it.didDisconnect(device) } + didDisconnectPeripheral.delegate = null } //Helper diff --git a/library/src/nativeMain/kotlin/dev/bluefalcon/PeripheralDelegate.kt b/library/src/nativeMain/kotlin/dev/bluefalcon/PeripheralDelegate.kt index df1117a..31098e9 100644 --- a/library/src/nativeMain/kotlin/dev/bluefalcon/PeripheralDelegate.kt +++ b/library/src/nativeMain/kotlin/dev/bluefalcon/PeripheralDelegate.kt @@ -2,7 +2,6 @@ package dev.bluefalcon import platform.CoreBluetooth.* import platform.Foundation.NSError -import platform.Foundation.NSNumber import platform.darwin.NSObject class PeripheralDelegate constructor( @@ -55,7 +54,7 @@ class PeripheralDelegate constructor( if (error != null) { log?.error("Error with characteristic update ${error}") } - log?.info("didUpdateValueForCharacteristic") + log?.info("handleCharacteristicValueChange ${didUpdateValueForCharacteristic.UUID}") val device = BluetoothPeripheral(peripheral, rssiValue = null) val characteristic = BluetoothCharacteristic(didUpdateValueForCharacteristic) blueFalcon.delegates.forEach {