Skip to content

Instantly share code, notes, and snippets.

@Arunshaik2001
Created October 27, 2022 04:20
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 Arunshaik2001/c227ac8f12c0623e080e2fe87ddbb992 to your computer and use it in GitHub Desktop.
Save Arunshaik2001/c227ac8f12c0623e080e2fe87ddbb992 to your computer and use it in GitHub Desktop.
private fun connectToChatDevice(device: BluetoothDevice) {
gattClientCallback = GattClientCallback()
gattClient = device.connectGatt(app, false, gattClientCallback)
}
private class GattClientCallback : 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) {
gatt.discoverServices()
}
}
override fun onServicesDiscovered(discoveredGatt: BluetoothGatt, status: Int) {
super.onServicesDiscovered(discoveredGatt, status)
if (status == BluetoothGatt.GATT_SUCCESS) {
gatt = discoveredGatt
val service = discoveredGatt.getService(SERVICE_UUID)
messageCharacteristic = service.getCharacteristic(MESSAGE_UUID)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment