[SOLVED] onNotify ble android never called what I’m doing wrong?


This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

Issue

I want to read records data from glucometer. That characteristic is not able to read or write, only set notifications. Iuse writeDescriptor and it’s return true but there is no any onCharacteristicChanged callbacks

override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                Log.w("BLE", "onServicesDiscovered ")
                val service = gatt.getService(GLUCOSE_SERVICE)
                val characteristic = service.getCharacteristic(RECORDS_CHARACTERISTIC)
                gatt!!.setCharacteristicNotification(characteristic, true)
                val descriptor = characteristic!!.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG)
                descriptor.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
                gatt!!.writeDescriptor(descriptor)
            }
        }

Solution

So, with Michael Kotzjan’s help I found out an answer on my quiestion. First of all I’ve checked which glucose characteristic must turn on indications/notifications in nRF Connect app and after that just setNotification&writeDescriptor to them one by one in onDescriptorWrite method. That’s sample of code, you can upgrade it because I’m sure that it can be much better

override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                val charGM =
                    gatt.getService(GlucometerUuids.GLUCOSE_SERVICE)
                        .getCharacteristic(GlucometerUuids.GLUCOSE_CHARACTERISTIC)
                gatt.setCharacteristicNotification(charGM, true)

                val descGM =
                    charGM.getDescriptor(GlucometerUuids.CLIENT_CHARACTERISTIC_CONFIG)
                descGM.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
                gatt.writeDescriptor(descGM)
            }
        }

override fun onDescriptorWrite(
            gatt: BluetoothGatt?,
            descriptor: BluetoothGattDescriptor?,
            status: Int
        ) {
            val parentCharacteristic = descriptor?.characteristic
            if(status!= BluetoothGatt.GATT_SUCCESS) {
            }
            else {
                if (parentCharacteristic?.uuid == GlucometerUuids.GLUCOSE_CHARACTERISTIC) {
                    val charContextGM =
                        gatt?.getService(GlucometerUuids.GLUCOSE_SERVICE)
                            ?.getCharacteristic(GlucometerUuids.CONTEXT_CHARACTERISTIC)
                    gatt?.setCharacteristicNotification(charContextGM, true)

                    val descContextGM =
                        charContextGM?.getDescriptor(GlucometerUuids.CLIENT_CHARACTERISTIC_CONFIG)
                    descContextGM?.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
                    gatt?.writeDescriptor(descContextGM)
                } else if (parentCharacteristic?.uuid == GlucometerUuids.CONTEXT_CHARACTERISTIC) {
                    val charRACP =
                        gatt?.getService(GlucometerUuids.GLUCOSE_SERVICE)
                            ?.getCharacteristic(GlucometerUuids.RECORDS_CHARACTERISTIC)
                    gatt?.setCharacteristicNotification(charRACP, true)
                    val descRACP =
                        charRACP?.getDescriptor(GlucometerUuids.CLIENT_CHARACTERISTIC_CONFIG)
                    descRACP?.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
                    gatt?.writeDescriptor(descRACP)
                } else if (parentCharacteristic?.uuid == GlucometerUuids.RECORDS_CHARACTERISTIC) {
                    val writeRACPchar =
                        gatt!!.getService(GlucometerUuids.GLUCOSE_SERVICE)
                            .getCharacteristic(GlucometerUuids.RECORDS_CHARACTERISTIC)
                    val data = ByteArray(2)
                    data[0] = 0x01 // Report Stored records

                    data[1] = 0x01 // 0x01 - all records, 0x06 - last record, 0x05 first record 

                    writeRACPchar.value = data
                    gatt.writeCharacteristic(writeRACPchar)
                }
            }
        }

Answered By – a2smoda2i

people found this article helpful. What about you?