是否可以通过不使用didReceiveReadRequest:
的更新方法更新外围设备上的动态特性?
例如,我需要向所有订阅的中心发送一条消息'你好,我的朋友们,我需要告诉你一个故事' - 长消息,无法在一次更新中处理。
到目前为止我有代码示例:
#pragma mark - Update
- (BOOL)updatePeripheralManager:(CBPeripheralManager *)peripheralManager withPeripheralName:(NSString *)peripheralName withCharacteristic:(CBCharacteristic *)characteristic byData:(NSData *)data {
self.failedUpdate = [[AIKBluetoothPeripheralManagerLastUpdate alloc] initWithPeripheralName:peripheralName withCharacteristic:characteristic withData:data];
if (peripheralManager && characteristic && data) {
return [peripheralManager updateValue:data forCharacteristic:(CBMutableCharacteristic *)characteristic onSubscribedCentrals:nil];
}
else {
DDLogDebug(@"%@ updatePeripheral: peripheral or characteristic or data not specified", self.debugDescription);
return NO;
}
}
- (void)updateContinuouslyPeripheralManager:(CBPeripheralManager *)peripheralManager withPeripheralName:(NSString *)peripheralName withCharacteristic:(CBCharacteristic *)characteristic byData:(NSData *)data {
NSData *chunk = data;
while (chunk != nil && [self updatePeripheralManager:peripheralManager withPeripheralName:peripheralName withCharacteristic:characteristic byData:chunk]) {
// request data
chunk = [self nextChunk];
}
}
- (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral {
DDLogVerbose(@"%@ isReadyToUpdateSubscribers", self.debugDescription);
DDLogDebug(@"%@ failedUpdate: %@", self.debugDescription, self.failedUpdate.debugDescription);
NSString *peripheralName = self.failedUpdate.peripheralName;
CBCharacteristic *characteristic = self.failedUpdate.characteristic;
NSData *chunk = self.failedUpdate.data;
[self updateContinuouslyPeripheralManager:self.peripheralManager withPeripheralName:peripheralName withCharacteristic:characteristic byData:chunk];
}
答案 0 :(得分:0)
如果你需要向同伴发送一个不适合一个数据包的长值,你应该首先设计一个协议,在外围一侧拆卸你的价值并在中央一侧汇回,然后削减你的长期价值进入较小的块,逐个发送它们并将它们组装回接收器。
iOS8 +设备通常协商MTU等于158个字节,最多可包含155个字节的有效负载。了解这一点,您可以按如下方式设计协议。
byte 0 = 1, byte 1 = i, bytes 2-154 = string contents
并将其发送到中央设备。