Skip to content

Instantly share code, notes, and snippets.

@tkubasik-luna
Last active October 20, 2021 10:29
Show Gist options
  • Save tkubasik-luna/d8ebc4594ffcbb72aaa8d29c428bea42 to your computer and use it in GitHub Desktop.
Save tkubasik-luna/d8ebc4594ffcbb72aaa8d29c428bea42 to your computer and use it in GitHub Desktop.
gatt callback
val gattCallBack = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
super.onConnectionStateChange(gatt, status, newState)
val isSuccess = status == BluetoothGatt.GATT_SUCCESS
val isConnected = newState == BluetoothProfile.STATE_CONNECTED
if (isSuccess && isConnected) {
// Discover the service on the server on are connected on
gatt.discoverServices()
}
}
override fun onServicesDiscovered(discoveredGatt: BluetoothGatt, status: Int) {
super.onServicesDiscovered(discoveredGatt, status)
if (status == BluetoothGatt.GATT_SUCCESS) {
val characteristic = discoveredGatt.getService(SERVICE_UUID).getCharacteristic(MESSAGE_UUID)
// Send a message to the watch
characteristic.value = "your message".toByteArray(Charsets.UTF_8)
val result = discoveredGatt.writeCharacteristic(characteristic)
if (result) {
// Manage the result
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment