1、建立Service结构体,包含了建立service、characteristic相关信息Handle。
struct ble_lbs_s
{
uint16_t service_handle;
/服务句柄,蓝牙栈提供
ble_gatts_char_handles_t led_char_handles;
/*指向LED特征句柄
ble_gatts_char_handles_t button_char_handles;
/指向Button特征句柄
uint8_t uuid_type;
/服务UUID信息
ble_lbs_led_write_handler_t led_write_handler;
/定义LED特征被写入时,回调的事件句柄
};
2、利用Service结构体建立宏定义,建立BLE实体化对象
#define BLE_LBS_DEF(_name) static ble_lbs_t _name; NRF_SDH_BLE_OBSERVER(_name ## _obs,
BLE_LBS_BLE_OBSERVER_PRIO,
ble_lbs_on_ble_evt, &_name)
其中NRF_SDH_BLE_OBSERVER存在nrf_sdh_ble.h文件中#define NRF_SDH_BLE_OBSERVER(_name, _prio, _handler, _context)
NRF_SECTION_SET_ITEM_REGISTER(sdh_ble_observers, _prio, static nrf_sdh_ble_evt_observer_t _name) =
{
.handler = _handler,
.p_context = _context
}
利用宏定义将搜索项定义为ble栈中sdh_ble_observers(客户端)的一部分。
3、在main.c文件中,进行此次服务实体化对象
BLE_LBS_DEF(m_lbs);
并且利用
NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
将服务事件进行本地注册,利用ble_evt_handler()回调函数进行事件的相应处理。
4、建立gatt数据结构//进行数据的GATT用来规范attribute中的数据内容,并运用group(分组)的概念对attribute进行分类管理
struct nrf_ble_gatt_s
{
uint16_t att_mtu_desired_periph; //!< Requested ATT_MTU size for the next peripheral connection that is established.
uint16_t att_mtu_desired_central; //!< Requested ATT_MTU size for the next central connection that is established.
uint8_t data_length; //!< Data length to use for the next connection that is established.
nrf_ble_gatt_link_t links[NRF_BLE_GATT_LINK_COUNT]; //!< GATT related information for all active connections.
nrf_ble_gatt_evt_handler_t evt_handler; //!< GATT event handler.
};