简体   繁体   English

Android如何阅读BLE属性可读写可通报的GATT特性

[英]Android How to read BLE properties Readable Writable Notifiable GATT Characteristics

如何阅读BluetoothGattCharacteristic属性,如特征ReadableWritableNotifiable

    /**
     * @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;
    }

I ran into the similar problem where the sample code ONLY works when the characteristic is READ because of the operator "|". 我遇到了类似的问题,由于操作符“|”,当特征为READ时,示例代码仅起作用。 If the characteritic is of other types such as Notification or Write, the code will always set it sup as READ. 如果特征属于其他类型,例如Notification或Write,则代码将始终将其设置为READ。 The correct code should be as the following: 正确的代码应如下所示:

if((charaProp & BluetoothGattCharacteristic.PROPERTY_READ) > 0){ 

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

(...Continue with other cases) (......继续其他案例)

Again, the google sample code is NOT correct. 同样,谷歌示例代码不正确。

David 大卫

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

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