iOS 蓝牙can发送超过8字节数据的数据超过20字节怎么办

主题 : 蓝牙写值发送崩溃了,请问这个是什么原因
级别: 侠客
UID: 533272
可可豆: 565 CB
威望: 398 点
在线时间: 356(时)
发自: Web Page
来源于&&分类
蓝牙写值发送崩溃了,请问这个是什么原因&&&
之前我在网上找到BabyBluetooth的demo,在demo调用[CBPeripheral writeValue:forCharacteristic:type:]发送成功了,但是今天想发送别的data的时候,崩了。提示这句:***Assertion failure in -[CBPeripheral writeValue:forCharacteristic:type:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreBluetooth/CoreBluetooth-327.1/CBPeripheral.m:386请问各位cocoaChainChian的朋友,这个是什么问题。求教
级别: 新手上路
UID: 495278
可可豆: 15 CB
威望: 16 点
在线时间: 99(时)
发自: Web Page
回 楼主(ZDHeng) 的帖子
解决了吗?我也遇到了同样的问题。求助。
级别: 侠客
UID: 533272
可可豆: 565 CB
威望: 398 点
在线时间: 356(时)
发自: Web Page
回 1楼(葸娟霞) 的帖子
你代码发我看看
级别: 侠客
UID: 533272
可可豆: 565 CB
威望: 398 点
在线时间: 356(时)
发自: Web Page
回 1楼(葸娟霞) 的帖子
我当时发送的时候,是因为我发送的类型不对类型/**&&&&CBCharacteristicWriteWithResponse = 0,&&&&CBCharacteristicWriteWithoutResponse,*/[currPeripheral writeValue:keyData2 forCharacteristic:c1 type:CBCharacteristicWriteWithoutResponse];我这里用的是第二个
级别: 新手上路
可可豆: 17 CB
威望: 17 点
在线时间: 339(时)
发自: Web Page
CBCharacteristicWriteWithResponse = 0,&&&&CBCharacteristicWriteWithoutResponse,这两个没关系和发送没啥关系,只是提示消息而已 你可以检查一下你的C1是否是可写的
关注本帖(如果有新回复会站内信通知您)
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 关注CVP公众号
扫一扫 浏览移动版2014年2月 扩充话题大版内专家分月排行榜第二
本帖子已过去太久远了,不再提供回复功能。在 SegmentFault,解决技术问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
一线的工程师、著名开源项目的作者们,都在这里:
获取验证码
已有账号?
问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
小弟目前在做iOS的蓝牙开发,用CoreBluetooth框架,目前从非iOS外设处接收数据已经没有问题了,但是发送数据一直不成功,外设收不到数据,用lightblue却可以正常收发数据,想问问大家是什么原因。
蓝牙模块的代码如下
#define kServiceUUID @"FFE0"
#define kCharacteristicUUID @"FFE1"
#import "BTManager.h"
@interface BTManager()
@property (strong,nonatomic) NSMutableArray *
//连接的外围设备
@property (nonatomic, strong) CBCharacteristic *writeC
@implementation BTManager
#pragma mark - Public Methods
- (void)writeToPeripheral:(NSString *)dataString {
NSLog(@"writeToPeripheral:%@",dataString);
if(_writeCharacteristic == nil){
NSLog(@"writeCharacteristic 为空");
NSData *value = [self dataWithHexstring:dataString];
NSLog(@"十六进制:%@",value);
[_peripheral writeValue:value forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithResponse];
NSLog(@"已经向外设%@的特征值%@写入数据",_peripheral.name,_writeCharacteristic.description);
- (void)scan{
options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@YES}];
[manager scanForPeripheralsWithServices:nil
options:nil];
- (instancetype)init
self = [super init];
if (self) {
manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
manager.delegate =
#pragma mark - CBCentralManager Delegate
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
NSString * state =
switch ([central state])
case CBCentralManagerStateUnsupported:
state = @"StateUnsupported";
case CBCentralManagerStateUnauthorized:
state = @"StateUnauthorized";
case CBCentralManagerStatePoweredOff:
state = @"PoweredOff";
case CBCentralManagerStatePoweredOn:
state = @"PoweredOn";
case CBCentralManagerStateUnknown:
state = @"unknown";
NSLog(@"手机状态:%@", state);
// 发现外设后
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSString *str = [NSString stringWithFormat:@"发现外设:%@ rssi:%@, UUID:%@ advertisementData: %@ ", peripheral, RSSI, peripheral.identifier.UUIDString, advertisementData];
NSLog(@"%@",str);
[_peripherals addObject:peripheral];
if ([peripheral.name isEqualToString:@"BT"]) {
[manager stopScan];
[manager connectPeripheral:peripheral options:nil];
NSLog(@"连接外设:%@",peripheral.description);
self.peripheral =
// 连接到外设后
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
NSLog(@"已经连接到:%@", peripheral.description);
peripheral.delegate =
[central stopScan];
[peripheral discoverServices:nil];
// 连接失败后
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
// 断开外设
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
#pragma mark - CBPeripheral Delegate
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
NSLog(@"didDiscoverServices");
if (error) {
NSLog(@"搜索服务%@时发生错误:%@", peripheral.name, [error localizedDescription]);
for (CBService *service in peripheral.services) {
//发现服务
if ([service.UUID isEqual:[CBUUID UUIDWithString:kServiceUUID]]) {
NSLog(@"发现服务:%@", service.UUID);
[peripheral discoverCharacteristics:nil forService:service];
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
if (error) {
NSLog(@"搜索特征%@时发生错误:%@", service.UUID, [error localizedDescription]);
NSLog(@"服务:%@",service.UUID);
for (CBCharacteristic *characteristic in service.characteristics) {
NSLog(@"特征:%@",characteristic);
//发现特征
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kCharacteristicUUID]]) {
NSLog(@"监听特征:%@",characteristic);//监听特征
_writeCharacteristic =
[self.peripheral setNotifyValue:YES forCharacteristic:characteristic];
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
if (error)
NSLog(@"更新特征值%@时发生错误:%@", characteristic.UUID, [error localizedDescription]);
// 收到数据
NSLog(@"%@",[self hexadecimalString:characteristic.value]);
#pragma mark - NSData and NSString
//将传入的NSData类型转换成NSString并返回
- (NSString*)hexadecimalString:(NSData *)data{
const unsigned char* dataBuffer = (const unsigned char*)[data bytes];
if(!dataBuffer){
NSUInteger dataLength = [data length];
NSMutableString* hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
for(int i = 0; i & dataL i++){
[hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)dataBuffer[i]]];
result = [NSString stringWithString:hexString];
//将传入的NSString类型转换成NSData并返回
- (NSData*)dataWithHexstring:(NSString *)hexstring{
NSData* aD
return aData = [hexstring dataUsingEncoding: NSASCIIStringEncoding];
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
我是连续发送数据接受数据发生顺序错乱,有遇到这样问题的吗?
分享到微博?
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:iOS BLE 分包发送 - 简书
iOS BLE 分包发送
单次发送的数据过大,蓝牙模块内部接收缓冲区只有20个字节
BLE_SEND_MAX_LEN是蓝牙单次可处理最大字节长度
//分包发送蓝牙数据
-(void)sendMsgWithSubPackage:(NSData*)msgData
Peripheral:(CBPeripheral*)peripheral
Characteristic:(CBCharacteristic*)character
for (int i = 0; i & [msgData length]; i += BLE_SEND_MAX_LEN) {
// 预加 最大包长度,如果依然小于总数据长度,可以取最大包数据大小
if ((i + BLE_SEND_MAX_LEN) & [msgData length]) {
NSString *rangeStr = [NSString stringWithFormat:@"%i,%i", i, BLE_SEND_MAX_LEN];
NSData *subData = [msgData subdataWithRange:NSRangeFromString(rangeStr)];
NSLog(@"%@",subData);
[self writeCharacteristic:peripheral
characteristic:character
value:subData];
//根据接收模块的处理能力做相应延时
usleep(20 * 1000);
NSString *rangeStr = [NSString stringWithFormat:@"%i,%i", i, (int)([msgData length] - i)];
NSData *subData = [msgData subdataWithRange:NSRangeFromString(rangeStr)];
[self writeCharacteristic:peripheral
characteristic:character
value:subData];
usleep(20 * 1000);CC2541发送超长字节内容的处理方式?(500~1000字节) - 蓝牙Bluetooth 技术 - 德州仪器在线技术支持社区
CC2541发送超长字节内容的处理方式?(500~1000字节)
发表于3年前
<input type="hidden" id="hGroupID" value="42"
GATT_Notification 我测试了可以一次发送20个字节,超过20个字节就没有反应,手机端接受不到消息&/p>
&p>GATT_WriteNoRsp 也可以一次发送20个字节,这两个函数有什么区别呢?&/p>
&p>另外在/support/low_power_rf/f/538/t/111465.aspx#394585 提到,快速调用GATT_WriteNoRsp &发送消息,循环几次后就会出现&/p>
&p>MSG_BUFFER_NOT_AVAIL ,那么合理的间隔应该是多少呢?&/p>
&p>烦请建议一下一秒钟内发送500~1000个字节的流程(包括接受)和注意事项,谢谢。&/p>&div style=&clear:&>&/div>" />
CC2541发送超长字节内容的处理方式?(500~1000字节)
此问题尚无答案
All Replies
GATT_Notification 我测试了可以一次发送20个字节,超过20个字节就没有反应,手机端接受不到消息
GATT_WriteNoRsp 也可以一次发送20个字节,这两个函数有什么区别呢?
另外在/support/low_power_rf/f/538/t/111465.aspx#394585 提到,快速调用GATT_WriteNoRsp &发送消息,循环几次后就会出现
MSG_BUFFER_NOT_AVAIL ,那么合理的间隔应该是多少呢?
烦请建议一下一秒钟内发送500~1000个字节的流程(包括接受)和注意事项,谢谢。
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
榜眼39530分
如果是peripheral, 端,那么只能以GATT_Notification&每次发送20个字节。
GATT_WriteNoRsp 是central端的接口, 在central你可以尝试&GATT_WriteLongCharValue()去写超过20个字节每包。
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
就是说peripheral, 端 &要发送500个字节,就只能拆分为20个字节,多次发送,所以对收到内容的完整性校验 要自己处理 对吗?
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
GATT_WriteCharValue() 能否连续调用?是否有次数限制?
我在central端连续发送512字节数据后,速度突然变慢??
(我是每包20字节,连续调用GATT_WriteCharValue() 直至发送完1K字节)
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
这个bStatus_t GATT_WriteCharValue( uint16 connHandle, attWriteReq_t *pReq, uint8 taskId ); &&taskId 是那个 task的 id ?
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
榜眼39530分
可以连续调用。
taskID可以是你的应用层的taskID,比如说SimpleBLECentral 工程,那么就是&simpleBLETaskId。
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
进士3073分
&这个是TI自己测试的数据吞吐量程序,最高可到5.9KB/s ,你可以参考一下,提供了测试的源代码。利用这个程序进行修改,研发进程要快的多。notification 的字节数20 是蓝牙技术联盟规定的BLE 协议,是标准,根本改不了。这不是TI定的
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
HI YAN; 问一个问题: 我用GATT_WriteCharValue往peripheral发送数据, 我看上面很多人说可以一次发送20个字节,但是我测试的结果是: 如果发送20个字节每次,peripheral端会返回错误, 19个字节就ok; & &
下面是抓包结果:
请问是什么原因导致的呢
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
找到原因了
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
举人1285分
你好,可是Notify和Write的最大大小不都是20B吗?
Charisticter定义为大小20B,怎么去写GATT_WriteLongCharValue()去写超过20个字节每包
难道Charisticter可以定位为&20B?
我可以定义Charisticter大小为500,然后直接用GATT_WriteLongCharValue()一次性写500B吗?
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
举人1285分
你好,可是Notify和Write的最大大小不都是20B吗?
Charisticter定义为大小20B,怎么去写GATT_WriteLongCharValue()去写超过20个字节每包
难道Charisticter可以定位为&20B?
我可以定义Charisticter大小为500,然后直接用GATT_WriteLongCharValue()一次性写500B吗?
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
上面说的有问题 最大是251字节 &notification可以是251字节
参考蓝牙协议规范vol6 partB
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
举人1285分
可是似乎是最大20B
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
& 蓝牙协议栈&理论上251b
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
举人1285分
你好&lenli LENLI,
& &非常感谢回复!
& &请问251B是在哪里看到的,能否发给我看一下。
& &不胜感激。
You have posted to a forum that requires a moderator to approve posts before they are publicly available.

我要回帖

更多关于 stm32 can多字节发送 的文章

 

随机推荐