繁体   English   中英

Android Ble Glucose - onCharacteristicChanged 未调用

[英]Android Ble Glucose - onCharacteristicChanged not called

我正在使用谷歌翻译,所以请原谅我糟糕的英语。

目前,血糖仪采用Android Ble联动。 设备名称为“ACCU-CHEK Guide Meter”。

我在 onServicesDiscovered 中找到了正确的服务和特征。

但是onCharacteristicChanged没有被调用,所以我不知道血糖仪测量的值。

下面是我一直在处理的代码。 我已经测试了几种方法,所以请原谅复杂性。

我尝试更改 ENABLE_NOTIFICATION_VALUE <-> ENABLE_INDICATION_VALUE。

如果我遗漏了什么或犯了错误,请告诉我。 谢谢你。

@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()));}

我找到了。

问题在于 onCharacteristicWrite 中的状态为 129(Gatt 内部错误)。

解决方案是在 Glucose Measurement 和 Glucose Measurement Context 中设置 ENABLE_NOTIFICATION,在 Record Access Control Point 中设置 ENABLE_INDICATION。

感谢以下问题的提问者和回答者。

参考: BleGattCharacteristicException:尝试指示->写入->通知 BLE 血糖仪时 GATT 异常状态 129

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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