2018年7月23日
SDK版本:nRF5_SDK_15.0.0_a53641a
SoftDevice版本:s132_nrf52_6.0.0
例程:nRF5_SDK_15.0.0_a53641a\examples\ble_peripheral\ble_app_blinky
此篇文章主要是為了為nRF52832增加我需要的characteristic,從分析例程ble_app_blinky中增加的Led/Button Service開始.
ble_app_blinky例程中,直接調(diào)用了sdk的ble_lbs_init函數(shù)來初始化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;
}
初始化的整個(gè)過程很簡(jiǎn)單,也和藍(lán)牙BLE的GATT profiles有關(guān).
Service-增加Characteristic-增加Descriptor
官方提供的流程如圖
(S132 v6.0.0 API Reference - Message Sequence Charts - GATTS ATT Table Population部分)

自定義Service實(shí)現(xiàn)
根據(jù)上述及uuid相關(guān),自定義一個(gè)service如下:
需要一個(gè)基本uuid,這里測(cè)試,使用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前,為了方便使用定義一個(gè)結(jié)構(gòu)體:
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. 此處只增加了一個(gè)characteristic,所以此處暫時(shí)不使用
uint8_t uuid_type; //< UUID type for the Service.
};
這個(gè)結(jié)構(gòu)體是為了記錄service/characteristic handles,方便其他位置調(diào)用.(當(dāng)然也可以不使用結(jié)構(gòu)體,直接用變量?jī)?chǔ)存.)
結(jié)構(gòu)體定義變量:
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,為了實(shí)現(xiàn)藍(lán)牙的雙方通信,暫時(shí)只為這個(gè)characteristic實(shí)現(xiàn)notify,read,write三個(gè)權(quán)限.為實(shí)現(xiàn)notify,需要配置characteristic的一個(gè)特殊descriptors:CCCD(Client Characteristic Configuration descriptors),
配置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權(quán)限
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;//實(shí)際測(cè)試:在notify=1;時(shí),此處寫NULL notify也能實(shí)現(xiàn)
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);//初始值長(zhǎng)度
attr_char_value.init_offs = 0;
attr_char_value.max_len = sizeof(uint8_t)*2; //value最大長(zhǎng)度
attr_char_value.p_value = NULL; //初始值
增加characteristic
最后調(diào)用增加characteristic函數(shù):
sd_ble_gatts_characteristic_add(user_uuid.service_handle,&char_md,&attr_char_value,&user_uuid.char1_handles);
至此,characteristic已經(jīng)增加完成,但是僅能實(shí)現(xiàn)read和notify功能,write功能寫入,mcu卻無法處理,所以還需要做一些配置
characteristic寫功能實(shí)現(xiàn)
sdk中有這么一個(gè)宏:
#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為回調(diào)函數(shù)的參數(shù),如果沒有必要可以設(shè)置為NULL ,關(guān)鍵是_prio:0,_handler:ble_user_uuid_on_ble_evt兩個(gè)參數(shù)
下面糾結(jié)的來了,回調(diào)函數(shù):
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發(fā)送來的數(shù)據(jù)打印出來不做其他處理
NRF_LOG_INFO("%x",p_evt_write->data[i]);
break;
default: // No implementation needed.
break;
}
}
很簡(jiǎn)單的幾句,但是如果沒有sdk的例程,要找到數(shù)據(jù)儲(chǔ)存在p_evt_write->data數(shù)組里,可能要花上一段時(shí)間了......
注意,寫入的數(shù)據(jù)的最大長(zhǎng)度取決與 attr_char_value.max_len = sizeof(uint8_t)*2; //value最大長(zhǎng)度.如果超出限制,那么寫入會(huì)無效.如果寫入的數(shù)據(jù)長(zhǎng)度小于現(xiàn)有的值的數(shù)據(jù)長(zhǎng)度,那么并不會(huì)覆蓋所有的值,如本身為字符"123",現(xiàn)在寫入"a",那么值并不是"a",而是"a23",需要注意.
至此characteristic的write功能也簡(jiǎn)單的實(shí)現(xiàn).
自定義Service/Charactereistic功能已經(jīng)簡(jiǎn)單完成,這里并沒有很深入的研究.
如果需要繼續(xù)增加其他權(quán)限或內(nèi)容,可以根據(jù)藍(lán)牙的說明修改對(duì)應(yīng)的權(quán)限配置即可