用流操作一个大的文件,分段传输,大致代码如下,实际应用中得加上状态机
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ble_app_ancs" ofType:@"bin"];
NSInputStream *inputStream = [[NSInputStream alloc] initWithFileAtPath: filePath];
[inputStream open];
NSInteger maxLength = 20;
uint8_t readBuffer [maxLength];
//是否已经到结尾标识
BOOL endOfStreamReached = NO;
// NOTE: this tight loop will block until stream ends
while (! endOfStreamReached)
{
NSInteger bytesRead = [inputStream read: readBuffer maxLength:maxLength];
if (bytesRead == 0)
{
//文件读取到最后
endOfStreamReached = YES;
}
else if (bytesRead == -1)
{
//文件读取错误
endOfStreamReached = YES;
}
else
{
//将字符不断