【问题标题】:Android How to read BLE properties Readable Writable Notifiable GATT CharacteristicsAndroid 如何读取 BLE 属性 Readable Writable Notifiable GATT 特性
【发布时间】:2014-02-13 13:44:34
【问题描述】:

如何读取BluetoothGattCharacteristic 属性,例如特征ReadableWritableNotifiable

【问题讨论】:

    标签: android bluetooth-lowenergy gatt characteristics


    【解决方案1】:
        /**
         * @return Returns <b>true</b> if property is writable
         */
        public static boolean isCharacteristicWritable(BluetoothGattCharacteristic pChar) {
            return (pChar.getProperties() & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) != 0;
        }
    
        /**
         * @return Returns <b>true</b> if property is Readable
         */
        public static boolean isCharacteristicReadable(BluetoothGattCharacteristic pChar) {
            return ((pChar.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) != 0);
        }
    
        /**
         * @return Returns <b>true</b> if property is supports notification
         */
        public boolean isCharacteristicNotifiable(BluetoothGattCharacteristic pChar) {
            return (pChar.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0;
        }
    

    【讨论】:

    • 我真的不知道为什么,但是在Android官方示例中是“ if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) ”使用'&'在我看来是正确的,但是'| ' 为我工作。无论如何,你能解释一下,为什么这个 BluetoothGattCharacteristic 属性具有它们所具有的值吗?例如 PROPERTY_READ 的 0x02。在任何服务中,阅读是我的第一个属性。
    • 这些是服务地址,设备上运行的每个服务都有不同的地址。
    【解决方案2】:

    我遇到了类似的问题,由于运算符“|”,示例代码仅在特征为 READ 时才有效。如果特征是其他类型,例如通知或写入,则代码将始终将其设置为 READ。正确的代码应该如下:

    if((charaProp & BluetoothGattCharacteristic.PROPERTY_READ) > 0){ 
    
    } else if(charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFICATION) > 0){
    }
    

    (...继续其他情况)

    同样,谷歌示例代码不正确。

    大卫

    【讨论】:

    • 你的意思是“charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFICATION”,对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多