这里会显示出您选择的修订版和当前版本之间的差别。
cc2640r2f:notify_characteristic6 [2017/08/31 16:30] 127.0.0.1 外部编辑 |
cc2640r2f:notify_characteristic6 [2021/06/22 23:14] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | < | ||
- | # CC2640增加一个具有通知属性的characteristic # | ||
- | ## 介绍 ## | ||
- | |||
- | 本章使用simple_peripheral作为实验平台。创建一个自定义属性的characteristic。simple_peripheral的IAR工程可以在目录C: | ||
- | 有关工程配置和编译选项以及下载方法参考< | ||
- | |||
- | ## 硬件环境 ## | ||
- | 使用UBS连接我们的开发板。如下图所示 | ||
- | |||
- | ![](http:// | ||
- | |||
- | 成功连接开发板之后,打开电脑的设备管理器。可以看见XDS110的两个端口,BLE Device Monitor将会使用XDS110 Class Application/ | ||
- | > | ||
- | > | ||
- | |||
- | ![](http:// | ||
- | |||
- | ## 软件环境 ## | ||
- | - IAR Embedded workbench 7.80.3 | ||
- | - BLE-STACK V3.0.1 | ||
- | - flash_programmer 2 | ||
- | - BLE Device Monitor | ||
- | |||
- | 协议栈和IAR安装参考< | ||
- | |||
- | ## BLE Device Monitor环境搭建 ## | ||
- | 有关BLE Device Monitor环境搭建以及使用方法请参考:< | ||
- | |||
- | ## 开发步骤 ## | ||
- | 在simple_perpheral工程中添加一个characteristic 6,主要通过修改simple_gatt_profile.c和simple_perpheral.c两个文件完成。 | ||
- | 1. 在simple_gatt_profile.h文件里面添加以下宏定义: | ||
- | ````C | ||
- | // | ||
- | #define SIMPLEPROFILE_CHAR6 | ||
- | #define SIMPLEPROFILE_CHAR6_UUID | ||
- | #define SIMPLEPROFILE_CHAR6_LEN | ||
- | ```` | ||
- | 2. 在simple_peripheral.c文件中设置特征值初始值,添加如下代码: | ||
- | ````C | ||
- | // | ||
- | uint8_t charValue6[SIMPLEPROFILE_CHAR6_LEN] = { ' | ||
- | // | ||
- | SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR6, | ||
- | | ||
- | ```` | ||
- | |||
- | 3. 由于我们调用了SimpleProfile_SetParameter这个函数,该函数作用是设置特征值,当特征值具有通知属性就使用通知发送出来。由于该函数没有characteristic6的处理过程,所以需要我们自己添加,在simple_gatt_profile.c文件中,增加如下代码。 | ||
- | ````C | ||
- | // | ||
- | case SIMPLEPROFILE_CHAR6: | ||
- | if ( len == SIMPLEPROFILE_CHAR6_LEN ) { | ||
- | VOID memcpy( simpleProfileChar6, | ||
- | // See if Notification has been enabled | ||
- | GATTServApp_ProcessCharCfg( simpleProfileChar6Config, | ||
- | simpleProfileAttrTbl, | ||
- | INVALID_TASK_ID, | ||
- | } else{ | ||
- | ret = bleInvalidRange; | ||
- | }break; | ||
- | ```` | ||
- | 4. 同理在SimpleProfile_GetParameter函数中也没有characteristic6的处理过程,在SimpleProfile_GetParameter中增加如下代码。 | ||
- | ````C | ||
- | // | ||
- | case SIMPLEPROFILE_CHAR6: | ||
- | VOID memcpy( value, simpleProfileChar6, | ||
- | break; | ||
- | ```` | ||
- | |||
- | 5. 在simple_gatt_profile.c定义characteristic6属性变量,并在simpleProfileAttrTbl属性数组表中添加。 | ||
- | ````C | ||
- | // | ||
- | static uint8 simpleProfileChar6Props = GATT_PROP_NOTIFY; | ||
- | static gattCharCfg_t *simpleProfileChar6Config; | ||
- | static uint8 simpleProfileChar6[SIMPLEPROFILE_CHAR6_LEN] = { 0, 0, 0, 0, 0 }; | ||
- | static uint8 simpleProfileChar6UserDesp[17] = " | ||
- | |||
- | // | ||
- | // Characteristic 6 UUID: 0xFFF6 | ||
- | CONST uint8 simpleProfilechar6UUID[ATT_BT_UUID_SIZE] ={ | ||
- | LO_UINT16(SIMPLEPROFILE_CHAR6_UUID), | ||
- | }; | ||
- | |||
- | // | ||
- | // Characteristic 6 Declaration | ||
- | { { ATT_BT_UUID_SIZE, | ||
- | GATT_PERMIT_READ, | ||
- | 0, | ||
- | & | ||
- | // Characteristic Value 6 | ||
- | { { ATT_BT_UUID_SIZE, | ||
- | 0, | ||
- | 0, | ||
- | simpleProfileChar6 }, | ||
- | { { ATT_BT_UUID_SIZE, | ||
- | GATT_PERMIT_READ | GATT_PERMIT_WRITE, | ||
- | 0, | ||
- | (uint8 *)& | ||
- | // Characteristic 6 User Description | ||
- | { { ATT_BT_UUID_SIZE, | ||
- | GATT_PERMIT_READ, | ||
- | 0, | ||
- | simpleProfileChar6UserDesp }, | ||
- | ```` | ||
- | > | ||
- | |||
- | 6. 接下来在simple_gatt_profile.c的SimpleProfile_AddService函数中对characteristic6的simpleProfileChar4Config参数分配内存。并初始化配置属性表。 | ||
- | ````C | ||
- | // | ||
- | simpleProfileChar6Config = (gattCharCfg_t *)ICall_malloc( sizeof(gattCharCfg_t)*linkDBNumConns ); | ||
- | if ( simpleProfileChar6Config == NULL ){ | ||
- | return ( bleMemAllocError ); | ||
- | } | ||
- | GATTServApp_InitCharCfg( INVALID_CONNHANDLE, | ||
- | ```` | ||
- | |||
- | 7. 回调改写,走读一下SimpleProfile_SetParameter()这个函数,可以发现,TI的处理过程是每次设置参数后会调用回调读取当前值,然后使用通知发送出来,所以如果要成功的使用通知发送数据,还必须在simpleProfile_ReadAttrCB回调函数中增加characteristic6的代码。在simple_gatt_profile.c文件中simpleProfile_ReadAttrCB函数里添加如下代码: | ||
- | ````C | ||
- | // | ||
- | case SIMPLEPROFILE_CHAR6_UUID: | ||
- | *pLen = SIMPLEPROFILE_CHAR6_LEN; | ||
- | VOID memcpy( pValue, pAttr-> | ||
- | break; | ||
- | ```` | ||
- | |||
- | 经过上面7步就完成了在增加通知属性的characteristic特征值,我们可以通过按键方式或者将就程序现成的定时器来每秒进行一次通知。这里我们演示使用定时器每秒通知一次characteristic6的值。在simple_peripheral.c文件里SimpleBLEPeripheral_performPeriodicTask函数中增加下面代码。 | ||
- | ````C | ||
- | // | ||
- | uint8_t charValue6[SIMPLEPROFILE_CHAR6_LEN] = { ' | ||
- | SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR6, | ||
- | ```` | ||
- | |||
- | 如下图所示,使用BLE Device Monitor就能接收到开发板发送出来的数据,这里显示的abcde和我们设置的一样。 | ||
- | ![](http:// | ||
- | |||
- | ## 加入我们 ## | ||
- | 文章所有代码、工具、文档开源。加入我们[**QQ群 591679055**](http:// | ||
- | <div> | ||
- | <p align=" | ||
- | <a target=" | ||
- | © Copyright 2017, 成都乐控畅联科技有限公司. | ||
- | </p> | ||
- | </ | ||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | </ | ||