iOS 蓝牙分段发送数据代码
2016-08-26 16:17
1061 查看
//
蓝牙的支持能力是每命令发送20byte
超过20,要分段发送
- (void)writeData:(NSData *)data
{
NSLog(@"send:%@", data);
_responseTimer = [NSTimer scheduledTimerWithTimeInterval:_responseInterval target:self selector:@selector(timeoutForResponse) userInfo:nil repeats:NO];
if (self.writingChar && self.notifyChar)
{
int len = (int)[data length];
//当写入的数据大于20个字节时,分包发送
if (len > 20)
{
/**
* subdataWithRange( , );
* @param 0 从第0个字节开始截取
* @param 20 截取数据长度为20
*/
NSData *dataTemp = [data subdataWithRange:NSMakeRange(0, 20)];
/**
dataTemp: 写入的数据
self.writingChar: 写给哪些特征
CBCharacteristicWriteWithResponse: 通过此响应纪录是否写入成功
*/
[self.cbPeripheral writeValue:dataTemp forCharacteristic:self.writingChar type:CBCharacteristicWriteWithResponse];
/**
举例:
if(len == 43),则 len / 20 == 2;
i == 0;
i == 1;
if(i == 0), 则 len - (20 + 20 * i) == 23;
if(i == 1), 则 len - (20 + 20 * i) == 3;
*/
//这里分包发送
for (int i = 0; i < len / 20; i++)
{
int lenTemp = 20;
if ((len - (20 + 20 * i) < 20) && (len - (20 + 20 * i) > 0))
{//i == 1
lenTemp = len - (20 + 20 * i);
}
if (len - (20 + 20 * i) > 0)
{//i ==0;
dataTemp = [data subdataWithRange:NSMakeRange(20 + 20 * i, lenTemp)];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.cbPeripheral writeValue:dataTemp forCharacteristic:self.writingChar type:CBCharacteristicWriteWithResponse];
});
}
}
}
else//小于20个字节时,直接写入
{
[self.cbPeripheral writeValue:data forCharacteristic:self.writingChar type:CBCharacteristicWriteWithResponse];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
Byte b[] = {3};
[self.cbPeripheral writeValue:[NSData dataWithBytes:b length:1] forCharacteristic:self.notifyChar type:CBCharacteristicWriteWithResponse];
});
}
else
{
NSLog(@"木有找到");
self.sendData = data;
[self.cbPeripheral discoverServices:nil];
}
}
蓝牙的支持能力是每命令发送20byte
超过20,要分段发送
- (void)writeData:(NSData *)data
{
NSLog(@"send:%@", data);
_responseTimer = [NSTimer scheduledTimerWithTimeInterval:_responseInterval target:self selector:@selector(timeoutForResponse) userInfo:nil repeats:NO];
if (self.writingChar && self.notifyChar)
{
int len = (int)[data length];
//当写入的数据大于20个字节时,分包发送
if (len > 20)
{
/**
* subdataWithRange( , );
* @param 0 从第0个字节开始截取
* @param 20 截取数据长度为20
*/
NSData *dataTemp = [data subdataWithRange:NSMakeRange(0, 20)];
/**
dataTemp: 写入的数据
self.writingChar: 写给哪些特征
CBCharacteristicWriteWithResponse: 通过此响应纪录是否写入成功
*/
[self.cbPeripheral writeValue:dataTemp forCharacteristic:self.writingChar type:CBCharacteristicWriteWithResponse];
/**
举例:
if(len == 43),则 len / 20 == 2;
i == 0;
i == 1;
if(i == 0), 则 len - (20 + 20 * i) == 23;
if(i == 1), 则 len - (20 + 20 * i) == 3;
*/
//这里分包发送
for (int i = 0; i < len / 20; i++)
{
int lenTemp = 20;
if ((len - (20 + 20 * i) < 20) && (len - (20 + 20 * i) > 0))
{//i == 1
lenTemp = len - (20 + 20 * i);
}
if (len - (20 + 20 * i) > 0)
{//i ==0;
dataTemp = [data subdataWithRange:NSMakeRange(20 + 20 * i, lenTemp)];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.cbPeripheral writeValue:dataTemp forCharacteristic:self.writingChar type:CBCharacteristicWriteWithResponse];
});
}
}
}
else//小于20个字节时,直接写入
{
[self.cbPeripheral writeValue:data forCharacteristic:self.writingChar type:CBCharacteristicWriteWithResponse];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
Byte b[] = {3};
[self.cbPeripheral writeValue:[NSData dataWithBytes:b length:1] forCharacteristic:self.notifyChar type:CBCharacteristicWriteWithResponse];
});
}
else
{
NSLog(@"木有找到");
self.sendData = data;
[self.cbPeripheral discoverServices:nil];
}
}
相关文章推荐
- 【iOS官方文档翻译】iOS的蓝牙连接、数据接收及发送
- iOS蓝牙开发 —— 连接打印机发送16进制数据的问题
- iOS的蓝牙连接、数据接收及发送
- iOS学习,蓝牙,iOS设备模拟BLE发送数据
- iOS 蓝牙外设检索、发送、接受数据问题。
- 【iOS官方文档翻译】iOS的蓝牙连接、数据接收及发送
- iOS 一个在UDP/TCP通信时,对16进制、10进制、NSData等数据之间的转换发送等处理的工具类库,一行代码将16进制字符串轻松转换成UDP需要的指令数据
- IOS蓝牙连接和发送数据
- ios后台处理数据存储线程的代码
- iOS开发之邮件发送代码
- php模拟socket一次连接,多次发送数据的实现代码
- android Timer在通话过程中中断——Handler中有数据发送代码
- ios开发之发送UDP广播并接收数据
- iOS开发之邮件发送代码
- 在网页中发送iMessage消息的代码(支持Mac和iOS)
- iOS开发之邮件发送代码
- ios开发之ASIHttpRequest-发送数据
- ios开发之发送UDP广播并接收数据
- ios开发之ASIHttpRequest-发送数据
- 从 iOS 向 ASP.NET WebServices 发送数据