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 例程

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