2

我正在开发一个应用程序来从启用了葡萄糖服务的 BLE 设备读取葡萄糖测量值。但是我无法读取已经进行的测量,永远不会调用 onCharacteristicChanged 回调。我的代码片段如下:

 @Override
 public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        super.onServicesDiscovered(gatt, status);
        if (status == BluetoothGatt.GATT_SUCCESS) {
          mGlucoseService = 
          gatt.getService(BluetoothUUIDS.GLUCOSE_MEASUREMENT_SERVICE_UUID);
          mGlucoseCharacteristic = 
          mGlucoseService.getCharacteristic(BluetoothUUIDS
                    .GLUCOSE_MEASUREMENT_CHARACTERISTIC_UUID);
            gatt.setCharacteristicNotification(mGlucoseCharacteristic, true);
            BluetoothGattDescriptor descMeasurement = mGlucoseCharacteristic.getDescriptor(BluetoothUUIDS
                    .CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
            descMeasurement.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            gatt.writeDescriptor(descMeasurement);

            mGlucoseContextCharacteristic = mGlucoseService.getCharacteristic(BluetoothUUIDS
                    .GLUCOSE_MEASUREMENT_CONTEXT_CHARACTERISTIC_UUID);
            gatt.setCharacteristicNotification(mGlucoseContextCharacteristic, true);
            BluetoothGattDescriptor descContext = mGlucoseCharacteristic.getDescriptor(BluetoothUUIDS
                    .CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
            descContext.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            gatt.writeDescriptor(descContext);

            mRecordAccessCharacteristic = mGlucoseService.getCharacteristic(BluetoothUUIDS
                    .GLUCOSE_RECORD_ACCESS_CHARACTERISTIC_UUID);
            gatt.setCharacteristicNotification(mRecordAccessCharacteristic, true);
            mRecordAccessConfigurationDescriptor = mRecordAccessCharacteristic.getDescriptor(BluetoothUUIDS
                    .CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
            if (mRecordAccessConfigurationDescriptor != null) {
                mRecordAccessConfigurationDescriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);

                new Thread(new Runnable() {
                    public void run() {
                        boolean success = false;
                        while (!success) {
                            if (mGatt.writeDescriptor(mRecordAccessConfigurationDescriptor)){
                                success = true;
                            }
                        }
                    }
                }).start();

为了获取我正在使用的记录:

mRecordAccessCharacteristic.setValue(new byte[2]);
mRecordAccessCharacteristic.setValue(OPCODE_REPORT_NUMBER_OF_RECORDS, BluetoothGattCharacteristic
            .FORMAT_UINT8, 0);
    mRecordAccessCharacteristic.setValue(OPERATOR_ALL_RECORDS, BluetoothGattCharacteristic.FORMAT_UINT8, 1);
    mGattDevice.writeCharacteristic(mRecordAccessCharacteristic);

或者

    byte[] data = new byte[2];
    data[0] = 0x01; // Report Stored records
    data[1] = 0x01; // All records
    mRecordAccessCharacteristic.setValue(data);
    mGattDevice.writeCharacteristic(mRecordAccessCharacteristic);

两种方式都行不通。请,任何帮助将不胜感激!

4

0 回答 0