使用 Swift 和 CoreBluetooth 创建静态特性
Creating a Static Characteristic with Swift and CoreBluetooth
我正在使用 Swift 2.2.1 构建一个用作蓝牙外围设备的 iOS 应用程序。
我想将一个人的名字宣传为 static 特征,所以我正在创建这样的特征:
// Build the NAME characteristic
if (identity.name != nil) {
nameCharacteristic =
CBMutableCharacteristic(type: performerNameCharacteristicUUID,
properties: ([CBCharacteristicProperties.Read, CBCharacteristicProperties.Broadcast]),
value: myIdentity?.name?.dataUsingEncoding(NSUTF8StringEncoding,
allowLossyConversion: false),
permissions: CBAttributePermissions.Readable)
characteristicsArray.append(nameCharacteristic!)
}
程序运行时调用addServices弹出异常:
*** 由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',
原因:'Characteristics with cached values must be read-only'
如能提出我可能做错的想法,将不胜感激!
您不能将 CBCharacteristicProperties.Broadcast
属性 与您自己创建的特征一起使用。来自 documentation:
CBCharacteristicPropertyBroadcast The characteristic’s value can be broadcast using a characteristic configuration descriptor.
This property is not allowed for local characteristics published via
the addService:
method of the CBPeripheralManager
class. This means
that you cannot use this property when you initialize a new
CBMutableCharacteristic
object via the
initWithType:properties:value:permissions:
method of the
CBMutableCharacteristic
class.
我正在使用 Swift 2.2.1 构建一个用作蓝牙外围设备的 iOS 应用程序。
我想将一个人的名字宣传为 static 特征,所以我正在创建这样的特征:
// Build the NAME characteristic
if (identity.name != nil) {
nameCharacteristic =
CBMutableCharacteristic(type: performerNameCharacteristicUUID,
properties: ([CBCharacteristicProperties.Read, CBCharacteristicProperties.Broadcast]),
value: myIdentity?.name?.dataUsingEncoding(NSUTF8StringEncoding,
allowLossyConversion: false),
permissions: CBAttributePermissions.Readable)
characteristicsArray.append(nameCharacteristic!)
}
程序运行时调用addServices弹出异常:
*** 由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException', 原因:'Characteristics with cached values must be read-only'
如能提出我可能做错的想法,将不胜感激!
您不能将 CBCharacteristicProperties.Broadcast
属性 与您自己创建的特征一起使用。来自 documentation:
CBCharacteristicPropertyBroadcast The characteristic’s value can be broadcast using a characteristic configuration descriptor.
This property is not allowed for local characteristics published via the
addService:
method of theCBPeripheralManager
class. This means that you cannot use this property when you initialize a newCBMutableCharacteristic
object via theinitWithType:properties:value:permissions:
method of theCBMutableCharacteristic
class.