【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
   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值