简体   繁体   中英

Android Ble Glucose - onCharacteristicChanged not called

I'm using Google Translate, so please forgive my poor English.

Currently, the blood glucose meter is linked using Android Ble. The device name is "ACCU-CHEK Guide Meter".

I found the right service and Characteristics in onServicesDiscovered.

But onCharacteristicChanged is not called, so I don't know the value measured by the glucometer.

Below is the code I've been working on. I've tested several methods, so please forgive the complexity.

I have tried change ENABLE_NOTIFICATION_VALUE <-> ENABLE_INDICATION_VALUE.

Please let me know if I missed anything or made a mistake. Thank you.

@Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            mGatt = gatt;
            List<BluetoothGattService> services = mGatt.getServices();
            for (BluetoothGattService service : gatt.getServices()) {
                for (BluetoothGattCharacteristic characteristic2 : service.getCharacteristics()) {
                    if (CBUUID.fromString(characteristic2.getUuid().toString()).uuidString().equals("2A52")
                            || new CBUUID(characteristic2.getUuid().toString()).toString().equals("Record Access Control Point")) {
                        characteristic2.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
                        byte[] requestValue = new byte[]{0x01, 0x01};
                        boolean setValueBoolean = characteristic2.setValue(requestValue);
                        boolean writeCharacteristicBoolean = gatt.writeCharacteristic(characteristic2);
                        break;
                    }
                }
            }
        }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic2, int status) {
        super.onCharacteristicWrite(gatt, characteristic2, status);

        List<BluetoothGattService> services = mGatt.getServices();
        for (BluetoothGattService service : services) {
            for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
                if (CBUUID.fromString(characteristic.getUuid().toString()).uuidString().equals("2A18")) {
                    boolean result2 = mGatt.setCharacteristicNotification(characteristic, true);

                    new Handler(Looper.getMainLooper()).postDelayed(() -> {
                        for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {                                descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                            boolean result = mGatt.writeDescriptor(descriptor);
                        }
                    }, 500);
                }
            }
        }
    }

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        super.onDescriptorWrite(gatt, descriptor, status);

        descriptor.getCharacteristic().setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        debugLog("i", currentActivityName, "onCharacteristicChanged " + Arrays.toString(characteristic.getValue()));}

I found it.

The problem was that in onCharacteristicWrite the status came out as 129 (Gatt Internal Error).

The solution is to set ENABLE_NOTIFICATION in Glucose Measurement and Glucose Measurement Context and ENABLE_INDICATION in Record Access Control Point.

Thanks to both the questioner and the answerer of the question below.

Reference: BleGattCharacteristicException: GATT exception status 129 when trying to Indicate->write->notify to BLE glucometer

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