【问题标题】:Android BLE characteristic.getProperties returns 16 what does it mean?Android BLE特性.getProperties返回16是什么意思?
【发布时间】:2018-08-01 07:59:47
【问题描述】:

您好,我正在尝试从 BLE 血糖仪读取数据。当我试图读取“00002a18-0000-1000-8000-00805f9b34fb”的特征时,它只不过是 BLOOD_GLUCOSE_MEASUREMENT UUID,characteristic.getProperties 方法返回 16,并且我的 onCharacteristicRead 方法本身没有被调用。请帮助我如何阅读 BLOOD_GLUCOSE_MEASUREMENT 特征。

private final ExpandableListView.OnChildClickListener servicesListClickListner =
            new ExpandableListView.OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                            int childPosition, long id) {
                    if (mGattCharacteristics != null) {
                        final BluetoothGattCharacteristic characteristic =
                                mGattCharacteristics.get(groupPosition).get(childPosition);
                        final int charaProp = characteristic.getProperties();
                        if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
                            // If there is an active notification on a characteristic, clear
                            // it first so it doesn't update the data field on the user interface.
                            if (mNotifyCharacteristic != null) {
                                mBluetoothLeService.setCharacteristicNotification(
                                        mNotifyCharacteristic, false);
                                mNotifyCharacteristic = null;
                            }
                            mBluetoothLeService.readCharacteristic(characteristic);
                        }
                        if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                            mNotifyCharacteristic = characteristic;
                            mBluetoothLeService.setCharacteristicNotification(
                                    characteristic, true);
                        }
                        return true;
                    }
                    return false;
                }
    };

而我的 readCharacteristic 方法是

public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
        final byte[] data = characteristic.getValue();

    if(data != null && data.length > 0){
        final char[] out = new char[data.length * 3 - 1];
        for(int j = 0; j < data.length; j++) {
            int v = data[j] & 0xFF;
            out[j * 3] = HEX_ARRAY[v >>> 4];
            out[j * 3 + 1] = HEX_ARRAY[v & 0x0F];
            if(j != data.length - 1)
                out[j * 3 + 2] = '-';
        }
    }

        mBluetoothGatt.readCharacteristic(characteristic);
    }

而我的 setCharacteristicNotification 方法是这样的:

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

        for (BluetoothGattService service : mBluetoothGatt.getServices()) {
            if ((service == null) || (service.getUuid() == null)) {
                continue;
            }
            if (SampleGattAttributes.BLOOD_GLUCOSE_SERVICE.equalsIgnoreCase(service
                    .getUuid().toString())) {

                BluetoothGattCharacteristic charGM =
                        mBluetoothGatt.getService(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_SERVICE))
                                .getCharacteristic(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_MEASUREMENT));
                mBluetoothGatt.setCharacteristicNotification(charGM, enabled);
                BluetoothGattDescriptor descGM = charGM.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
                descGM.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                mBluetoothGatt.writeDescriptor(descGM);

                BluetoothGattCharacteristic charGMC =
                        mBluetoothGatt.getService(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_SERVICE))
                                .getCharacteristic(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_MEASUREMENT_context));
                mBluetoothGatt.setCharacteristicNotification(charGMC, enabled);
                BluetoothGattDescriptor descGMC = charGMC.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
                descGMC.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                mBluetoothGatt.writeDescriptor(descGMC);

                BluetoothGattCharacteristic charRACP =
                        mBluetoothGatt.getService(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_SERVICE))
                                .getCharacteristic(UUID.fromString(SampleGattAttributes.RECORD_ACCESS_CONTROL_POINT));
                mBluetoothGatt.setCharacteristicNotification(charRACP, enabled);
                BluetoothGattDescriptor descRACP = charRACP.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
                descRACP.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                mBluetoothGatt.writeDescriptor(descRACP);


                /*BluetoothGattCharacteristic charBarrery =
                        mBluetoothGatt.getService(UUID.fromString(SampleGattAttributes.SERVICE_BATTERY_SERVICE))
                                .getCharacteristic(UUID.fromString(SampleGattAttributes.CHAR_BATTERY_LEVEL));
                mBluetoothGatt.setCharacteristicNotification(charBarrery, enabled);
                BluetoothGattDescriptor descBarrery = charBarrery.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
                descBarrery.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                mBluetoothGatt.writeDescriptor(descBarrery);*/

               /* runOnUiThread(new Runnable() {
                    public void run() {
                        btnUpdateData.setEnabled(true);
                    };
                });*/
            }
        }
    }

【问题讨论】:

  • 尝试添加onCharacteristicChanged()回调
  • @Amrit kumar 我已经 onCharacteristicChanged() 回调,但仍然无法正常工作。
  • 您是否启用了此特征的通知,请发布您的代码
  • 我通过编辑我的问题发布了代码。请帮帮我@Amrit kumar。

标签: android bluetooth-lowenergy


【解决方案1】:

characteristic.getProperties() 返回 16,

这是BluetoothGattCharacteristic.PROPERTY_NOTIFY的值。

你已经设置了特征通知,

if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
    mNotifyCharacteristic = characteristic;
    mBluetoothLeService.setCharacteristicNotification(
        characteristic, true);
}

您现在需要致电readCharacteristic(desiredCharacteristic)

或者/And on onDescriptorWrite() 回调。

编辑:

调用后

mBluetoothGatt.readCharacteristic(characteristic);

你需要在onCharacteristicRead()回调上接收数据

onCharacteristicRead(BluetoothGatt gatt, ..., ...){
    final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
    final byte[] data = characteristic.getValue();

    if(data != null && data.length > 0){
        final char[] out = new char[data.length * 3 - 1];
        for(int j = 0; j < data.length; j++) {
            int v = data[j] & 0xFF;
            out[j * 3] = HEX_ARRAY[v >>> 4];
            out[j * 3 + 1] = HEX_ARRAY[v & 0x0F];
            if(j != data.length - 1)
                out[j * 3 + 2] = '-';
        }
    }
}

【讨论】:

  • 好的 @Amrit kumar 感谢您的时间和帮助。但我有一个问题,我们如何在 onDescriptorWrite() 回调中传递 BluetoothGattCharacteristic?
  • 嗨@Amrit kumar 我尝试在 if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0){} 之后调用 readCharacteristic(desiredCharacteristic) 方法,但它没有用..
  • 您还需要更改|到一个 &。否则 if 语句将永远为真。
  • No @Amrit kumar 我的 onCharacteristicRead 回调本身没有被调用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 2013-04-25
  • 1970-01-01
  • 1970-01-01
  • 2012-09-06
  • 2014-05-16
相关资源
最近更新 更多