【Mac/iOS】解決CoreBluetooth中Characteristic的Properties顯示問題

項目中希望能顯示Characteristic的所有Properties,在外設管理器中,CBPeripheral類並未提供其顯示方法,但在CBCharacteristic中含有.properties屬性值。因此可以通過獲取屬性值對比系統定義的數值以獲取其Properties的功能。

代碼如下,直接複製代碼到工程,調用即可。

使用方法:

// 已有 CBPeripheral  myCharacteristic
NSLog(""Properties:\(propertiesString(properties: myCharacteristic.properties)!) \n")

代碼:

   //顯示屬性權限
   func propertiesString(properties: CBCharacteristicProperties)->(String)!{
       var propertiesReturn : String = ""

       if (properties.rawValue & CBCharacteristicProperties.broadcast.rawValue) != 0 {
           propertiesReturn += "broadcast|"
       }
       if (properties.rawValue & CBCharacteristicProperties.read.rawValue) != 0 {
           propertiesReturn += "read|"
       }
       if (properties.rawValue & CBCharacteristicProperties.writeWithoutResponse.rawValue) != 0 {
           propertiesReturn += "write without response|"
       }
       if (properties.rawValue & CBCharacteristicProperties.write.rawValue) != 0 {
           propertiesReturn += "write|"
       }
       if (properties.rawValue & CBCharacteristicProperties.notify.rawValue) != 0 {
           propertiesReturn += "notify|"
       }
       if (properties.rawValue & CBCharacteristicProperties.indicate.rawValue) != 0 {
           propertiesReturn += "indicate|"
       }
       if (properties.rawValue & CBCharacteristicProperties.authenticatedSignedWrites.rawValue) != 0 {
           propertiesReturn += "authenticated signed writes|"
       }
       if (properties.rawValue & CBCharacteristicProperties.extendedProperties.rawValue) != 0 {
           propertiesReturn += "indicate|"
       }
       if (properties.rawValue & CBCharacteristicProperties.notifyEncryptionRequired.rawValue) != 0 {
           propertiesReturn += "notify encryption required|"
       }
       if (properties.rawValue & CBCharacteristicProperties.indicateEncryptionRequired.rawValue) != 0 {
           propertiesReturn += "indicate encryption required|"
       }
       return propertiesReturn
   }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章