欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

BLE如何发送超过一包为20个字节的问题

程序员文章站 2024-03-22 21:38:46
...

大家都知道蓝牙 BLE 4.0发送数据时都是 20 字节一个包,协议规定,payload 最大 27。在协议第六章中的 2.4,刨去 L2CAP 的头,4 个字节,剩下的就 23 个字节 MTU。就是你看到的。ATT 层会用掉上 1 个字节的 op code, 2 个字节的 attribute handle,就剩下 20了。这剩下的 20 字节就是我们常说的发送的 20 字节的数据。

BLE5.0

数据长度扩展
总结
数据长度的扩展功能允许LE控制器发送数据信道的分组数据单元(PDU)高达251字节的数据应用负载,而在连接状态。此外,一个新的PDU的大小是可以协商的任何一方在任何时间在一个连接。
以前,控制器最大的数据通道有效载荷为27字节。相比蓝牙核心规范版本4和4.1设备(如果两个设备支持扩展的包长度并正确配置),这将提高数据速率约2.5倍。

LE Data Length Extension

Summary

The data length extension feature allows the LE controller to send data channel packet data units (PDUs) with payloads of up to 251 bytes of application data, while in the connected state. Furthermore, a new PDU size can be negotiated by either side at any time during a connection.

Previously, the controller’s largest data channel payload was 27 bytes. This increases the data rate by around 2.5*, compared to Bluetooth Core Specification Versions 4.0 and 4.1 devices (if both devices support extended packet length and are configured properly).

在CC2640R2F中如何实现:

BLE如何发送超过一包为20个字节的问题

这是BLE4.1 4.2的PDU部分:

#define APP_TX_PDU_SIZE 27
#define APP_RX_PDU_SIZE 27
#define APP_TX_TIME 328
#define APP_RX_TIME 328

//This API is documented in hci.h
HCI_EXT_SetMaxDataLenCmd(APP_TX_PDU_SIZE ,  APP_TX_TIME,
   APP_RX_PDU_SIZE, APP_RX_TIME);
我们要修改为:

#define APP_SUGGESTED_PDU_SIZE 251
#define APP_SUGGESTED_TX_TIME 2120

//This API is documented in hci.h
HCI_LE_WriteSuggestedDefaultDataLenCmd(APP_SUGGESTED_PDU_SIZE ,
APP_SUGGESTED_TX_TIME)

在初始化中:

uint16_t cxnHandle; //Request max supported size
uint16_t requestedPDUSize = 251;
uint16_t requestedTxTime = 2120;

GAPRole_GetParameter(GAPROLE_CONNHANDLE, &cxnHandle); //This API is documented in hci.h

if (SUCCESS != HCI_LE_SetDataLenCmd(cxnHandle, requestedPDUSize, requestedTxTime))
   DISPLAY_WRITE_STRING("Data length update failed", LCD_PAGE0);

Youneed to set the MAX_PDU_SIZE 251 and adjust the heap size accordingly so thatyou can send packets that are > 20 bytes data. 

参考:http://dev.ti.com/tirex/content/simplelink_cc2640r2_sdk_1_35_00_33/docs/ble5stack/ble_user_guide/html/ble-stack/data-length-extensions.html#sec-utilizing-data-length-ext-at-run-time

相关标签: 蓝牙