简体   繁体   中英

onNotify ble android never called what I'm doing wrong?

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)
            }
        }

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)
                }
            }
        }

that code works for receive Contour One Plus glucometer data, but for other glucometers code may be a bit different. For example in Accucheck Instant there is no Context Characteristic but an algorithm is same. Glucometers data received in onCharacteristicChanged as byte array. Some glucometers are not have glucose service (for example Onetouch Select Plus Flex has own service and characteristic and this code will not work for it, unfortunately)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM