Skip to content

Commit

Permalink
Merge pull request #32 from virulentHive/characteristic-indication
Browse files Browse the repository at this point in the history
Added characteristic indication feature
  • Loading branch information
Reedyuk authored Apr 27, 2020
2 parents 17cfb39 + 64877ca commit 0e6932c
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 8 deletions.
65 changes: 57 additions & 8 deletions library/src/androidMain/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,75 @@ actual class BlueFalcon actual constructor(
}
}

actual fun notifyCharacteristic(
private fun setCharacteristicNotification(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
notify: Boolean
enable: Boolean,
descriptorValue: ByteArray
) {
mGattClientCallback.gattForDevice(bluetoothPeripheral.bluetoothDevice)?.let { gatt ->
fetchCharacteristic(bluetoothCharacteristic, gatt)
.forEach {
gatt.setCharacteristicNotification(it.characteristic, notify)
it.characteristic.descriptors.forEach {descriptor ->
descriptor.value = if (notify) BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE else byteArrayOf(
0x00,
0x00
)
gatt.setCharacteristicNotification(it.characteristic, enable)
it.characteristic.descriptors.forEach { descriptor ->
descriptor.value = descriptorValue
gatt.writeDescriptor(descriptor)
}
}
}
}

actual fun notifyCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
notify: Boolean
) {
setCharacteristicNotification(
bluetoothPeripheral,
bluetoothCharacteristic,
notify,
if (notify)
BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
else
BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE
)
}

actual fun indicateCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
indicate: Boolean
) {
setCharacteristicNotification(
bluetoothPeripheral,
bluetoothCharacteristic,
indicate,
if (indicate)
BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
else
BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE
)
}

actual fun notifyAndIndicateCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
enable: Boolean
) {
setCharacteristicNotification(
bluetoothPeripheral,
bluetoothCharacteristic,
enable,
if (enable)
byteArrayOf(
0x03,
0x00
)
else
BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE
)
}

actual fun writeCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
Expand Down
12 changes: 12 additions & 0 deletions library/src/commonMain/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ expect class BlueFalcon(context: ApplicationContext, serviceUUID: String?) {
notify: Boolean
)

fun indicateCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
indicate: Boolean
)

fun notifyAndIndicateCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
enable: Boolean
)

fun writeCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
Expand Down
16 changes: 16 additions & 0 deletions library/src/iosMain/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ actual class BlueFalcon actual constructor(
bluetoothPeripheral.bluetoothDevice.setNotifyValue(notify, bluetoothCharacteristic.characteristic)
}

actual fun indicateCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
indicate: Boolean
) {
notifyCharacteristic(bluetoothPeripheral, bluetoothCharacteristic, indicate)
}

actual fun notifyAndIndicateCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
enable: Boolean
) {
notifyCharacteristic(bluetoothPeripheral, bluetoothCharacteristic, enable)
}

actual fun writeCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
Expand Down
16 changes: 16 additions & 0 deletions library/src/jsMain/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ actual class BlueFalcon actual constructor(context: ApplicationContext, serviceU
TODO("not implemented")
}

actual fun indicateCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
indicate: Boolean
) {
TODO("not implemented")
}

actual fun notifyAndIndicateCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
enable: Boolean
) {
TODO("not implemented")
}

actual fun readDescriptor(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
Expand Down
16 changes: 16 additions & 0 deletions library/src/macosX64Main/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ actual class BlueFalcon actual constructor(
bluetoothPeripheral.bluetoothDevice.setNotifyValue(notify, bluetoothCharacteristic.characteristic)
}

actual fun indicateCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
indicate: Boolean
) {
notifyCharacteristic(bluetoothPeripheral, bluetoothCharacteristic, indicate)
}

actual fun notifyAndIndicateCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
enable: Boolean
) {
notifyCharacteristic(bluetoothPeripheral, bluetoothCharacteristic, enable)
}

actual fun writeCharacteristic(
bluetoothPeripheral: BluetoothPeripheral,
bluetoothCharacteristic: BluetoothCharacteristic,
Expand Down

0 comments on commit 0e6932c

Please sign in to comment.