在iOS上通过BTLE发送数据时的最大数据大小 [英] Maximum data size when sending data via BTLE on iOS

查看:206
本文介绍了在iOS上通过BTLE发送数据时的最大数据大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在实施一个使用CoreBluetooth在两台设备之间传输数据的iOS应用程序。例如,要将数据从中心发送到外设,我使用以下代码:

I am currently implementing an iOS application that uses CoreBluetooth to transfer data between 2 devices. For example, to send data from the central to the peripheral, I use this code:

NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
NSLog(@"Writing data of length %d", [data length]);
[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

这个工作绝对正常,但问题是我现在多次阅读特征可以转移一个一次最多20个字节。另外,当我查看 Apple的BTLE示例代码时,他们实现一种机制,将数据分成20个字节的块。但是上面的代码打印的内容类似于写入长度为124的数据 - 但是数据在其他设备上很好。

This works absolutely fine, but the thing is that I have read multiple times now that characteristics can transfer a maximum of 20 bytes at once. Also, when I look at Apple's BTLE Example Code they implement a mechanism that splits the data into chunks of 20 bytes. But the above code prints something like Writing data of length 124 - the data arrives fine at the other device, though.

所以:为什么这个有效?更重要的是,我担心这会在某些时候破裂,特别是当非iOS设备发挥作用时。

推荐答案

BLE标准要求23个字节作为所有BLE设备必须支持的最小ATT_MTU(属性协议最大传输单元)。然而,最大ATT_MTU是255字节,并且对于BLE 4.2再次加倍。

The BLE standard requires 23 bytes as the minimum ATT_MTU (Attribute Protocol Maximum Transmission Unit) which all BLE devices must support. The maximum ATT_MTU is 255 bytes, however, and has been doubled again for BLE 4.2.

BLUETOOTH规范版本4.2 [第3卷,A部分]:

BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part A]:


所有L2CAP实现应支持LE-U逻辑链路上23个八位字节的最小MTU;但是,某些协议和配置文件明确要求支持
更大的MTU。

All L2CAP implementations shall support a minimum MTU of […] 23 octets over the LE-U logical link; however, some protocols and profiles explicitly require support for a larger MTU.

建立连接时,两个设备将交换其ATT_MTU大小,并且使用较小的两个值。当Apple开始使用BLE时,他们只支持最低限度,但后来扩大了可能的大小。这就是你的124字节工作的原因,但是较旧的文档和示例代码使用了更小的ATT_MTU。

When establishing a connection, both devices will exchange their ATT_MTU size, and the smaller of both values is used. When Apple started with BLE, they would only support the minimum, but have since expanded the possible size. That is why your 124 bytes work, but the older documentation and sample code uses a much smaller ATT_MTU.

这篇关于在iOS上通过BTLE发送数据时的最大数据大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆