当前位置: 首页 > 工具软件 > CoreBlueTooth > 使用案例 >

【Mac/iOS】解决CoreBluetooth中Characteristic的Properties显示问题

柳梓
2023-12-01

项目中希望能显示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
   }
 类似资料: