2014-01-23 56 views

回答

12
/** 
    * @return Returns <b>true</b> if property is writable 
    */ 
    public static boolean isCharacteristicWriteable(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 isCharacterisitcReadable(BluetoothGattCharacteristic pChar) { 
     return ((pChar.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) != 0); 
    } 

    /** 
    * @return Returns <b>true</b> if property is supports notification 
    */ 
    public boolean isCharacterisiticNotifiable(BluetoothGattCharacteristic pChar) { 
     return (pChar.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0; 
    } 
+0

我真的不知道为什么,但Android的官方样品中是 “如果((charaProp | BluetoothGattCharacteristic.PROPERTY_READ)> 0)” 使用 '&' 在我看来是正确的,但“| '正在为我工​​作。无论如何,你能解释一下,为什么这个BluetoothGattCharacteristic属性有它们的值?例如对于PROPERTY_READ 0x02。在任何服务中,Read是我的第一个属性。 – Krystian

+0

这些是服务地址,您在设备上运行的每项服务都有不同的地址。 –

0

我遇到了类似的问题,其中示例代码只有在因操作符“|”而导致特征为READ时才起作用。如果特性属于其他类型,如通知或写入,则代码将始终将其设置为READ。正确的代码应该如下所示:

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

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

(......与其他案件继续)

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

大卫

+0

你的意思是“charaProp&BluetoothGattCharacteristic.PROPERTY_NOTIFICATION”,对吧? – Emil