Skip to content

Instantly share code, notes, and snippets.

@tdcolvin
Last active January 5, 2024 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdcolvin/a1e789960e57f79fe7f9d3ad77da55a3 to your computer and use it in GitHub Desktop.
Save tdcolvin/a1e789960e57f79fe7f9d3ad77da55a3 to your computer and use it in GitHub Desktop.
private val callback = object: BluetoothGattCallback() {
...
override fun onCharacteristicWrite(
gatt: BluetoothGatt,
characteristic: BluetoothGattCharacteristic,
status: Int
) {
super.onCharacteristicWrite(gatt, characteristic, status)
if (characteristic.uuid == /* the one you're looking for */) {
Log.v("bluetooth", "Write status: $status")
}
}
}
@RequiresPermission(PERMISSION_BLUETOOTH_CONNECT)
fun writeCharacteristic(serviceUUID: UUID, characteristicUUID: UUID) {
val service = gatt?.getService(serviceUUID)
val characteristic = service?.getCharacteristic(characteristicUUID)
if (characteristic != null) {
// First write the new value to our local copy of the characteristic
characteristic.value = "Tom".toByteArray()
//...Then send the updated characteristic to the device
val success = gatt?.writeCharacteristic(characteristic)
Log.v("bluetooth", "Write status: $success")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment