CC2642添加自定义特征值Characteristics并实现Notify功能

        CC2642蓝牙芯片内部集成了Cotex-M0和Cotex-M4f ARM内核,是TI支持BLE5.0协议的芯片中性能最强的,不过如今还是预生产阶段,想提前使用只能从官网购买样片。接下来开始在ble5_simple_peripheral_cc26x2r1lp_app工程中添加一个自定义的特征值,并实现Notify通信。

       在现有服务中添加特征值只需要修改simple_gatt_profile.c和simple_gatt_profile.h文件。首先在simple_gatt_profile.h文件中添加自定义的特征值charValue6:

#define SIMPLEPROFILE_CHAR6                   5  //添加特征值char6
#define SIMPLEPROFILE_CHAR6_UUID            0xFFF6 //添加char6 UUID
#define SIMPLEPROFILE_CHAR6_LEN           20  //设置长度

定义charValue6的UUID为0xFFF6,设置CHAR6的数据长度20字节。接下来在simple_gatt_profile.c中定义charValue6的UUID:

// Characteristic 6 UUID: 0xFFF6
CONST uint8 simpleProfilechar6UUID[ATT_BT_UUID_SIZE] =
{
  LO_UINT16(SIMPLEPROFILE_CHAR6_UUID), HI_UINT16(SIMPLEPROFILE_CHAR6_UUID)
};

然后定义特征值参数,初始化数组:

// Simple Profile Characteristic 6 Properties 具有可读,可写,可notify功能
static uint8 simpleProfileChar6Props = GATT_PROP_READ | GATT_PROP_WRITE | GATT_PROP_NOTIFY;

// Characteristic 6 Value
static uint8 simpleProfileChar6[SIMPLEPROFILE_CHAR6_LEN] = { 0 };

// Simple Profile Characteristic 6 Configuration Each client has its own
// instantiation of the Client Characteristic Configuration. Reads of the
// Client Characteristic Configuration only shows the configuration for
// that client and writes only affect the configuration of that client.
static gattCharCfg_t *simpleProfileChar6Config;

// Simple Profile Characteristic 6 User Description
static uint8 simpleProfileChar6UserDesp[17] = "Characteristic 6";

 使用Notify功能要定义*simpleProfileChar6Config,如果不使用Notify功能可不定义。接下来在特征值属性表simpleProfileAttrTbl中添加自定义特征值的定义:

 // 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
        }

添加完特征值属性表以后,要修改属性表的大小,定义在文件的最前面,从17改为21:

#define SERVAPP_NUM_ATTR_SUPPORTED        21

接下来在SimpleProfile_AddService函数中为simpleProfileChar6Config分配内存空间:

  simpleProfileChar6Config = (gattCharCfg_t *)ICall_malloc( sizeof(gattCharCfg_t)*linkDBNumConns );

  if ( simpleProfileChar6Config == NULL ){
  return ( bleMemAllocError );
  }

  GATTServApp_InitCharCfg( LINKDB_CONNHANDLE_INVALID, simpleProfileChar6Config );

SimpleProfile_SetParameter函数用于设置特征值value,需要在其中添加新增的特征值:

    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;

SimpleProfile_GetParameter函数用于获得特征值value,在其中添加自定义的特征值:

    case SIMPLEPROFILE_CHAR6:
      VOID memcpy( value, simpleProfileChar6, SIMPLEPROFILE_CHAR6_LEN );
      break;

simpleProfile_ReadAttrCB函数用于读特征值:

      case SIMPLEPROFILE_CHAR6_UUID:
        *pLen = SIMPLEPROFILE_CHAR6_LEN;
        VOID memcpy( pValue, pAttr->pValue, SIMPLEPROFILE_CHAR6_LEN );
        break;

        simpleProfile_WriteAttrCB是特征值写回调函数,对于实现Notify功能不需要添加,如果想实现写特征值功能,需要增加自定义特征值部分。至此,一个支持Notify功能的特征值就添加好了。为了验证功能实现,在simple_peripheral.c的SimplePeripheral_performPeriodicTask周期函数中周期性的像手机端APP发送数据:

        本文实现的是每隔100ms发送一次六轴传感器采集到的数据,在手机端使用lightblue APP可以实时查看到传感器数据,如下图所示:

   

5个重要的函数作用辨析:

simpleProfile_WriteAttrCB 和simpleProfile_ReadAttrCB  函数:APP端读写属性表中的特征值时,触发;

simpleProfile_GetParameter和simpleProfile_GetParameter函数:device端读写属性表中的特征值时,触发;

pfnSimpleProfileChange回调函数:只要属性表中特征值改变,该函数就会被调用;

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值