Skip to content

Commit

Permalink
Added a constructor so you could create a platform specific device. A…
Browse files Browse the repository at this point in the history
…dded isnofiying property (#149)
  • Loading branch information
Reedyuk authored Nov 26, 2024
1 parent 34dee01 commit aae937d
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 31 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.0.0
version=2.0.1
group=dev.bluefalcon
libraryName=blue-falcon
kotlinx_coroutines_version=1.8.1
Expand Down
22 changes: 11 additions & 11 deletions library/src/androidMain/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ actual class BlueFalcon actual constructor(

actual fun connect(bluetoothPeripheral: BluetoothPeripheral, autoConnect: Boolean) {
log?.info("connect")
bluetoothPeripheral.bluetoothDevice.connectGatt(
bluetoothPeripheral.device.connectGatt(
context,
autoConnect,
mGattClientCallback,
Expand All @@ -44,7 +44,7 @@ actual class BlueFalcon actual constructor(

actual fun disconnect(bluetoothPeripheral: BluetoothPeripheral) {
log?.info("disconnect")
val gatt = mGattClientCallback.gattForDevice(bluetoothPeripheral.bluetoothDevice)
val gatt = mGattClientCallback.gattForDevice(bluetoothPeripheral.device)
gatt?.apply {
disconnect()
close()
Expand Down Expand Up @@ -84,15 +84,15 @@ actual class BlueFalcon actual constructor(
serviceUUIDs: List<String>
) {
// cant individually get services.
mGattClientCallback.gattForDevice(bluetoothPeripheral.bluetoothDevice)?.discoverServices()
mGattClientCallback.gattForDevice(bluetoothPeripheral.device)?.discoverServices()
}
actual fun discoverCharacteristics(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothService: BluetoothService,
characteristicUUIDs: List<String>
) {
if (!bluetoothPeripheral.services.containsKey(bluetoothService.uuid)) {
mGattClientCallback.gattForDevice(bluetoothPeripheral.bluetoothDevice)?.discoverServices()
mGattClientCallback.gattForDevice(bluetoothPeripheral.device)?.discoverServices()
}
// no need to do anything here for android.
}
Expand Down Expand Up @@ -120,7 +120,7 @@ actual class BlueFalcon actual constructor(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic
) {
mGattClientCallback.gattForDevice(bluetoothPeripheral.bluetoothDevice)?.let { gatt ->
mGattClientCallback.gattForDevice(bluetoothPeripheral.device)?.let { gatt ->
fetchCharacteristic(bluetoothCharacteristic, gatt)
.forEach { gatt.readCharacteristic(it.characteristic) }
}
Expand All @@ -133,7 +133,7 @@ actual class BlueFalcon actual constructor(
descriptorValue: ByteArray
) {
log?.info("setCharacteristicNotification ${bluetoothCharacteristic.uuid} enable $enable")
mGattClientCallback.gattForDevice(bluetoothPeripheral.bluetoothDevice)?.let { gatt ->
mGattClientCallback.gattForDevice(bluetoothPeripheral.device)?.let { gatt ->
fetchCharacteristic(bluetoothCharacteristic, gatt).forEach {
log?.info("setCharacteristicNotification ${it.uuid} enable $enable ${gatt}")
gatt.setCharacteristicNotification(it.characteristic, enable)
Expand All @@ -157,7 +157,7 @@ actual class BlueFalcon actual constructor(
value: ByteArray
) {
log?.info("writeDescriptor ${bluetoothCharacteristicDescriptor.uuid} value $value")
mGattClientCallback.gattForDevice(bluetoothPeripheral.bluetoothDevice)?.let { gatt ->
mGattClientCallback.gattForDevice(bluetoothPeripheral.device)?.let { gatt ->
bluetoothCharacteristicDescriptor.value = value
gatt.writeDescriptor(bluetoothCharacteristicDescriptor)
}
Expand Down Expand Up @@ -264,14 +264,14 @@ actual class BlueFalcon actual constructor(
writeType: Int?
) {
log?.info("Writing value {length = ${value.size}, bytes = 0x${value.toHexString()}} with response $writeType")
mGattClientCallback.gattForDevice(bluetoothPeripheral.bluetoothDevice)?.let { gatt ->
mGattClientCallback.gattForDevice(bluetoothPeripheral.device)?.let { gatt ->
fetchCharacteristic(bluetoothCharacteristic, gatt)
.forEach {
writeType?.let { writeType ->
it.characteristic.writeType = writeType
}
it.characteristic.setValue(value)
val resp = gatt.writeCharacteristic(it.characteristic)
gatt.writeCharacteristic(it.characteristic)
}
}
}
Expand All @@ -281,13 +281,13 @@ actual class BlueFalcon actual constructor(
bluetoothCharacteristic: BluetoothCharacteristic,
bluetoothCharacteristicDescriptor: BluetoothCharacteristicDescriptor
) {
mGattClientCallback.gattForDevice(bluetoothPeripheral.bluetoothDevice)
mGattClientCallback.gattForDevice(bluetoothPeripheral.device)
?.readDescriptor(bluetoothCharacteristicDescriptor)
log?.debug("readDescriptor -> ${bluetoothCharacteristicDescriptor.uuid}")
}

actual fun changeMTU(bluetoothPeripheral: BluetoothPeripheral, mtuSize: Int) {
mGattClientCallback.gattForDevice(bluetoothPeripheral.bluetoothDevice)?.requestMtu(mtuSize)
mGattClientCallback.gattForDevice(bluetoothPeripheral.device)?.requestMtu(mtuSize)
}

inner class BluetoothScanCallBack : ScanCallback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ actual class BluetoothCharacteristic(val characteristic: BluetoothGattCharacteri
actual val uuid: String
get() = characteristic.uuid.toString().uppercase()


actual val isNotifying: Boolean
get() = (characteristic.properties and BluetoothGattCharacteristic.PROPERTY_NOTIFY) == BluetoothGattCharacteristic.PROPERTY_NOTIFY
}

actual typealias BluetoothCharacteristicDescriptor = BluetoothGattDescriptor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.bluefalcon

actual typealias NativeBluetoothDevice = android.bluetooth.BluetoothDevice
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import android.os.Parcel
import android.os.Parcelable
import kotlinx.coroutines.flow.MutableStateFlow

actual class BluetoothPeripheral(val bluetoothDevice: BluetoothDevice) : Parcelable {
actual class BluetoothPeripheral actual constructor(val device: NativeBluetoothDevice) : Parcelable {
actual val name: String?
get() = bluetoothDevice.name ?: bluetoothDevice.address
get() = device.name ?: device.address
actual val services: Map<String, BluetoothService>
get() = _servicesFlow.value.associateBy { it.uuid }
actual val uuid: String
get() = bluetoothDevice.address
get() = device.address

actual var rssi: Float? = null
actual var mtuSize: Int? = null
Expand All @@ -32,7 +32,7 @@ actual class BluetoothPeripheral(val bluetoothDevice: BluetoothDevice) : Parcela
}

override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeParcelable(bluetoothDevice, flags)
parcel.writeParcelable(device, flags)
parcel.writeValue(rssi)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ actual class BluetoothCharacteristic(val characteristic: CBCharacteristic) {
internal actual val _descriptorsFlow = MutableStateFlow<List<BluetoothCharacteristicDescriptor>>(emptyList())
actual val uuid: String
get() = characteristic.UUID.UUIDString.uppercase()

actual val isNotifying: Boolean
get() = characteristic.isNotifying
}

actual typealias BluetoothCharacteristicDescriptor = CBDescriptor
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import platform.CoreBluetooth.CBPeripheral
import platform.CoreBluetooth.CBService

actual class BluetoothPeripheral(val bluetoothDevice: CBPeripheral, val rssiValue: Float?) {
actual constructor(device: NativeBluetoothDevice): this(device, null)

actual val name: String? = bluetoothDevice.name
actual var rssi: Float? = rssiValue
actual var mtuSize: Int? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ expect class BluetoothCharacteristic {
val uuid: String
val value: ByteArray?
val descriptors: List<BluetoothCharacteristicDescriptor>
val isNotifying: Boolean
internal val _descriptorsFlow: MutableStateFlow<List<BluetoothCharacteristicDescriptor>>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dev.bluefalcon

import kotlinx.coroutines.flow.MutableStateFlow

expect class BluetoothPeripheral {
expect class BluetoothPeripheral(device: NativeBluetoothDevice) {
val name: String?
val uuid: String
var rssi: Float?
Expand All @@ -12,3 +12,5 @@ expect class BluetoothPeripheral {

val characteristics: Map<String, BluetoothCharacteristic>
}

expect class NativeBluetoothDevice
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ actual class BluetoothCharacteristic(val characteristic: BluetoothRemoteGATTChar
internal actual val _descriptorsFlow = MutableStateFlow<List<BluetoothCharacteristicDescriptor>>(emptyList())
actual val uuid: String
get() = characteristic.uuid.uppercase()
actual val isNotifying: Boolean
get() = false
}

actual class BluetoothCharacteristicDescriptor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.bluefalcon

import dev.bluefalcon.external.BluetoothDevice

actual typealias NativeBluetoothDevice = BluetoothDevice
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package dev.bluefalcon

import dev.bluefalcon.external.BluetoothDevice
import kotlinx.coroutines.flow.MutableStateFlow

actual class BluetoothPeripheral(val device: BluetoothDevice) {
actual class BluetoothPeripheral actual constructor(val device: NativeBluetoothDevice) {
actual val name: String?
get() = device.name
actual val uuid: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.w3c.dom.events.EventTarget
import kotlin.js.Promise

//https://developer.mozilla.org/en-US/docs/Web/API/BluetoothDevice
abstract external class BluetoothDevice : EventTarget {
external class BluetoothDevice : EventTarget {
val id: String
val name: String?
val gatt: BluetoothRemoteGATTServer?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.bluefalcon

actual typealias NativeBluetoothDevice = platform.CoreBluetooth.CBPeripheral
14 changes: 7 additions & 7 deletions library/src/rpiMain/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ actual class BlueFalcon actual constructor(
private val bluetoothManager = BluetoothCentralManager(bluetoothManagerCallback)

actual fun connect(bluetoothPeripheral: BluetoothPeripheral, autoConnect: Boolean) {
bluetoothManager.connectPeripheral(bluetoothPeripheral.bluetoothDevice, bluetoothPeripheralCallback)
bluetoothManager.connectPeripheral(bluetoothPeripheral.device, bluetoothPeripheralCallback)
}

actual fun disconnect(bluetoothPeripheral: BluetoothPeripheral) {
bluetoothManager.cancelConnection(bluetoothPeripheral.bluetoothDevice)
bluetoothManager.cancelConnection(bluetoothPeripheral.device)
}

@Throws(
Expand Down Expand Up @@ -117,15 +117,15 @@ actual class BlueFalcon actual constructor(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic
) {
bluetoothPeripheral.bluetoothDevice.readCharacteristic(bluetoothCharacteristic.characteristic)
bluetoothPeripheral.device.readCharacteristic(bluetoothCharacteristic.characteristic)
}

actual fun notifyCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
notify: Boolean
) {
bluetoothPeripheral.bluetoothDevice.setNotify(bluetoothCharacteristic.characteristic, notify)
bluetoothPeripheral.device.setNotify(bluetoothCharacteristic.characteristic, notify)
}

actual fun indicateCharacteristic(
Expand All @@ -150,7 +150,7 @@ actual class BlueFalcon actual constructor(
value: String,
writeType: Int?
) {
bluetoothPeripheral.bluetoothDevice.writeCharacteristic(bluetoothCharacteristic.characteristic, value.toByteArray(), writeType.writeType)
bluetoothPeripheral.device.writeCharacteristic(bluetoothCharacteristic.characteristic, value.toByteArray(), writeType.writeType)
}

actual fun writeCharacteristic(
Expand All @@ -159,7 +159,7 @@ actual class BlueFalcon actual constructor(
value: ByteArray,
writeType: Int?
) {
bluetoothPeripheral.bluetoothDevice.writeCharacteristic(bluetoothCharacteristic.characteristic, value, writeType.writeType)
bluetoothPeripheral.device.writeCharacteristic(bluetoothCharacteristic.characteristic, value, writeType.writeType)
}

actual fun writeCharacteristicWithoutEncoding(
Expand All @@ -168,7 +168,7 @@ actual class BlueFalcon actual constructor(
value: ByteArray,
writeType: Int?
){
bluetoothPeripheral.bluetoothDevice.writeCharacteristic(bluetoothCharacteristic.characteristic, value, writeType.writeType)
bluetoothPeripheral.device.writeCharacteristic(bluetoothCharacteristic.characteristic, value, writeType.writeType)
}

private val Int?.writeType: BluetoothGattCharacteristic.WriteType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ actual class BluetoothCharacteristic(val characteristic: BluetoothGattCharacteri
internal actual val _descriptorsFlow = MutableStateFlow<List<BluetoothCharacteristicDescriptor>>(emptyList())
actual val uuid: String
get() = characteristic.uuid.toString().uppercase()
actual val isNotifying: Boolean
get() = characteristic.supportsNotifying()
}

actual typealias BluetoothCharacteristicDescriptor = BluetoothGattDescriptor
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package dev.bluefalcon
import com.welie.blessed.BluetoothPeripheral
import kotlinx.coroutines.flow.MutableStateFlow

actual class BluetoothPeripheral(val bluetoothDevice: BluetoothPeripheral) {
actual class BluetoothPeripheral actual constructor(val device: NativeBluetoothDevice) {
actual val name: String?
get() = bluetoothDevice.name
get() = device.name
actual val uuid: String
get() = bluetoothDevice.address
get() = device.address

actual var rssi: Float? = null
actual var mtuSize: Int? = null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.bluefalcon

import com.welie.blessed.BluetoothPeripheral

actual typealias NativeBluetoothDevice = BluetoothPeripheral

0 comments on commit aae937d

Please sign in to comment.