nRF52832 GATT 自定義Service/Characteristic

nRF52832 GATT 自定義Service/Characteristic

ble_app_blinky例程中,直接調用了sdk的ble_lbs_init函數來初始化service,所以爲了增加我們自己的service,從ble_lbs_init來看


uint32_t ble_lbs_init(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init)
{
    uint32_t   err_code;
    ble_uuid_t ble_uuid;

    // Initialize service structure.
    p_lbs->led_write_handler = p_lbs_init->led_write_handler;

    // Add service.
    ble_uuid128_t base_uuid = {LBS_UUID_BASE};
    err_code = sd_ble_uuid_vs_add(&base_uuid, &p_lbs->uuid_type);
    VERIFY_SUCCESS(err_code);

    ble_uuid.type = p_lbs->uuid_type;
    ble_uuid.uuid = LBS_UUID_SERVICE;

    err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_lbs->service_handle);
    VERIFY_SUCCESS(err_code);

    // Add characteristics.
    err_code = button_char_add(p_lbs, p_lbs_init);
    VERIFY_SUCCESS(err_code);

    err_code = led_char_add(p_lbs, p_lbs_init);
    VERIFY_SUCCESS(err_code);

    return NRF_SUCCESS;
}

初始化的整個過程和藍牙BLE的GATT profiles有關。

Service->增加Characteristic->增加Descriptor

下圖爲官方提供的流程

(S132 v6.0.0 API Reference - Message Sequence Charts - GATTS ATT Table Population部分)
在這裏插入圖片描述

自定義Service實現

根據上述及uuid相關,自定義一個service如下:
需要一個基本uuid,這裏測試,使用0000XXXX-1234-5678-9abcdef012345678

#define USER_UUID_BASE {0x78, 0x56, 0x34, 0x12, 0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00}
//定義基本UUID
ble_uuid128_t base_uuid = {USER_UUID_BASE};
err_code = sd_ble_uuid_vs_add(&base_uuid, &ble_uuid.type);
APP_ERROR_CHECK(err_code);

下一步要增加Service,在增加Service前,爲了方便使用定義一個結構體:

struct user_ble_gatt_s
{
    uint16_t                    service_handle;      // Handle of LED Button Service (as provided by the BLE stack). 
    ble_gatts_char_handles_t    char1_handles;    // Handles related to the Characteristic 1. 
    ble_gatts_char_handles_t    char2_handles; // Handles related to the  Characteristic 2. 此處只增加了一個characteristic,所以此處暫時不使用
    uint8_t                     uuid_type;           //< UUID type for the Service.
};

這個結構體是爲了記錄service/characteristic handles,方便其他位置調用。(當然也可以不使用結構體,直接用變量儲存。)

結構體定義變量:

struct user_ble_gatt_s user_uuid;

增加Service

  //增加Service
    ble_uuid.type=user_uuid.uuid_type;
    ble_uuid.uuid = USER_UUID_SERVICE;
    err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &user_uuid.service_handle );
    APP_ERROR_CHECK(err_code);

增加Service後,就可以增加characteristic,爲了實現藍牙的雙方通信,暫時只爲這個characteristic實現notify、read、write三個權限。

配置cccd:

ble_gatts_attr_md_t cccd_md;
        //配置cccd
        memset(&cccd_md, 0, sizeof(cccd_md));
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
        cccd_md.vloc = BLE_GATTS_VLOC_STACK;  

配置characteristic權限


ble_gatts_char_md_t char_md;
        memset(&char_md, 0, sizeof(char_md));
        char_md.char_props.read=1;  //允許讀
        char_md.char_props.write = 1;//允許寫
        char_md.char_props.notify=1;    //notify
        char_md.p_char_user_desc  = NULL;
        char_md.p_char_pf         = NULL;
        char_md.p_user_desc_md    = NULL;
        char_md.p_cccd_md         = &cccd_md;//實際測試:在notify=1;時,此處寫NULL notify也能實現
        char_md.p_sccd_md         = NULL;

配置其他屬性

ble_gatts_attr_md_t attr_md;
ble_gatts_attr_t    attr_char_value;
        memset(&attr_md, 0, sizeof(attr_md));
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
        attr_md.vloc    = BLE_GATTS_VLOC_STACK;

        memset(&attr_char_value, 0, sizeof(attr_char_value));
        attr_char_value.p_uuid    = &ble_uuid;	//此characteristic的uuid
        attr_char_value.p_attr_md = &attr_md;
        attr_char_value.init_len  = sizeof(uint8_t);	//初始值長度
        attr_char_value.init_offs = 0;
        attr_char_value.max_len   = sizeof(uint8_t)*2;  //value最大長度
        attr_char_value.p_value   = NULL;  //初始值

增加characteristic

最後調用增加characteristic函數:sd_ble_gatts_characteristic_add(user_uuid.service_handle,&char_md,&attr_char_value,&user_uuid.char1_handles);至此,characteristic已經增加完成,但是僅能實現read和notify功能,write功能寫入,mcu卻無法處理,所以還需要做一些配置

characteristic寫功能實現

sdk中有這麼一個宏:

#define NRF_SDH_BLE_OBSERVER (   
	_name,
    _prio,
    _handler,
    _context 
)
Macro for registering nrf_sdh_soc_evt_observer_t. Modules that want to be notified about SoC events must register the handler using this macro.
This macro places the observer in a section named "sdh_soc_observers".

Parameters
[in]    _name   	Observer name.
[in]    _prio   	Priority of the observer event handler. The smaller the number, the higher the priority.
[in]    _handler    BLE event handler.
[in]    _context    Parameter to the event handler.

描述中:Modules that want to be notified about SoC events must register the handler using this macro.

所以在user_uuid定義處增加此宏:

struct user_ble_gatt_s user_uuid;
NRF_SDH_BLE_OBSERVER(user_uuid_obs,0,ble_user_uuid_on_ble_evt, &user_uuid);

其中,_name:user_uuid_obs只是name,隨意定義,
_context:&user_uuid爲回調函數的參數,如果沒有必要可以設置爲NULL ,
關鍵是_prio:0,_handler:ble_user_uuid_on_ble_evt兩個參數

重症糾結患者,回調函數:

void ble_user_uuid_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
{
    ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
    switch (p_ble_evt->header.evt_id)
    {
      case BLE_GATTS_EVT_WRITE:
          for(int i=0;i<p_evt_write->len;i++)	//這裏僅僅是直接將ble發送來的數據打印出來不做其他處理
            NRF_LOG_INFO("%x",p_evt_write->data[i]);
        break;
      default: 	// No implementation needed.
        break;
    }
}

簡單的幾句,但是如果沒有sdk的例程,要找到數據儲存在p_evt_write->data數組裏,可能要花上一段時間了…

注意,寫入的數據的最大長度取決與 attr_char_value.max_len = sizeof(uint8_t)*2;【value最大長度,如果超出限制,那麼寫入會無效】。如果寫入的數據長度小於現有的值的數據長度,那麼並不會覆蓋所有的值,如本身爲字符"123",現在寫入"a",那麼值並不是"a",而是"a23",需要注意。

至此characteristic的write功能簡單的實現。
自定義Service/Charactereistic功能已經簡單完成,這裏並沒有很深入的研究。
如果需要繼續增加其他權限或內容,可以根據藍牙的說明修改對應的權限配置即可。

nRF52832 ble_app_blinky 例程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章