iOS 蓝牙分段发送数据代码
来源:互联网 发布:老克勒淘宝店是真是假 编辑:程序博客网 时间:2024/05/21 06:55
//
- (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];
}
}
0 0
- iOS 蓝牙分段发送数据代码
- IOS蓝牙连接和发送数据
- iOS学习,蓝牙,iOS设备模拟BLE发送数据
- android 蓝牙发送数据
- iOS的蓝牙连接、数据接收及发送
- iOS 蓝牙外设检索、发送、接受数据问题。
- iOS的蓝牙连接 数据接收及发送
- 【iOS官方文档翻译】iOS的蓝牙连接、数据接收及发送
- 【iOS官方文档翻译】iOS的蓝牙连接、数据接收及发送
- 手机蓝牙接收数据分段----bytebuffer可以解决
- STM32F3使用USART串口DMA发送数据,使用蓝牙发送
- iOS udp发送数据
- 分段数据
- 数据转换成蓝牙数据发送的data
- iOS蓝牙开发:蓝牙连接和数据读写
- [iOS蓝牙]蓝牙连接并通过特征CBCharacteristic读写数据
- iOS蓝牙开发:蓝牙连接和数据读写
- iOS蓝牙开发:蓝牙连接和数据读写
- Leetcode 322. Coin Change (Medium) (cpp)
- java中标签的使用(附带break和continue)
- servlet单例模式的理解
- 友盟社会化分享集成微博分享直接报错的解决方法
- 有关/etc/resolv.conf、/etc/hosts、/etc/sysconfig/network
- iOS 蓝牙分段发送数据代码
- 小题目
- doTag方法程序的测试
- 开发时Activity生命周期异常记录
- Thorlabs APT activeX 开发应用
- MySQL中mysql.sock找不到的解决方法
- Intent详解
- Andrew Ng coursera上的《机器学习》ex8
- android studio升级后的各种错误解决