Created
July 27, 2018 08:24
-
-
Save SunXiaoShan/43e0fcd89bbc93bd5c2a244739b5aa8f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (void)sendData:(NSData *)sendMsg | |
| dataIndex:(NSInteger)sendMsgIndex { | |
| // First up, check if we're meant to be sending an EOM | |
| static BOOL sendingEOM = NO; | |
| NSString *logMessage = nil; | |
| const int NOTIFY_MTU = 20; | |
| const float SendMessageTimeInterval = 0.2f; | |
| if (sendingEOM) { | |
| sendingEOM = NO; | |
| return; | |
| } | |
| NSInteger sendMsgIndex = 0; | |
| BOOL didSend = YES; | |
| while (didSend) { | |
| // Make the next chunk | |
| // Work out how big it should be | |
| NSInteger amountToSend = sendMsg.length; | |
| // Can't be longer than 20 bytes | |
| if (amountToSend > NOTIFY_MTU) { | |
| amountToSend = NOTIFY_MTU; | |
| } | |
| // Copy out the data we want | |
| NSData *chunk = [NSData dataWithBytes:sendMsg.bytes+sendMsgIndex length:amountToSend]; | |
| [NSThread sleepForTimeInterval:SendMessageTimeInterval]; | |
| if (self.characteristicM == nil) { | |
| break; | |
| } | |
| didSend = [self.peripheralManager updateValue:chunk | |
| forCharacteristic:self.characteristicM | |
| onSubscribedCentrals:nil]; | |
| if (didSend == NO) { | |
| return; | |
| } | |
| NSString *stringFromData = [[NSString alloc] initWithData:chunk encoding:NSUTF8StringEncoding]; | |
| sendMsgIndex += amountToSend; | |
| if (sendMsgIndex >= sendMsg.length) { | |
| sendingEOM = NO; | |
| return; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment