Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finding characteristic by UUID + required property #128

Closed
wants to merge 9 commits into from
2 changes: 1 addition & 1 deletion core/src/androidMain/kotlin/BluetoothDevice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ private val Phy.intValue: Int
}

private val BluetoothDevice.threadName: String
get() = "Gatt@$this"
get() = "Gatt@$this"
17 changes: 9 additions & 8 deletions core/src/androidMain/kotlin/Peripheral.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import android.bluetooth.BluetoothAdapter.STATE_TURNING_OFF
import android.bluetooth.BluetoothAdapter.STATE_TURNING_ON
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCharacteristic.PROPERTY_INDICATE
import android.bluetooth.BluetoothGattCharacteristic.PROPERTY_NOTIFY
import android.bluetooth.BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
import android.bluetooth.BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
import android.bluetooth.BluetoothGattCharacteristic.*
import android.bluetooth.BluetoothGattDescriptor
import android.bluetooth.BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE
import android.bluetooth.BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
Expand Down Expand Up @@ -264,7 +261,9 @@ public class AndroidPeripheral internal constructor(
data: ByteArray,
writeType: WriteType,
) {
val bluetoothGattCharacteristic = bluetoothGattCharacteristicFrom(characteristic)
val bluetoothGattCharacteristic = bluetoothGattCharacteristicFrom(
characteristic,
PROPERTY_WRITE or PROPERTY_WRITE_NO_RESPONSE)
connection.execute<OnCharacteristicWrite> {
bluetoothGattCharacteristic.value = data
bluetoothGattCharacteristic.writeType = writeType.intValue
Expand Down Expand Up @@ -313,7 +312,9 @@ public class AndroidPeripheral internal constructor(
): Flow<ByteArray> = observers.acquire(characteristic, onSubscription)

internal suspend fun startObservation(characteristic: Characteristic) {
val platformCharacteristic = platformServices.findCharacteristic(characteristic)
val platformCharacteristic = platformServices.findCharacteristic(
characteristic,
PROPERTY_NOTIFY or PROPERTY_INDICATE)
connection
.bluetoothGatt
.setCharacteristicNotification(platformCharacteristic, true)
Expand Down Expand Up @@ -356,8 +357,8 @@ public class AndroidPeripheral internal constructor(
}

private fun bluetoothGattCharacteristicFrom(
characteristic: Characteristic
) = platformServices.findCharacteristic(characteristic).bluetoothGattCharacteristic
characteristic: Characteristic, propertyMask: Int = -1
) = platformServices.findCharacteristic(characteristic, propertyMask).bluetoothGattCharacteristic

private fun bluetoothGattDescriptorFrom(
descriptor: Descriptor
Expand Down
16 changes: 10 additions & 6 deletions core/src/androidMain/kotlin/PlatformService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,25 @@ internal fun BluetoothGattService.toPlatformService(): PlatformService {

/** @throws NoSuchElementException if service or characteristic is not found. */
internal fun List<PlatformService>.findCharacteristic(
characteristic: Characteristic
characteristic: Characteristic, propertyMask: Int = -1
): PlatformCharacteristic =
findCharacteristic(
serviceUuid = characteristic.serviceUuid,
characteristicUuid = characteristic.characteristicUuid
characteristicUuid = characteristic.characteristicUuid,
propertyMask = propertyMask
)

/** @throws NoSuchElementException if service or characteristic is not found. */
private fun List<PlatformService>.findCharacteristic(
serviceUuid: Uuid,
characteristicUuid: Uuid
characteristicUuid: Uuid,
propertyMask: Int = -1
): PlatformCharacteristic =
first(serviceUuid)
.characteristics
.first(characteristicUuid)
.first { platformCharacteristic ->
platformCharacteristic.characteristicUuid == characteristicUuid
&& platformCharacteristic.bluetoothGattCharacteristic.properties and propertyMask != 0
}

/** @throws NoSuchElementException if service, characteristic or descriptor is not found. */
internal fun List<PlatformService>.findDescriptor(
Expand All @@ -63,5 +67,5 @@ private fun List<PlatformService>.findDescriptor(
): PlatformDescriptor =
findCharacteristic(
serviceUuid = serviceUuid,
characteristicUuid = characteristicUuid
characteristicUuid = characteristicUuid,
).descriptors.first(descriptorUuid)
17 changes: 7 additions & 10 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ POM_NAME=Kable
POM_DESCRIPTION=Kotlin Asynchronous Bluetooth Low Energy
POM_INCEPTION_YEAR=2020

POM_URL=https://github.com/JuulLabs/kable
POM_SCM_URL=https://github.com/JuulLabs/kable
POM_SCM_CONNECTION=scm:git:git://github.com/JuulLabs/kable.git
POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com/JuulLabs/kable.git
POM_URL=https://github.com/solvek/kable
POM_SCM_URL=https://github.com/solvek/kable
POM_SCM_CONNECTION=scm:git:git://github.com/solvek/kable.git
POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com/solvek/kable.git

POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=https://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=repo

POM_DEVELOPER_ID=twyatt
POM_DEVELOPER_NAME=Travis Wyatt
POM_DEVELOPER_URL=https://github.com/twyatt

kotlin.js.webpack.major.version=5
kotlin.js.compiler=both
POM_DEVELOPER_ID=solvek
POM_DEVELOPER_NAME=Sergi Adamchuk
POM_DEVELOPER_URL=https://github.com/solvek