这里会显示出您选择的修订版和当前版本之间的差别。
cc2640r2f:notify_characteristic6 [2017/09/02 12:15] long |
cc2640r2f:notify_characteristic6 [2021/06/22 23:14] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | < | ||
- | |||
- | # CC2640增加一个具有通知属性的 characteristic # | ||
- | |||
- | ## 介绍 ## | ||
- | |||
- | 本章使用 simple_peripheral 作为实验平台,创建一个自定义属性的 characteristic 。 | ||
- | simple_peripheral 的 IAR 工程可以在目录C: | ||
- | 有关工程配置和编译选项以及下载方法参考< | ||
- | |||
- | ## 硬件环境 ## | ||
- | |||
- | 使用USB连接开发板如下图所示: | ||
- | |||
- | ![](http:// | ||
- | |||
- | 成功连接开发板之后,打开电脑的设备管理器,有XDS110 的两个端口,BLE Device Monitor 使用 XDS110 Class Application/ | ||
- | |||
- | > | ||
- | > XDS110 驱动程序在安装 IAR 时会默认安装,如果你没有安装 IAR ,可以手动更新驱动程序软件。XDS 驱动可以在< | ||
- | |||
- | ![](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 这个函数作用是设置特征值,当特征值具有通知属性就使用通知发送出来。由于该函数没有 characteristic 6 的处理过程,所以需要我们自己添加,在 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 函数中也没有 characteristic 6 的处理过程,在 SimpleProfile_GetParameter 中增加如下代码: | ||
- | |||
- | ````C | ||
- | // | ||
- | case SIMPLEPROFILE_CHAR6: | ||
- | VOID memcpy( value, simpleProfileChar6, | ||
- | break; | ||
- | ```` | ||
- | |||
- | 5. 在 simple_gatt_profile.c 定义 characteristic 6 属性变量,并在 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 函数中对 characteristic 6 的 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 回调函数中增加 characteristic 6 的代码。 | ||
- | 在 simple_gatt_profile.c 文件中 simpleProfile_ReadAttrCB 函数里添加如下代码: | ||
- | |||
- | ````C | ||
- | // | ||
- | case SIMPLEPROFILE_CHAR6_UUID: | ||
- | *pLen = SIMPLEPROFILE_CHAR6_LEN; | ||
- | VOID memcpy( pValue, pAttr-> | ||
- | break; | ||
- | ```` | ||
- | |||
- | 经过上面 7 步就完成了增加通知属性的 characteristic 特征值,我们可以通过按键方式或定时器每秒进行一次通知。本文演示的是使用定时器每秒通知一次characteristic 6 的值,在 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> | ||
- | </ | ||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | </ | ||