CC2640R2F 添加notify 接口


-----------------------------------------------------------------------------------simple_gatt_profile.c

uint16_t GATTServApp_FindCharhandle( gattAttribute_t *pAttrTbl,
                                       uint16 numAttrs, uint16 uuid )
{
  uint16  i;
  uint8 charuuid[ATT_BT_UUID_SIZE] =
  {
    LO_UINT16(uuid), HI_UINT16(uuid)
  };    
    
  for ( i = 0; i < numAttrs; i++ )
  {
    if(memcmp(pAttrTbl[i].type.uuid,charuuid,sizeof(charuuid)) == 0)
    {
      return ( pAttrTbl[i].handle );
    }
  }


  return ( 0 );
}


bStatus_t ServApp_SendNotiInd( uint8_t *nvalue,uint16_t nlen,uint16_t char_uuid)
{
  attHandleValueNoti_t noti;
  uint16_t slen;
  bStatus_t status = FAILURE;
  slen  = nlen;
  
  uint16 notify_Handle;  
  
  GAPRole_GetParameter(GAPROLE_CONNHANDLE,&notify_Handle); //获取当前连接的handle


  if  ( notify_Handle != INVALID_CONNHANDLE )
  {
      noti.pValue = (uint8 *)GATT_bm_alloc( notify_Handle, ATT_HANDLE_VALUE_NOTI,slen,&slen);
      
      if ( noti.pValue != NULL )               
      {
          noti.handle =  GATTServApp_FindCharhandle(simpleProfileAttrTbl,GATT_NUM_ATTRS( simpleProfileAttrTbl ),char_uuid);
          noti.len = slen;
          memcpy(noti.pValue, nvalue, slen);
          
          if(noti.handle != 0)
          {
              status = GATT_Notification( notify_Handle, &noti, FALSE );
          }


        if ( status != SUCCESS )
        {
          GATT_bm_free( (gattMsg_t *)&noti, ATT_HANDLE_VALUE_NOTI );
        }
      }
      else
      {
        GATT_bm_free( (gattMsg_t *)&noti, ATT_HANDLE_VALUE_NOTI );
        status = bleNoResources;
      }
  }


  return ( status );
}
-----------------------------------------------------------------------------------simple_gatt_profile.h

extern bStatus_t CoolBan_SendNotiInd_Char4( uint8 *nvalue,uint8 nlen);   // add


-----------------------------------------------------------------------------------simple_peripheral.

static void SimpleBLEPeripheral_performPeriodicTask(void)    // event= SBP_PERIODIC_EVT     period= SBP_PERIODIC_EVT_PERIOD 
{
  bStatus_t ret;
  static uint8 Txbuf[300]= {0};
  static uint8 TxLen = 10;
  
  uint8 i = 0;
  for(i = 0; i < 0xFF ;i++){
    Txbuf[i] = i;
  }
  
  ret =  CoolBan_SendNotiInd_Char4( Txbuf,TxLen);
  if(ret == SUCCESS){
  }else{
  }
}






### CC2340 Notify 功能与实现 CC2340 是德州仪器(TI)推出的低功耗蓝牙(BLE)SoC,广泛应用于物联网设备中。Notify 功能是 BLE 通信中的重要组成部分,用于从服务器端(通常是外围设备)向客户端(通常是中央设备)发送数据更新的通知。 #### 1. Notify 的基本概念 在 BLE 协议中,Notify 是一种单向通信机制,允许服务器将数据变化通知给订阅的客户端。当某个特性(Characteristic)的值发生变化时,服务器可以通过 Notify 将新值发送给已订阅该特性的客户端。这种机制不需要客户端主动请求数据,从而降低了功耗和延迟[^3]。 #### 2. CC2340 中的 Notify 实现 在 CC2340 平台上实现 Notify 功能,通常需要以下步骤: - **配置 GATT 服务和特性** 在应用层定义一个包含 Notify 特性的 GATT 服务。例如,使用 TI 提供的 `SimpleGATTProfile` 示例代码来创建自定义服务和特性[^4]。 ```c static uint8_t simpleProfile characteristicValue = 0; static uint8_t simpleProfile characteristicProperties = GATT_PROP_NOTIFY; static gattAttribute_t simpleProfile_attributes[] = { { { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* UUID */ GATT_PERMIT_READ, /* Permissions */ 0, /* Handle */ (uint8_t *)&simpleProfile_service /* Value */ }, { { ATT_BT_UUID_SIZE, characterUUID }, /* UUID */ GATT_PERMIT_READ, /* Permissions */ 0, /* Handle */ &simpleProfile_characteristicProperties /* Value */ }, { { ATT_BT_UUID_SIZE, clientCharCfgUUID }, /* UUID */ GATT_PERMIT_READ | GATT_PERMIT_WRITE, /* Permissions */ 0, /* Handle */ NULL /* Value */ }, { { ATT_BT_UUID_SIZE, customUUID }, /* UUID */ GATT_PERMIT_READ, /* Permissions */ 0, /* Handle */ &simpleProfile_characteristicValue /* Value */ } }; ``` - **启用 Notify** 当客户端订阅了特定的特性后,服务器可以调用 API 函数发送通知。例如,在 TI 的 SimplePeripheral 示例中,使用 `GATT_Notification` 函数实现 Notify 功能[^5]。 ```c void SimpleProfile_Notify(uint16_t connHandle) { attHandleValueNoti_t noti; noti.pValue = &simpleProfile_characteristicValue; noti.len = sizeof(simpleProfile_characteristicValue); noti.handle = simpleProfile_handles[IDX_CHAR_VAL]; GAPRole_EstablishLink(connHandle, &noti); } ``` - **处理客户端订阅** 当客户端通过写入 Client Characteristic Configuration Descriptor(CCCD)来订阅或取消订阅特性时,服务器需要监听 CCCD 的变化并调整其行为。如果 CCCD 值为 `0x0001`,表示客户端已订阅 Notify;如果为 `0x0000`,则表示取消订阅[^6]。 #### 3. 注意事项 - **功耗优化** Notify 功能虽然高效,但在设计时仍需考虑功耗问题。确保只在必要时发送通知,并合理设置广播间隔以延长电池寿命[^7]。 - **安全性** 如果数据敏感,建议启用加密连接,防止未经授权的设备接收通知数据[^8]。 #### 4. 示例代码 以下是一个完整的 Notify 实现示例: ```c #include <ti/devices/cc23x0/inc/hw_types.h> #include <ti/drivers/GATT/GATT.h> void SimpleProfile_Init(void) { // 初始化 GATT 属性表 GATT_AddService(&simpleProfile_service); } void SimpleProfile_SetParameter(uint8_t param, uint8_t *value) { if (param == SIMPLE_PROFILE_CHAR_VALUE) { GATT_SetParameter(simpleProfile_handles[IDX_CHAR_VAL], sizeof(simpleProfile_characteristicValue), value); } } void SimpleProfile_Notify(uint16_t connHandle) { attHandleValueNoti_t noti; noti.pValue = &simpleProfile_characteristicValue; noti.len = sizeof(simpleProfile_characteristicValue); noti.handle = simpleProfile_handles[IDX_CHAR_VAL]; GAPRole_EstablishLink(connHandle, &noti); } ``` ###
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值