本章使用 simple_peripheral 作为实验平台,创建一个自定义属性的 characteristic 。
simple_peripheral 的 IAR 工程可以在目录C:\ti\simplelink_cc2640r2_sdk_1_35_00_33\examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\tirtos\iar 下找到。
有关工程配置和编译选项以及下载方法参考 CC2640R2 BLE 开发环境搭建部分和使用 IAR 进行开发。
使用USB连接开发板如下图所示:
成功连接开发板之后,打开电脑的设备管理器,有XDS110 的两个端口,BLE Device Monitor 使用 XDS110 Class Application/User UART ( COM25 )这个端口。
注意:XDS110 驱动程序在安装 IAR 时会默认安装,如果你没有安装 IAR ,可以手动更新驱动程序软件。XDS 驱动可以在 CC2640R2 开发工具集介绍里获得。
协议栈和 IAR 安装参考 CC2640R2 BLE 开发环境搭建。本例程需要使用 BLE Device Monitor,有关 BLE Device Monitor 使用介绍请参考:BLE Device Monitor 所有工具可以在 CC2640R2 开发工具集介绍里获得。
有关 BLE Device Monitor 环境搭建以及使用方法请参考: BLE Device Monitor 。
在 simple_perpheral 工程中添加一个 characteristic 6,主要通过修改 simple_gatt_profile.c 和 simple_perpheral.c 两个文件完成。
//source\ti\ble5stack\profiles\simple_profile\simple_gatt_profile.h SIMPLEPROFILE_CHAR6 line 87
#define SIMPLEPROFILE_CHAR6 5 //特征值
#define SIMPLEPROFILE_CHAR6_UUID 0xFFF6 //特征值UUID
#define SIMPLEPROFILE_CHAR6_LEN 5 //特征值长度
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c charValue6 line 618
uint8_t charValue6[SIMPLEPROFILE_CHAR6_LEN] = { 'a', 'b', 'c', 'd', 'e' };
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c SimpleProfile_SetParameter line 629
SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR6, SIMPLEPROFILE_CHAR6_LEN,
charValue6);
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c SimpleProfile_SetParameter line 547
case SIMPLEPROFILE_CHAR6:
if ( len == SIMPLEPROFILE_CHAR6_LEN ) {
VOID memcpy( simpleProfileChar6, value, SIMPLEPROFILE_CHAR6_LEN );
// See if Notification has been enabled
GATTServApp_ProcessCharCfg( simpleProfileChar6Config, simpleProfileChar6, FALSE,
simpleProfileAttrTbl, GATT_NUM_ATTRS( simpleProfileAttrTbl ),
INVALID_TASK_ID, simpleProfile_ReadAttrCB );
} else{
ret = bleInvalidRange;
}break;
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c SimpleProfile_GetParameter line 604
case SIMPLEPROFILE_CHAR6:
VOID memcpy( value, simpleProfileChar6, SIMPLEPROFILE_CHAR6_LEN );
break;
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c simpleProfileChar6Props line 197
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";
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c simpleProfilechar6UUID line 197
// Characteristic 6 UUID: 0xFFF6
CONST uint8 simpleProfilechar6UUID[ATT_BT_UUID_SIZE] ={
LO_UINT16(SIMPLEPROFILE_CHAR6_UUID), HI_UINT16(SIMPLEPROFILE_CHAR6_UUID)
};
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c simpleProfileAttrTbl line 340
// Characteristic 6 Declaration
{ { ATT_BT_UUID_SIZE, characterUUID },
GATT_PERMIT_READ,
0,
&simpleProfileChar6Props },
// Characteristic Value 6
{ { ATT_BT_UUID_SIZE, simpleProfilechar6UUID },
0,
0,
simpleProfileChar6 },
{ { ATT_BT_UUID_SIZE, clientCharCfgUUID },
GATT_PERMIT_READ | GATT_PERMIT_WRITE,
0,
(uint8 *)&simpleProfileChar6Config },
// Characteristic 6 User Description
{ { ATT_BT_UUID_SIZE, charUserDescUUID },
GATT_PERMIT_READ,
0,
simpleProfileChar6UserDesp },
注意:修改 simpleProfileAttrTbl [ SERVAPP_NUM_ATTR_SUPPORTED ]数组大小,SERVAPP_NUM_ATTR_SUPPORTED 定义的是 17 ,由于我们添加了 4 个属性,所以这里需要修改成 21 。
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c SimpleProfile_AddService line 419
simpleProfileChar6Config = (gattCharCfg_t *)ICall_malloc( sizeof(gattCharCfg_t)*linkDBNumConns );
if ( simpleProfileChar6Config == NULL ){
return ( bleMemAllocError );
}
GATTServApp_InitCharCfg( INVALID_CONNHANDLE, simpleProfileChar6Config );
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c simpleProfile_ReadAttrCB line 670
case SIMPLEPROFILE_CHAR6_UUID:
*pLen = SIMPLEPROFILE_CHAR6_LEN;
VOID memcpy( pValue, pAttr->pValue, SIMPLEPROFILE_CHAR6_LEN );
break;
经过上面 7 步就完成了增加通知属性的 characteristic 特征值,我们可以通过按键方式或定时器每秒进行一次通知。本文演示的是使用定时器每秒通知一次characteristic 6 的值,在 simple_peripheral.c 文件里 SimpleBLEPeripheral_performPeriodicTask 函数中增加下面代码:
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c SimpleBLEPeripheral_performPeriodicTask line 1380
uint8_t charValue6[SIMPLEPROFILE_CHAR6_LEN] = { 'a', 'b', 'c', 'd', 'e' };
SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR6, SIMPLEPROFILE_CHAR6_LEN, charValue6);
如下图所示,使用 BLE Device Monitor 就能接收到开发板发送出来的数据,这里显示的 abcde 和我们设置的一样。
文章所有代码、工具、文档开源。加入我们QQ群 591679055获取更多支持,共同研究CC2640R2F&BLE5.0。