Skip to content

Commit

Permalink
Added uuid instead of strings (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reedyuk authored Nov 28, 2024
1 parent aae937d commit 31674fb
Show file tree
Hide file tree
Showing 27 changed files with 87 additions and 52 deletions.
6 changes: 5 additions & 1 deletion library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import java.util.*
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework

plugins {
kotlin("multiplatform") version "2.0.0"
kotlin("multiplatform") version "2.0.20"
id("com.android.library")
id("maven-publish")
id("signing")
Expand Down Expand Up @@ -56,6 +56,10 @@ android {

val frameworkName = "BlueFalcon"

kotlin.sourceSets.all {
languageSettings.optIn("kotlin.uuid.ExperimentalUuidApi")
}

kotlin {
jvmToolchain(17)
androidTarget {
Expand Down
4 changes: 2 additions & 2 deletions library/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ org.gradle.configureondemand = false
android.useAndroidX=true
android.enableJetifier=true

version=2.0.1
version=2.1.0
group=dev.bluefalcon
libraryName=blue-falcon
kotlinx_coroutines_version=1.8.1
kotlinx_coroutines_version=1.9.0

projectGithubUrl=https://github.com/Reedyuk/blue-falcon
projectGithubSCM=scm:git:git://[email protected]:reedyuk/blue-falcon.git
Expand Down
19 changes: 11 additions & 8 deletions library/src/androidMain/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ actual class BlueFalcon actual constructor(

actual fun discoverServices(
bluetoothPeripheral: BluetoothPeripheral,
serviceUUIDs: List<String>
serviceUUIDs: List<Uuid>
) {
// cant individually get services.
mGattClientCallback.gattForDevice(bluetoothPeripheral.device)?.discoverServices()
}
actual fun discoverCharacteristics(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothService: BluetoothService,
characteristicUUIDs: List<String>
characteristicUUIDs: List<Uuid>
) {
if (!bluetoothPeripheral.services.containsKey(bluetoothService.uuid)) {
mGattClientCallback.gattForDevice(bluetoothPeripheral.device)?.discoverServices()
Expand Down Expand Up @@ -130,16 +130,19 @@ actual class BlueFalcon actual constructor(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
enable: Boolean,
descriptorValue: ByteArray
descriptorValue: ByteArray?
) {
log?.info("setCharacteristicNotification ${bluetoothCharacteristic.uuid} enable $enable")
log?.info("notifyCharacteristic setNotify for ${bluetoothCharacteristic.uuid} notify: $enable $bluetoothCharacteristic")
mGattClientCallback.gattForDevice(bluetoothPeripheral.device)?.let { gatt ->
fetchCharacteristic(bluetoothCharacteristic, gatt).forEach {
log?.info("setCharacteristicNotification ${it.uuid} enable $enable ${gatt}")
log?.info("setCharacteristicNotification ${it.uuid} enable $enable ${gatt} ${it}")
gatt.setCharacteristicNotification(it.characteristic, enable)
it.characteristic.descriptors.forEach { descriptor ->
descriptor.value = descriptorValue
gatt.writeDescriptor(descriptor)
descriptorValue?.let { descriptorValue ->
it.characteristic.descriptors.forEach { descriptor ->
writeDescriptor(
bluetoothPeripheral, descriptor, descriptorValue
)
}
}
val read = (bluetoothCharacteristic.characteristic.properties and BluetoothGattCharacteristic.PROPERTY_READ) == BluetoothGattCharacteristic.PROPERTY_READ
val notify = (bluetoothCharacteristic.characteristic.properties and BluetoothGattCharacteristic.PROPERTY_NOTIFY) == BluetoothGattCharacteristic.PROPERTY_NOTIFY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.bluetooth.BluetoothGattDescriptor
import android.os.Parcel
import android.os.Parcelable
import kotlinx.coroutines.flow.MutableStateFlow
import kotlin.uuid.toKotlinUuid

actual class BluetoothCharacteristic(val characteristic: BluetoothGattCharacteristic) : Parcelable {
actual val name: String?
Expand Down Expand Up @@ -39,8 +40,8 @@ actual class BluetoothCharacteristic(val characteristic: BluetoothGattCharacteri
}
}

actual val uuid: String
get() = characteristic.uuid.toString().uppercase()
actual val uuid: Uuid
get() = characteristic.uuid.toKotlinUuid()

actual val isNotifying: Boolean
get() = (characteristic.properties and BluetoothGattCharacteristic.PROPERTY_NOTIFY) == BluetoothGattCharacteristic.PROPERTY_NOTIFY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
actual class BluetoothPeripheral actual constructor(val device: NativeBluetoothDevice) : Parcelable {
actual val name: String?
get() = device.name ?: device.address
actual val services: Map<String, BluetoothService>
actual val services: Map<Uuid, BluetoothService>
get() = _servicesFlow.value.associateBy { it.uuid }
actual val uuid: String
get() = device.address
Expand Down Expand Up @@ -50,7 +50,7 @@ actual class BluetoothPeripheral actual constructor(val device: NativeBluetoothD
}
}

actual val characteristics: Map<String, BluetoothCharacteristic>
actual val characteristics: Map<Uuid, BluetoothCharacteristic>
get() = services.values
.flatMap { it.characteristics }
.associateBy { it.uuid }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.bluefalcon

import android.bluetooth.BluetoothGattService
import kotlinx.coroutines.flow.MutableStateFlow
import kotlin.uuid.toKotlinUuid

actual class BluetoothService(val service: BluetoothGattService) {
actual val name: String?
Expand All @@ -11,6 +12,6 @@ actual class BluetoothService(val service: BluetoothGattService) {
BluetoothCharacteristic(it)
}
internal actual val _characteristicsFlow = MutableStateFlow<List<BluetoothCharacteristic>>(emptyList())
actual val uuid: String
get() = service.uuid.toString().uppercase()
actual val uuid: Uuid
get() = service.uuid.toKotlinUuid()
}
5 changes: 5 additions & 0 deletions library/src/androidMain/kotlin/dev/bluefalcon/Uuid.android.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.bluefalcon

import kotlin.uuid.Uuid

actual typealias Uuid = Uuid
9 changes: 5 additions & 4 deletions library/src/appleMain/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,21 @@ actual class BlueFalcon actual constructor(

actual fun discoverServices(
bluetoothPeripheral: BluetoothPeripheral,
serviceUUIDs: List<String>
serviceUUIDs: List<Uuid>
) {
log?.info("discoverServices ${bluetoothPeripheral.uuid} services: $serviceUUIDs")
bluetoothPeripheral.bluetoothDevice.discoverServices(
serviceUUIDs.map { CBUUID.UUIDWithString(it) }
serviceUUIDs.map { CBUUID.UUIDWithString(it.toString()) }
)
}
actual fun discoverCharacteristics(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothService: BluetoothService,
characteristicUUIDs: List<String>
characteristicUUIDs: List<Uuid>
) {
log?.info("discoverCharacteristics ${bluetoothPeripheral.uuid} services: ${bluetoothService.uuid} chars: $characteristicUUIDs ${bluetoothPeripheral.bluetoothDevice.delegate}")
bluetoothPeripheral.bluetoothDevice.discoverCharacteristics(
characteristicUUIDs.map { CBUUID.UUIDWithString(it) }, bluetoothService.service
characteristicUUIDs.map { CBUUID.UUIDWithString(it.toString()) }, bluetoothService.service
)
}

Expand All @@ -107,6 +107,7 @@ actual class BlueFalcon actual constructor(
bluetoothCharacteristic: BluetoothCharacteristic,
notify: Boolean
) {
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 @@ -24,8 +24,9 @@ actual class BluetoothCharacteristic(val characteristic: CBCharacteristic) {
get() = characteristic.descriptors as List<BluetoothCharacteristicDescriptor>

internal actual val _descriptorsFlow = MutableStateFlow<List<BluetoothCharacteristicDescriptor>>(emptyList())
actual val uuid: String
get() = characteristic.UUID.UUIDString.uppercase()

actual val uuid: Uuid
get() = characteristic.UUID

actual val isNotifying: Boolean
get() = characteristic.isNotifying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ actual class BluetoothPeripheral(val bluetoothDevice: CBPeripheral, val rssiValu
return other.uuid == uuid
}

actual val services: Map<String, BluetoothService>
actual val services: Map<Uuid, BluetoothService>
get() = bluetoothDevice.services
?.filterIsInstance<CBService>()
?.map { service -> BluetoothService(service) }
?.associateBy { it.uuid }
?: emptyMap()

actual val characteristics: Map<String, BluetoothCharacteristic>
actual val characteristics: Map<Uuid, BluetoothCharacteristic>
get() = services.values
.flatMap { it.characteristics }
.associateBy { it.uuid }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ actual class BluetoothService(val service: CBService) {
} ?: emptyList()

internal actual val _characteristicsFlow = MutableStateFlow<List<BluetoothCharacteristic>>(emptyList())
actual val uuid: String
get() = service.UUID.UUIDString.uppercase()
actual val uuid: Uuid
get() = service.UUID
}
4 changes: 2 additions & 2 deletions library/src/commonMain/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ expect class BlueFalcon(

fun stopScanning()

fun discoverServices(bluetoothPeripheral: BluetoothPeripheral, serviceUUIDs: List<String> = emptyList())
fun discoverServices(bluetoothPeripheral: BluetoothPeripheral, serviceUUIDs: List<Uuid> = emptyList())

fun discoverCharacteristics(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothService: BluetoothService,
characteristicUUIDs: List<String> = emptyList()
characteristicUUIDs: List<Uuid> = emptyList()
)

fun readCharacteristic(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlinx.coroutines.flow.MutableStateFlow

expect class BluetoothCharacteristic {
val name: String?
val uuid: String
val uuid: Uuid
val value: ByteArray?
val descriptors: List<BluetoothCharacteristicDescriptor>
val isNotifying: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ expect class BluetoothPeripheral(device: NativeBluetoothDevice) {
val uuid: String
var rssi: Float?
var mtuSize: Int?
val services: Map<String, BluetoothService>
val services: Map<Uuid, BluetoothService>
internal val _servicesFlow: MutableStateFlow<List<BluetoothService>>

val characteristics: Map<String, BluetoothCharacteristic>
val characteristics: Map<Uuid, BluetoothCharacteristic>
}

expect class NativeBluetoothDevice
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.bluefalcon
import kotlinx.coroutines.flow.MutableStateFlow

expect class BluetoothService {
val uuid: String
val uuid: Uuid
val name: String?
val characteristics: List<BluetoothCharacteristic>
internal val _characteristicsFlow: MutableStateFlow<List<BluetoothCharacteristic>>
Expand Down
3 changes: 3 additions & 0 deletions library/src/commonMain/kotlin/dev/bluefalcon/Uuid.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.bluefalcon

expect class Uuid
6 changes: 3 additions & 3 deletions library/src/jsMain/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ actual class BlueFalcon actual constructor(

actual fun discoverServices(
bluetoothPeripheral: BluetoothPeripheral,
serviceUUIDs: List<String>
serviceUUIDs: List<Uuid>
) {
readService(
bluetoothPeripheral,
Expand All @@ -97,12 +97,12 @@ actual class BlueFalcon actual constructor(
actual fun discoverCharacteristics(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothService: BluetoothService,
characteristicUUIDs: List<String>
characteristicUUIDs: List<Uuid>
) {
if (!bluetoothPeripheral.services.containsKey(bluetoothService.uuid)) {
readService(
bluetoothPeripheral,
bluetoothService.uuid
bluetoothService.uuid.toString()
)
}
// no need to do anything.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ actual class BluetoothCharacteristic(val characteristic: BluetoothRemoteGATTChar

val stringValue get() = value?.decodeToString()
internal actual val _descriptorsFlow = MutableStateFlow<List<BluetoothCharacteristicDescriptor>>(emptyList())
actual val uuid: String
get() = characteristic.uuid.uppercase()

actual val uuid: Uuid
get() = Uuid.parse(characteristic.uuid)
actual val isNotifying: Boolean
get() = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ actual class BluetoothPeripheral actual constructor(val device: NativeBluetoothD

internal actual val _servicesFlow = MutableStateFlow<List<BluetoothService>>(emptyList())
val serviceArray: Array<BluetoothService> get() = services.values.toTypedArray()
actual val services: Map<String, BluetoothService>
actual val services: Map<Uuid, BluetoothService>
get() = _servicesFlow.value.associateBy { it.uuid }

actual val characteristics: Map<String, BluetoothCharacteristic>
actual val characteristics: Map<Uuid, BluetoothCharacteristic>
get() = services.values
.flatMap { it.characteristics }
.associateBy { it.uuid }
Expand Down
4 changes: 2 additions & 2 deletions library/src/jsMain/kotlin/dev/bluefalcon/BluetoothService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ actual class BluetoothService(val service: BluetoothRemoteGATTService) {
get() = _characteristicsFlow.value
val characteristicArray: Array<BluetoothCharacteristic> get() = characteristics.toTypedArray()
internal actual val _characteristicsFlow = MutableStateFlow<List<BluetoothCharacteristic>>(emptyList())
actual val uuid: String
get() = service.uuid.uppercase()
actual val uuid: Uuid
get() = Uuid.parse(service.uuid)
}
5 changes: 5 additions & 0 deletions library/src/jsMain/kotlin/dev/bluefalcon/Uuid.js.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.bluefalcon

import kotlin.uuid.Uuid

actual typealias Uuid = Uuid
5 changes: 5 additions & 0 deletions library/src/nativeMain/kotlin/dev/bluefalcon/Uuid.native.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.bluefalcon

import platform.CoreBluetooth.CBUUID

actual typealias Uuid = CBUUID
5 changes: 2 additions & 3 deletions library/src/rpiMain/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import java.util.*

actual class BlueFalcon actual constructor(
log: Logger?,
Expand Down Expand Up @@ -102,13 +101,13 @@ actual class BlueFalcon actual constructor(

actual fun discoverServices(
bluetoothPeripheral: BluetoothPeripheral,
serviceUUIDs: List<String>
serviceUUIDs: List<Uuid>
) {
}
actual fun discoverCharacteristics(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothService: BluetoothService,
characteristicUUIDs: List<String>
characteristicUUIDs: List<Uuid>
) {
// no need to do anything.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ actual class BluetoothCharacteristic(val characteristic: BluetoothGattCharacteri

internal var mutableVal: ByteArray? = null
internal actual val _descriptorsFlow = MutableStateFlow<List<BluetoothCharacteristicDescriptor>>(emptyList())
actual val uuid: String
get() = characteristic.uuid.toString().uppercase()

actual val uuid: Uuid
get() = Uuid.parse(characteristic.uuid.toString())
actual val isNotifying: Boolean
get() = characteristic.supportsNotifying()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.bluefalcon

import com.welie.blessed.BluetoothPeripheral
import kotlinx.coroutines.flow.MutableStateFlow

actual class BluetoothPeripheral actual constructor(val device: NativeBluetoothDevice) {
Expand All @@ -13,10 +12,10 @@ actual class BluetoothPeripheral actual constructor(val device: NativeBluetoothD
actual var mtuSize: Int? = null

internal actual val _servicesFlow = MutableStateFlow<List<BluetoothService>>(emptyList())
actual val services: Map<String, BluetoothService>
actual val services: Map<Uuid, BluetoothService>
get() = _servicesFlow.value.associateBy { it.uuid }

actual val characteristics: Map<String, BluetoothCharacteristic>
actual val characteristics: Map<Uuid, BluetoothCharacteristic>
get() = services.values
.flatMap { it.characteristics }
.associateBy { it.uuid }
Expand Down
Loading

0 comments on commit 31674fb

Please sign in to comment.