BleGattCallback

class BleGattCallback(connectionStateMachine: ConnectionStateMachine, bondManager: BondManager? = null) : BluetoothGattCallback

Custom implementation of BluetoothGattCallback that handles all GATT client events.

This callback acts as a bridge between the Android Bluetooth stack and the ConnectionStateMachine, translating low-level GATT events into state machine transitions and operations.

Handled GATT Events

  • Connection State Changes: Connect/disconnect events

  • Service Discovery: When remote services are discovered

  • MTU Changes: When MTU negotiation completes

  • PHY Updates: When physical layer parameters change

  • Characteristic Operations: Read, write, and notification events

  • Descriptor Operations: Descriptor write completions

Logging

All callbacks are logged using Constants.TAG for debugging purposes. Log messages include the event type, relevant UUIDs, and status codes.

API Compatibility

This class handles both legacy (pre-API 33) and modern (API 33+) callback methods for characteristic change notifications. The appropriate method is called by the system based on the Android version.

Usage Example

val stateMachine = ConnectionStateMachine(scope, ...)
val callback = BleGattCallback(stateMachine, bondManager)

// Use the callback when connecting
device.connectGatt(context, false, callback)

Parameters

connectionStateMachine

The ConnectionStateMachine that processes GATT events and manages the connection lifecycle.

bondManager

Optional BondManager for handling device bonding. Currently reserved for future use in automatic bonding scenarios.

See also

Constructors

Link copied to clipboard
constructor(connectionStateMachine: ConnectionStateMachine, bondManager: BondManager? = null)

Functions

Link copied to clipboard
open override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic)

Called when a characteristic value has changed due to a notification or indication (API < 33).

open override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, value: ByteArray)

Called when a characteristic value has changed due to a notification or indication (API 33+).

Link copied to clipboard

open override fun onCharacteristicRead(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int)

Called when a characteristic read operation completes.

Link copied to clipboard
open override fun onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int)

Called when a characteristic write operation completes.

Link copied to clipboard
open override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int)

Called when the connection state changes (connected or disconnected).

Link copied to clipboard
open override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int)

Called when a descriptor write operation completes.

Link copied to clipboard
open override fun onMtuChanged(gatt: BluetoothGatt, mtu: Int, status: Int)

Called when the MTU (Maximum Transmission Unit) for a connection has changed.

Link copied to clipboard
open fun onPhyRead(p0: BluetoothGatt, p1: Int, p2: Int, p3: Int)
Link copied to clipboard
open override fun onPhyUpdate(gatt: BluetoothGatt, txPhy: Int, rxPhy: Int, status: Int)

Called when the PHY (Physical Layer) for a connection has been updated.

Link copied to clipboard
open fun onReadRemoteRssi(p0: BluetoothGatt, p1: Int, p2: Int)
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int)

Called when remote device services have been discovered.