Skip to content

Commit

Permalink
Added functionality to allow ios to set its own delegate. (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reedyuk authored Dec 11, 2024
1 parent f154a1f commit 4747cdc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion library/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions library/src/nativeMain/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ actual class BlueFalcon actual constructor(
}

actual fun disconnect(bluetoothPeripheral: BluetoothPeripheral) {
log?.info("disconnect ${bluetoothPeripheral.uuid}")
centralManager.cancelPeripheralConnection(bluetoothPeripheral.bluetoothDevice)
}

Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -73,6 +78,7 @@ class BluetoothPeripheralManager constructor(
blueFalcon.delegates.forEach {
it.didDisconnect(device)
}
didDisconnectPeripheral.delegate = null
}

//Helper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4747cdc

Please sign in to comment.