androidbluetooth-lowenergybluetooth-gattandroid-ble

BLE's - onCharacteristicRead not invoked on calling readCharacteristic


I'm trying to read a value from a custom characteristic in a custom service on a BLE device. I successfully call the readCharcteristic method as it returns true. However, the onCharacteristicRead callback function is never invoked. I am unable to determine why this is so.

My read function is as follows (there is no pending or ongoing read or write request when this method if called) -

fun readOneSpecificCharacteristic() {

    if (lpnInfoChar?.isReadable() == true) {
    
        Log.i("READ", "READABLE")
    
        if (btGatt?.readCharacteristic(lpnInfoChar) == true) {
            Log.i("READ", "INIT SUCCESS") //<- my code reaches here
        } else {
            Log.i("READ", "INIT FAIL")
        }
    
    } else {
        Log.i("READ", "NOT READABLE")
    }
}

My Callback (which is never invoked) -

private val gattCallback = object: BluetoothGattCallback() {

    override fun onCharacteristicRead(
        gatt: BluetoothGatt,
        characteristic: BluetoothGattCharacteristic,
        value: ByteArray,
        status: Int
    ) {
    
        Log.i("Callback", "onCharacteristicRead")
    
        with(characteristic) {
            when (status) {
                BluetoothGatt.GATT_SUCCESS -> {
                    Log.i("BluetoothGattCallback", "Read characteristic $uuid:\n${value.toHexString()}")
                }
                BluetoothGatt.GATT_READ_NOT_PERMITTED -> {
                    Log.e("BluetoothGattCallback", "Read not permitted for $uuid!")
                }
                else -> {
                    Log.e("BluetoothGattCallback", "Characteristic read failed for $uuid, error: $status")
                }
            }
        }
    }
}

The other callbacks work as expected - I am able to write, read rssi value, discover service etc.

Additionally, I know that the BLE device is working fine since BLE scanning apps form the PlayStore are able to read the value from it.

Where gattCallback is registered -

override fun onScanResult(callbackType: Int, result: ScanResult) {

            if (isScanning) {
                stopScan()
            }

            with(result.device) {
                Log.i("ScanCallback", "Found BLE device! Name: ${name ?: "Unnamed"}, address: $address")
                setGeneralStatusText("BLE Device Found - ${name ?: "Unnamed"}")

                Log.i("ScanResultAdapter", "Connecting to $address")
                setGeneralStatusText("Connecting to ${name ?: "Unnamed"} - $address")
                connectGatt(baseContext, true, gattCallback, BluetoothDevice.TRANSPORT_LE)
            }
        }

Solution

  • Turns out the deprecated method needed to be called. More info here -

    New `onCharacteristicRead` method not working