iOS: 将数据添加到 BLE 特征抛出错误要求只读特征
iOS: Adding data to a BLE characteristic throwing error asking for read-only characteristic
我有一个 iOS BLE 服务,可以通告并可以连接,但现在我想为外围设备添加一个特性。我想我只是将一个新的 NSData 对象添加到值 属性 以创建特征,但是每当我在外围管理器上调用 addService 时,我都会收到此错误:
2015-09-24 09:02:59.456 peripheral[459:27589] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Characteristics with cached values must be read-only'
我添加特征的方法是这样的:
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
_shareService = [[CBMutableService alloc] initWithType:_serviceUUID primary:YES];
_notifyUUID = [CBUUID UUIDWithString:@"F098FDEF-B82C-43F1-8BFB-18757743BA10"];
_notificationCharacteristic = [[CBMutableCharacteristic alloc] initWithType:_notifyUUID
properties:CBCharacteristicPropertyNotify
value:[@"Some Data" dataUsingEncoding:NSUTF16StringEncoding]
permissions:CBAttributePermissionsReadable];
[_shareService setCharacteristics:@[_notificationCharacteristic]];
[_pMgr addService:_shareService];
[self startAdvertising];
}
}
虽然当我将价值项目更改为零时,它再次宣传正常。我确定我遗漏了一些简单的东西,但我对 iOS 还是很陌生,所以任何帮助都会很棒。谢谢!
来自 CBMutableCharacteristic
初始化方法的文档 -
value - The characteristic value to be cached. If nil, the value is
dynamic and will be requested on demand.
这意味着如果您在创建 CBMutableCharacteristic
时指定了一个非零值,那么它就是一个 'cached characteristic' 并且正如错误消息所说,您以后无法更改该值。在创建 CBMutableCharacteristic
时指定 nil
- 您在 didReceiveReadRequest
CBPeripheralManagerDelegate
方法中请求时提供值。
我有一个 iOS BLE 服务,可以通告并可以连接,但现在我想为外围设备添加一个特性。我想我只是将一个新的 NSData 对象添加到值 属性 以创建特征,但是每当我在外围管理器上调用 addService 时,我都会收到此错误:
2015-09-24 09:02:59.456 peripheral[459:27589] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Characteristics with cached values must be read-only'
我添加特征的方法是这样的:
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
_shareService = [[CBMutableService alloc] initWithType:_serviceUUID primary:YES];
_notifyUUID = [CBUUID UUIDWithString:@"F098FDEF-B82C-43F1-8BFB-18757743BA10"];
_notificationCharacteristic = [[CBMutableCharacteristic alloc] initWithType:_notifyUUID
properties:CBCharacteristicPropertyNotify
value:[@"Some Data" dataUsingEncoding:NSUTF16StringEncoding]
permissions:CBAttributePermissionsReadable];
[_shareService setCharacteristics:@[_notificationCharacteristic]];
[_pMgr addService:_shareService];
[self startAdvertising];
}
}
虽然当我将价值项目更改为零时,它再次宣传正常。我确定我遗漏了一些简单的东西,但我对 iOS 还是很陌生,所以任何帮助都会很棒。谢谢!
来自 CBMutableCharacteristic
初始化方法的文档 -
value - The characteristic value to be cached. If nil, the value is dynamic and will be requested on demand.
这意味着如果您在创建 CBMutableCharacteristic
时指定了一个非零值,那么它就是一个 'cached characteristic' 并且正如错误消息所说,您以后无法更改该值。在创建 CBMutableCharacteristic
时指定 nil
- 您在 didReceiveReadRequest
CBPeripheralManagerDelegate
方法中请求时提供值。