Is it possible to update dynamic characteristic on peripheral side by update method without didReceiveReadRequest:
?
For example, I need to send to all subscribed centrals a message 'hello, my friends, I need to tell you a story' - long message and can't be processed in one update.
I have code sample so far:
#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];
}
If you need to send to your peer a long value that doesn't fit into one packet, you should first design a protocol to disassemble your value at peripheral side and assemble it back at the central side, then cut your long value into smaller chunks, send them one-by-one and assemble them back at the receiver.
iOS8+ devices usually negotiate MTU equal to 158 bytes that can contain up to 155 bytes of payload. Knowing this, you can design the protocol as follows.
byte 0 = 1, byte 1 = i, bytes 2-154 = string contents
and send them to the central device.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.