12

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

AZ_
  • 21,688
  • 25
  • 143
  • 191

2 Answers2

16
    /**
     * @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;
    }
Zubair Akber
  • 2,760
  • 13
  • 30
AZ_
  • 21,688
  • 25
  • 143
  • 191
  • I really do not know why, but in Android official sample is " if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) " Using '&' is in my opinion correct, but ' | ' is working for me. Anyway, could you explain, why this BluetoothGattCharacteristic properties have values which they have? For example 0x02 for PROPERTY_READ. In any service Read is my first property. – Krystian Dec 28 '16 at 15:41
  • These are service address, you have different address for each service running on a device. – AZ_ Dec 30 '16 at 08:04
0

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

us_david
  • 4,431
  • 35
  • 29