Android How to read BLE properties Readable Writable Notifiable GATT Characteristics

為{幸葍}努か 提交于 2019-12-04 18:03:47

问题


How to read BluetoothGattCharacteristic properties like is characteristic Readable, Writable or Notifiable.


回答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;
    }



回答2:


I ran into the similar problem where the sample code ONLY works when the characteristic is READ because of the operator "|". If the characteritic is of other types such as Notification or Write, the code will always set it sup as 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



来源:https://stackoverflow.com/questions/21300915/android-how-to-read-ble-properties-readable-writable-notifiable-gatt-characteris

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!