首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android characteristic.getProperties返回16是什么意思?

Android characteristic.getProperties返回16是什么意思?
EN

Stack Overflow用户
提问于 2018-08-01 07:59:47
回答 2查看 1K关注 0票数 0

嗨,我正在试着读BLE血糖计的数据。当我试图读取"00002a18-0000-1000-8000-00805f9b34fb“的特性时,characteristic.getProperties方法返回16,而我的onCharacteristicRead方法本身没有被调用。请帮助我如何阅读BLOOD_GLUCOSE_MEASUREMENT的特点。

代码语言:javascript
运行
复制
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方法是

代码语言:javascript
运行
复制
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方法是:

代码语言:javascript
运行
复制
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);
                    };
                });*/
            }
        }
    }
EN

回答 2

Stack Overflow用户

发布于 2018-08-01 09:37:45

characteristic.getProperties()返回16,

它是BluetoothGattCharacteristic.PROPERTY_NOTIFY的值。

你已经设置了特征通知,

代码语言:javascript
运行
复制
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
    mNotifyCharacteristic = characteristic;
    mBluetoothLeService.setCharacteristicNotification(
        characteristic, true);
}

你现在需要打电话给readCharacteristic(desiredCharacteristic)

或/和onDescriptorWrite()回调。

编辑:

打电话后

代码语言:javascript
运行
复制
mBluetoothGatt.readCharacteristic(characteristic);

您需要接收onCharacteristicRead()回调的数据

代码语言:javascript
运行
复制
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] = '-';
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2022-06-02 10:49:54

16是通知的常量。您可以使用此功能,检查是否适合您的特定用途。attr应填充以下常量之一:

代码语言:javascript
运行
复制
 public static final int PROPERTY_EXTENDED_PROPS = 128;
 public static final int PROPERTY_INDICATE = 32;
 public static final int PROPERTY_NOTIFY = 16;
 public static final int PROPERTY_READ = 2;
 public static final int PROPERTY_SIGNED_WRITE = 64;
 public static final int PROPERTY_WRITE = 8;
 public static final int PROPERTY_WRITE_NO_RESPONSE = 4;

 private boolean checkForCharacteristicProperty(BluetoothGattCharacteristic chara, int attr) {
     return chara != null && ((chara.getProperties() & attr) != 0);
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51628204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档