I'm trying to read values from a concrete BLE characteristic in my app but at the moment I try to read values from the desired characteristic the onCharacteristicRead function seems like not being triggered.
In very first place i use onServicesDiscovered in order to get every service from the device:
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
Log.e("BluetoothLeService", "onServiceDiscovered()")
if(status == BluetoothGatt.GATT_SUCCESS){
Log.i(TAG, "**ACTION_SERVICE_DISCOVERED** $status")
var gattServices: List<BluetoothGattService> = mBluetoothGatt!!.services
Log.e("onServiceDiscovered", "Services count: ${gattServices.size}")
for(gattService in gattServices){
var serviceUUID = gattService.uuid.toString()
Log.e("OnServicesDiscovered", "Service uuid: $serviceUUID" )
readCharacteristic(gattService)
}
Log.e("OnServicesDiscovered", "---------------------------" )
broadcastUpdate( "com.np.lekotlin.ACTION_GATT_SERVICES_DISCOVERED")
} else {
//Service discovery failed so log a warning
Log.i(TAG, "onServicesDiscovered received: $status")
}
}
As you can see I use a for loop in order to achieve every service name and characteristic associated with every service with the readCharacteristic function
fun readCharacteristic(service: BluetoothGattService) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized")
return
}
var gattCharacteristic: List<BluetoothGattCharacteristic> = service.characteristics
Log.i(TAG, "**LEO LAS ${gattCharacteristic.count()} CARACTERISTICAS DE $service**")
for(characteristic in gattCharacteristic){
Log.e("OnServicesDiscovered", "Service characteristic: ${characteristic.uuid}" )
if(characteristic.uuid == characteristicRS){
mBluetoothGatt!!.setCharacteristicNotification(characteristic, true)
mBluetoothGatt!!.readCharacteristic(characteristic)
}
}
Log.e("OnServicesDiscovered", "-----------------------------" )
}
All this process finally returns me the next result
E/BluetoothLeService: onServiceDiscovered()
I/PSoCCapSenseLedService: **ACTION_SERVICE_DISCOVERED** 0
E/onServiceDiscovered: Services count: 4
E/OnServicesDiscovered: Service uuid: 00001800-0000-1000-8000-00805f9b34fb
I/PSoCCapSenseLedService: **LEO LAS 3 CARACTERISTICAS DE android.bluetooth.BluetoothGattService@1b31ffb*
E/OnServicesDiscovered: Service characteristic: 00002a00-0000-1000-8000-00805f9b34fb
E/OnServicesDiscovered: Service characteristic: 00002a01-0000-1000-8000-00805f9b34fb
E/OnServicesDiscovered: Service characteristic: 00002a04-0000-1000-8000-00805f9b34fb
E/OnServicesDiscovered: -----------------------------
E/OnServicesDiscovered: Service uuid: 00001801-0000-1000-8000-00805f9b34fb
I/PSoCCapSenseLedService: **LEO LAS 1 CARACTERISTICAS DE android.bluetooth.BluetoothGattService@4254818**
E/OnServicesDiscovered: Service characteristic: 00002a05-0000-1000-8000-00805f9b34fb
-----------------------------
E/OnServicesDiscovered: Service uuid: 00001814-0000-1000-8000-00805f9b34fb
I/PSoCCapSenseLedService: **LEO LAS 4 CARACTERISTICAS DE android.bluetooth.BluetoothGattService@a673271**
E/OnServicesDiscovered: Service characteristic: 00002a53-0000-1000-8000-00805f9b34fb
Service characteristic: 00002a54-0000-1000-8000-00805f9b34fb
E/OnServicesDiscovered: Service characteristic: 00002a5d-0000-1000-8000-00805f9b34fb
Service characteristic: 00002a55-0000-1000-8000-00805f9b34fb
-----------------------------
E/OnServicesDiscovered: Service uuid: 0000180a-0000-1000-8000-00805f9b34fb
I/PSoCCapSenseLedService: **LEO LAS 3 CARACTERISTICAS DE android.bluetooth.BluetoothGattService@3b51856**
E/OnServicesDiscovered: Service characteristic: 00002a29-0000-1000-8000-00805f9b34fb
E/OnServicesDiscovered: Service characteristic: 00002a24-0000-1000-8000-00805f9b34fb
Service characteristic: 00002a27-0000-1000-8000-00805f9b34fb
E/OnServicesDiscovered: -----------------------------
---------------------------
D/BluetoothGatt: onConnectionUpdated() - Device=00:A0:50:00:00:0F interval=36 latency=0 timeout=500 status=0
Which is completely correct (it displays me every service on the device and every characteristic inside this service), but my problem comes when I try to read values from characteristicRS which is 00002a53-0000-1000-8000-00805f9b34fb, which is correctly reported in the return terminal, but the point is (according to my knowledge) than, in the moment I call
mBluetoothGatt!!.readCharacteristic(characteristic)
The function
override fun onCharacteristicRead(
gatt: BluetoothGatt,
characteristic: BluetoothGattCharacteristic,
status: Int
) {
Log.i(TAG, "HAGO COSAS")
if (status == BluetoothGatt.GATT_SUCCESS) {
//See if the read was successful
Log.i(TAG, "**ACTION_DATA_READ** ${characteristic.uuid}")
broadcastUpdate("com.np.lekotlin.ACTION_DATA_AVAILABLE"/*, characteristic*/) //Go broadcast an intent with the characteristic data
data = characteristic.value
Log.e("onCharacteristicRead", "Datos:")
Log.e("onCharacteristicRead", "$data")
} else {
Log.i(TAG, "ACTION_DATA_READ: Error$status")
}
}
Should be triggered and returning me the values from the characteristic, but is not even entering after all. What am I forgetting?
PD: data is declared as a ByetArray