Android BLE GATT Operations — Complete Guide
In this tutorial, you'll learn about Android BLE GATT Operations. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Your BLE read returns GATT_INVALID_ATTRIBUTE_LENGTH, or you never receive notifications after enabling them.
Wrong Approach ❌
// Reading characteristic without checking properties
gatt.readCharacteristic(characteristic) // May not be readable!
// Enabling notifications without descriptor
gatt.setCharacteristicNotification(characteristic, true)
// Forgot to write CCCD descriptor!
Output: Read fails with GATT_READ_NOT_PERMITTED. Notifications never arrive.
Right Approach ✅
class GattOperationManager(private val gatt: BluetoothGatt) {
// Safe characteristic read
fun readCharacteristic(char: BluetoothGattCharacteristic) {
if (!containsProperty(char, BluetoothGattCharacteristic.PROPERTY_READ)) {
Log.e("GattOp", "Characteristic not readable")
return
}
gatt.readCharacteristic(char)
}
// Write with response
fun writeCharacteristic(char: BluetoothGattCharacteristic, data: ByteArray) {
if (!containsProperty(char, BluetoothGattCharacteristic.PROPERTY_WRITE)) {
Log.e("GattOp", "Characteristic not writable")
return
}
char.value = data
char.writeType = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
gatt.writeCharacteristic(char)
}
// Write without response (faster, no ACK)
fun writeCharacteristicWithoutResponse(char: BluetoothGattCharacteristic, data: ByteArray) {
if (!containsProperty(char, BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) {
Log.e("GattOp", "Characteristic not writable without response")
return
}
char.value = data
char.writeType = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
gatt.writeCharacteristic(char)
}
// Enable notifications
fun enableNotifications(char: BluetoothGattCharacteristic) {
if (!containsProperty(char, BluetoothGattCharacteristic.PROPERTY_NOTIFY)) {
Log.e("GattOp", "Characteristic does not support notifications")
return
}
gatt.setCharacteristicNotification(char, true)
// Write CCCD (Client Characteristic Configuration Descriptor)
val cccd = char.getDescriptor(
UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")
) ?: run {
Log.e("GattOp", "CCCD not found")
return
}
cccd.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
gatt.writeDescriptor(cccd)
}
// Enable indications
fun enableIndications(char: BluetoothGattCharacteristic) {
if (!containsProperty(char, BluetoothGattCharacteristic.PROPERTY_INDICATE)) {
Log.e("GattOp", "Characteristic does not support indications")
return
}
gatt.setCharacteristicNotification(char, true)
val cccd = char.getDescriptor(CCCD_UUID)
cccd?.let {
it.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
gatt.writeDescriptor(it)
}
}
// Read descriptor
fun readDescriptor(descriptor: BluetoothGattDescriptor) {
gatt.readDescriptor(descriptor)
}
// Handle response in BluetoothGattCallback:
// onCharacteristicRead, onCharacteristicWrite, onDescriptorWrite
// onCharacteristicChanged (notification/indication)
private fun containsProperty(char: BluetoothGattCharacteristic, property: Int): Boolean {
return char.properties and property != 0
}
companion object {
val CCCD_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")
}
}
// Callback handlers
override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
val data = characteristic.value
// Handle notification data
}
Output: Correct GATT operations with property checking.
Prevention
- Always check characteristic properties before read/write.
- Always write CCCD descriptor after
setCharacteristicNotification. - Use
WRITE_TYPE_NO_RESPONSEfor high-throughput, lossy-tolerant data. - Queue operations — BLE is single-threaded per GATT connection.
Common Mistakes with bluetooth gatt
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro