1. 程式人生 > >CC2640之新增一個自定義的特性值

CC2640之新增一個自定義的特性值

測試環境

協議棧版本:BLE-STACK V2.1

IAR開發環境版本:IAR for Arm 7.40

硬體裝置:Amo-SmartRF v2.0 開發板(對應TI官方的SmartRF06EB 開發板)

示例測試Demo工程:simpleBLEPeripheral工程

新增自定義特徵值

我們今天一起來了解下,如何在一個現有的profile中新增一個自定義的特徵值,而且該特徵值具有讀、寫和通知許可權。下面的講解中,我們以simpleBLEPeripheral工程為例,來了解如何在其現有的profile中,新增一個具有讀、寫和通知功能的特徵值char6。

首先,我們先了解下

simpleBLEPeripheral工程原有的服務和特徵值,該工程本身有4個服務,其中Simple Profile Service服務是我們可以新增自定義特徵值的,該服務本身有5個特徵值,UUID分別為FFF1,FFF2,FFF3,FFF4,FFF5,下面我們來實際看一下如何新增一個特徵值char6,UUID為FFF6。

1.在C:\ti\simplelink\ble_cc26xx_2_01_00_44423\Projects\ble\Profiles\SimpleProfile目錄下找到simpleGATTprofile.h檔案,我們先在這個檔案中定義我們要新增的新的特徵值的一些宣告:

  1. #define SIMPLEPROFILE_CHAR6              5  // RW uint8 - Profile Characteristic 6 value
  2. #define SIMPLEPROFILE_CHAR6_UUID            0xFFF6
  3. // Length of Characteristic 6 in bytes
  4. #define SIMPLEPROFILE_CHAR6_LEN           19  
  5. // Position of simple char6 value in attribute array
  6. // char6特性在屬性表中的位置,根據實際情況而定,協議棧中的demo新增之後分別是18,19
  7. #define SIMPLE_MEAS6_VALUE_POS            18
  8. #define SIMPLE_MEAS6_CCC_POS            19

2.在C:\ti\simplelink\ble_cc26xx_2_01_00_44423\Projects\ble\Profiles\SimpleProfile\CC26xx目錄下找到simpleGATTprofile.c檔案。

(1)在該檔案全域性變數宣告UUID的地方,宣告我們新新增的特徵值的UUID:

  1. // Characteristic 6 UUID: 0xFFF6
  2. CONST uint8 simpleProfilechar6UUID[ATT_BT_UUID_SIZE] =  
  3. {   
  4.     LO_UINT16(SIMPLEPROFILE_CHAR6_UUID), HI_UINT16(SIMPLEPROFILE_CHAR6_UUID)  
  5. };  

(2)在該檔案內部變數宣告的地方,宣告一下我們要新增的新特徵值char6的相關變數:

  1. // Simple Profile Characteristic 6 Properties
  2. static uint8 simpleProfileChar6Props = GATT_PROP_READ | GATT_PROP_WRITE_NO_RSP | GATT_PROP_NOTIFY;  
  3. // Characteristic 6 Value
  4. static uint8 simpleProfileChar6[SIMPLEPROFILE_CHAR6_LEN] = { 0, 0, 0, 0, 0 };  
  5. static uint8 simpleProfileChar6Len = 0;  
  6. // Simple Profile Characteristic 6 Configuration Each client has its own
  7. // instantiation of the Client Characteristic Configuration. Reads of the
  8. // Client Characteristic Configuration only shows the configuration for
  9. // that client and writes only affect the configuration of that client.
  10. static gattCharCfg_t *simpleProfileChar6Config;  
  11. // Simple Profile Characteristic 6 User Description
  12. static uint8 simpleProfileChar6UserDesp[17] = "Characteristic 6";  

(3)在該Profile的屬性表

static gattAttribute_t simpleProfileAttrTbl[SERVAPP_NUM_ATTR_SUPPORTED] =

中新增char6相關配置,新增的程式碼如下:

  1. // 17 Characteristic 6 Declaration
  2. {   
  3.     { ATT_BT_UUID_SIZE, characterUUID },  
  4.     GATT_PERMIT_READ,   
  5.     0,  
  6.     &simpleProfileChar6Props   
  7. },  
  8. // 18 Characteristic Value 6
  9. {   
  10.     { ATT_BT_UUID_SIZE, simpleProfilechar6UUID },  
  11.     GATT_PERMIT_READ | GATT_PERMIT_WRITE,  
  12.     0,   
  13.     simpleProfileChar6   
  14. },  
  15. // 19 Characteristic 6 configuration
  16. {   
  17.     { ATT_BT_UUID_SIZE, clientCharCfgUUID },  
  18.     GATT_PERMIT_READ | GATT_PERMIT_WRITE,   
  19.     0,   
  20.     (uint8 *)&simpleProfileChar6Config   
  21. },  
  22. // 20 Characteristic 6 User Description
  23. {   
  24.     { ATT_BT_UUID_SIZE, charUserDescUUID },  
  25.     GATT_PERMIT_READ,   
  26.     0,   
  27.     simpleProfileChar6UserDesp   
  28. },    

上面程式碼註釋上的標號,是我自己標的,也就是屬性表陣列的下標,注意是從0開始,上述註釋的下標就跟我之前標頭檔案中定義的SIMPLE_MEAS6_VALUE_POS和SIMPLE_MEAS6_CCC_POS的值相對應。

因為增加了這4個數組成員,所以屬性表SERVAPP_NUM_ATTR_SUPPORTED的值需要由原來的17修改為21,如下:

  1. /********************************************************************* 
  2. * CONSTANTS 
  3. */
  4. #define SERVAPP_NUM_ATTR_SUPPORTED        21//17

(4)新新增的特徵值char6具有通知功能,所以,我們需要在SimpleProfile_AddService函式中進行相應的初始化配置操作:

  1. // Allocate Client Characteristic Configuration table
  2. simpleProfileChar6Config = (gattCharCfg_t *)ICall_malloc( sizeof(gattCharCfg_t) *linkDBNumConns );  
  3. if ( simpleProfileChar6Config == NULL )  
  4. {  
  5.     // Free already allocated data
  6.     ICall_free( simpleProfileChar6Config );  
  7.     return ( bleMemAllocError );  
  8. }  
  9. ......  
  10. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, simpleProfileChar6Config );  

(5)SimpleProfile_SetParameter方法中參考其他特徵值的配置,新增如下程式碼:

  1. case SIMPLEPROFILE_CHAR6:  
  2.     if ( len <= SIMPLEPROFILE_CHAR6_LEN )   
  3.     {  
  4.         VOID memcpy( simpleProfileChar6, value, len );  
  5.         simpleProfileChar6Len = len;  
  6.         // See if Notification has been enabled
  7.         GATTServApp_ProcessCharCfg( simpleProfileChar6Config, simpleProfileChar6, FALSE,  
  8.                         simpleProfileAttrTbl, GATT_NUM_ATTRS( simpleProfileAttrTbl ),  
  9.                         INVALID_TASK_ID, simpleProfile_ReadAttrCB );  
  10.     }  
  11.     else
  12.     {  
  13.         ret = bleInvalidRange;  
  14.     }  
  15.     break;  

(6)SimpleProfile_GetParameter方法中參考其他特徵值的配置,新增新特徵值的相關配置:

  1. case SIMPLEPROFILE_CHAR6:  
  2.     VOID memcpy( value, simpleProfileChar6, simpleProfileChar6Len );  
  3.     break;    

(7)simpleProfile_ReadAttrCB函式是呼叫讀之後的回撥函式,在其中新增新特徵值的相應處理:

  1. case SIMPLEPROFILE_CHAR6_UUID:  
  2.     *pLen = SIMPLEPROFILE_CHAR6_LEN;  
  3.     VOID memcpy( pValue, pAttr->pValue, SIMPLEPROFILE_CHAR6_LEN );  
  4.     break;  

(8)simpleProfile_WriteAttrCB函式是呼叫寫操作之後的回撥函式,在其中新增新特徵值的相應處理:

  1. case SIMPLEPROFILE_CHAR6_UUID:  
  2.     if ( offset == 0 )  
  3.     {  
  4.         if ( len > SIMPLEPROFILE_CHAR6_LEN )  
  5.         {  
  6.             status = ATT_ERR_INVALID_VALUE_SIZE;  
  7.         }  
  8.     }  
  9.     else
  10.     {  
  11.         status = ATT_ERR_ATTR_NOT_LONG;  
  12.     }  
  13.     //Write the value
  14.     if ( status == SUCCESS )  
  15.     {  
  16.         VOID memcpy( pAttr->pValue, pValue, SIMPLEPROFILE_CHAR6_LEN );  
  17.         //VOID memcpy( pAttr->pValue, pValue, len );
  18.         //simpleProfileChar6Len = len;
  19.         notifyApp = SIMPLEPROFILE_CHAR6;  
  20.         //tx_printf("pValue:%s",pAttr->pValue);
  21.     }  
  22.     break;  

(9)我們在該檔案中封裝一個通過通知功能傳送資料的介面函式,原始碼如下:

  1. /********************************************************************* 
  2. * @fn          SimpleProfile_Notification 
  3. * 
  4. * @brief       Send a notification containing a ir value 
  5. * 
  6. * @param       connHandle - connection handle 
  7. * @param       pNoti - pointer to notification structure 
  8. * 
  9. * @return      Success or Failure 
  10. */
  11. bStatus_t SimpleProfile_Notification( uint16 connHandle, attHandleValueNoti_t *pNoti )  
  12. {  
  13.     uint16 value = GATTServApp_ReadCharCfg( connHandle, simpleProfileChar6Config );  
  14.     // If notifications is enabled
  15.     if ( value & GATT_CLIENT_CFG_NOTIFY )  
  16.     {  
  17.         // Set the handle
  18.         pNoti->handle = simpleProfileAttrTbl[SIMPLE_MEAS6_VALUE_POS].handle;  
  19.         // Send the notification
  20.         return GATT_Notification( connHandle, pNoti, FALSE );  
  21.     }  
  22.     return bleIncorrectMode;  
  23. }  

開啟C:\ti\simplelink\ble_cc26xx_2_01_00_44423\Projects\ble\Profiles\SimpleProfile目錄下的simpleGATTprofile.h檔案,在其中新增該通知功能介面的宣告,便於外部呼叫:

  1. /********************************************************************* 
  2. * @fn          SimpleProfile_Notification 
  3. * 
  4. * @brief       Send a notification containing a ir value 
  5. * 
  6. * @param       connHandle - connection handle 
  7. * @param       pNoti - pointer to notification structure 
  8. * 
  9. * @return      Success or Failure 
  10. */
  11. extern bStatus_t SimpleProfile_Notification( uint16 connHandle, attHandleValueNoti_t *pNoti );  

3.經過上面的配置,我們就在Profile中添加了自定義的特徵值char6,下面我們在應用檔案中新增測試程式,進行相應測試。

(1)定義兩個內部變數,通知功能使用的變數以及連線控制代碼的變數:

  1. 相關推薦

    CC2640新增一個定義特性

    測試環境 協議棧版本:BLE-STACK V2.1 IAR開發環境版本:IAR for Arm 7.40 硬體裝置:Amo-SmartRF v2.0 開發板(對應TI官方的SmartRF06EB 開發板) 示例測試Demo工程:simpleBLEPeriphera

    C# 直接建立一個DataTable,併為新增資料(定義DataTable)

    DataTable dt=new DataTable("cart"); DataColumn dc1=new DataColumn("prizename",Type.GetType("System.String")); DataColumn dc2=new Da

    Pixhawk---通過串列埠方式新增一個定義感測器(超聲波為例)

    Pixhawk—新增一個自定義感測器—超聲波(串列埠方式) 1 說明   首先超聲波模組是通過串列埠方式傳送(Tx)出資料,使用的模組資料傳送週期為100ms,資料格式為: R0034 R0122 R0122 R0046 R0127 R0044 R00

    C#定義特性

    創建 tip comm 字段 運算符 包含 自動 名稱 程序   在前面介紹的代碼中有使用特性,這些特性都是Microsoft定義好的,作為.NET Framework類庫的一部分,許多特性都得到了C#編譯器的支持。   .NET Frmework也允許定義自己的特性。自

    Android下新增新的定義和按鍵處理流程

    [cpp] view plain copy print? <span style="font-family:FangSong_GB2312;font-size:18px;">/*  * Copyright (C) 2010 The Android Open So

    Openstack元件部署 — 將一個定義 Service 新增到 Keystone

    目錄 Keystone 認證流程 User 使用憑證(username/password) 到 keystone 驗證並獲得一個臨時的 Token 和 Generic catalog(全域性目錄),臨時的 Token 會儲存在 keystone-

    .NET(C#):獲取方法返回定義特性(Attribute)

    .NET中特性的索取就是圍繞著ICustomAttributeProvider介面(System.Reflection名稱空間內),而MethodInfo類的ReturnTypeCustomAttributes屬性直接返回方法返回值的ICustomAttributeProvider介面物件。同時Method

    Python+Selenium中級篇8-Python定義封裝一個簡單的Log類

           本文介紹如何寫一個Python日誌類,用來輸出不同級別的日誌資訊到本地資料夾下的日誌檔案裡。為什麼需要日誌輸出呢,我們需要記錄我們測試指令碼到底做了什麼事情,最好的辦法是寫事件監聽。這個

    C++筆記為什麼一個定義了解構函式就幾乎肯定要定義拷貝建構函式和拷貝賦運算子

    這個問題本來很簡單,但是時間久了就容易忘,所以做個筆記用來提示下自己 先來看看這樣一個類: class HasPtr { public: HasPtr(const string& s = string()) :ps(new string(s)), i(0) {

    一個ros包下怎麽使用另外一個定義ros包裏的message

    com doc pack .cn .html docs ssa ace hit 假設自定義消息包my_message_package https://answers.ros.org/question/206257/catkin-use-ros-message-from-an

    反射,定義特性,對象復制

    str pan 特性 復制 des sage tel write getprop [AttributeUsage(AttributeTargets.All)] public class VersionAttribute : Attribute {

    創建一個定義比較器

    rac void ger 接口 table string pre ride com 雙列集合: -------------| Map 如果是實現了Map接口的集合類,具備的特點: 存儲的數據都是以鍵值對的形式存在的,鍵不可重復,值可以重復。 ---------------

    Vue徹底理解定義組件的v-model

    自動 value tro 需要 this 變量 mode type 自定義 最近在學習vue,今天看到自定義事件的表單輸入組件,糾結了一會會然後恍然大悟...官方教程寫得不是很詳細,所以我決定總結一下。 v-model語法糖 v-model實現了表單輸入的雙向綁定,我們

    python路_定義forms組件

    char deepcopy clean copy 所有 ges div object 失敗 import re import copy class ValidateError(Exception): def __init__(self,detail):

    定義特性與應用

    TP stat target 一個 計算 如果 得到 for https   自定義特性允許把自定義元數據與程序元素關聯起來。在.NET Framework框架中,微軟定義了許多特性提供給開發人員使用,如StructLayout特性中的信息在內存中布置結構。這些已有的特性得

    NPOI+反射+定義特性實現上傳excel轉List及驗證

    type set custom pre script private property xssf don 1.自定義特性 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited

    關於jQuery獲取html標簽定義屬性或data

    自定義屬性 標簽 定義 .data 獲取 div val data 屬性 //獲取屬性值<div id="id1" value="優秀" ></div>jQuery取值:$("#id1").attr("value"); //獲取自定義屬性值&l

    Flutter:教你用CustomPaint畫一個定義的CircleProgressBar

      注意:這其實是一篇CustomPaint的使用教程!! 原始碼地址:github.com/yumi0629/Fl…   在Flutter中,CustomPaint就像是Android中的Paint一樣,可以用它繪製出各種各樣的自定義圖形。確實,Paint的使用比較複雜,我覺得直接講API的話也太無聊了

    Xamarin定義佈局系列——ListView的一個定義實現ItemsControl(橫向列表)

    原文: Xamarin自定義佈局系列——ListView的一個自定義實現ItemsControl(橫向列表) 在以前寫UWP程式的時候,瞭解到在ListView或者ListBox這類的列表空間中,有一個叫做ItemsPannel的屬性,它是所有列表中子元素實際的容器,如果要讓列表進行橫向排列,只需要在Xam

    如何安裝和配置打印服務器六:定義客戶端電腦使用網絡打印機的默認設置

    oss strong mode 客戶端 pre 51cto col str 裝配 如何安裝和配置打印服務器之六:自定義客戶端電腦使用網絡打印機的默認設置 ?Lander Zhang 專註外企按需IT基礎架構運維服務,IT Helpdesk 實戰培訓踐行者http://blo