Android How to read BLE properties Readable Writab

2020-06-12 04:41发布

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

2条回答
贼婆χ
2楼-- · 2020-06-12 05:32

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

查看更多
不美不萌又怎样
3楼-- · 2020-06-12 05:38
    /**
     * @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;
    }
查看更多
登录 后发表回答