Last active
October 20, 2021 10:29
-
-
Save tkubasik-luna/d8ebc4594ffcbb72aaa8d29c428bea42 to your computer and use it in GitHub Desktop.
gatt callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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