做了一个简单的测试BLE传输速度的app,central使用Mac,peripheral使用iphone6
并非通过传统的传输文件进行测试,所以对于传输文件的问题并不是很适用。
(传输文件的话可以使用NSData将文件分割后再进行传输)
peripheral接收到central的读取数据请求时,每次传送100Byte的数据。
(为什么是100Byte呢?因为楼主测试时,每次最大能传输的数据量是102Byte。。。)
在central可以设置读取数据的大小,并显示平均速度。
peripheral代码:
接收到读取数据请求时的动作:
- (void)peripheralManager:(CBPeripheralManager *)peripheral
didReceiveReadRequest:(CBATTRequest *)request
{
[self publishLocalNotificationWithMessage:@"Read!"];
NSLog(@"Readリクエスト受信! requested service uuid:%@ characteristic uuid:%@ value:%@",
request.characteristic.service.UUID,
request.characteristic.UUID,
request.characteristic.value);
if ([request.characteristic.UUID isEqual:self.characteristic.UUID]) {
//request.value = self.characteristic.value;
request.value = [self setData];
[self.peripheralManager respondToRequest:request
withResult:CBATTErrorSuccess];
}
}
每次读取100Byte的数据(test.csv文件大小为100Byte)
- (NSData *)setData{
NSData *data = [NSData new];
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"test" ofType:@"csv"];
if (![path isEqualToString:@""]) {
data = [[NSData alloc] initWithContentsOfFile:path];
}
return data;
}
central代码:
一些变量定义:
static int count = 100;
static int dataLength;
static NSDate *startDate;
static NSDate *endDate;
static NSDate *nowDate;// 可省略,直接使用endDate
static NSString *filePath;
点击read按钮时的动作:
- (IBAction)readBtnTapped:(id)sender {
// 初期化
if (filePath == nil) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.csv"];
filePath = [documentsDirectory stringByAppendingPathComponent:@"test.csv"];
}
[self.progressLbl setStringValue:@"0%"];
[self.speedLbl setStringValue:@"0kbps"];
[self.receivedStr setString:@""];
[self.warningLbl setTextColor:[NSColor blackColor]];
NSString *inputLength = [self.dataLengthTxt stringValue];
if (![inputLength isEqualToString:@""]) {
// 整数チェック
NSCharacterSet *digitCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
NSCharacterSet *aCharacterSet = [NSCharacterSet characterSetWithCharactersInString:inputLength];
if ([digitCharSet isSupersetOfSet:aCharacterSet]) {
NSLog(@"数字のみ");
NSInteger num = [inputLength integerValue];
dataLength = (int)num * 10;
} else {
NSLog(@"数字以外の文字が存在");
[self.receivedStr setString:@""];
[self.warningLbl setTextColor:[NSColor redColor]];
return;
}
}else{
// 10kB
dataLength = 100;
}
count = dataLength;
NSLog(@"dataLength: %dKB", dataLength/10);
NSLog(@"入力: %@", inputLength);
// 受信開始
[self.peripheral readValueForCharacteristic:self.characteristic];
NSLog(@"送信開始");
startDate = [NSDate date];
//preDate = startDate;
}
读取数据结束时的动作
- (void) peripheral:(CBPeripheral *)peripheral
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic // complete reading!
error:(NSError *)error
{
if (error) {
NSLog(@"Update失敗...error:%@, characteristic uuid:%@, value%@", error, characteristic.UUID, characteristic.value);
return;
}
NSLog(@"Update成功!service uuid:%@, characteristice uuid:%@, value%@",
characteristic.service.UUID, characteristic.UUID, characteristic.value);
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"1111"]]) {// peripheral的characteristic的UUID
//NSLog(@"length: %lu", (unsigned long)characteristic.value.length);
NSData *data = characteristic.value;
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[self.receivedStr appendString:str];
//NSLog(@"receivedStr: %@", self.receivedStr);
NSData *receivedData = [self.receivedStr dataUsingEncoding:NSUTF8StringEncoding];
// 進捗
float per = (float)(receivedData.length)/(dataLength*100);
int num = (int)(per * 100);
[self.progressLbl setStringValue:[NSString stringWithFormat:@"%d%@", num, @"%"]];
// 速度
nowDate = [NSDate date];
// 平均速度
NSTimeInterval interval = [nowDate timeIntervalSinceDate:startDate];
float speed = (receivedData.length*8)/(float)interval/1000;
[self.speedLbl setStringValue:[NSString stringWithFormat:@"%.1f%@", speed, @"kbps"]];
// 受信を続ける
if(count > 1){
[self.peripheral readValueForCharacteristic:self.characteristic];
count--;
//preDate = [NSDate date];
}else{
//NSData *receivedData = [self.receivedStr dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"length: %lu", (unsigned long)receivedData.length);
[receivedData writeToFile:filePath atomically:YES];
NSLog(@"送信終了");
endDate = [NSDate date];
// 開始時間と終了時間の差を表示
NSTimeInterval interval = [endDate timeIntervalSinceDate:startDate];
NSLog(@"処理開始時間 = %@",[self getDateString:startDate]);
NSLog(@"処理終了時間 = %@",[self getDateString:endDate]);
NSLog(@"処理時間 = %.3f秒",interval);
// 平均速度
//float speed = (receivedData.length*8)/(float)interval/1000;
//[self.speedLbl setStringValue:[NSString stringWithFormat:@"%.1f%@", speed, @"kbps"]];
[self.lbl03 setStringValue:@"OK"];
}
}
}
返回日期
- (NSString*)getDateString:(NSDate*)date
{
// 日付フォーマットオブジェクトの生成
NSDateFormatter *dateFormatter = [NSDateFormatter new];
// フォーマットを指定の日付フォーマットに設定
[dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss.SSS"];
// 日付の文字列を生成
NSString *dateString = [dateFormatter stringFromDate:date];
return dateString;
}