关于如何新增一个自定义的服务,其实Nordic已经有现成的封装的函数了。下面是我之前写的一个自定义服务。供你参考:
/**@brief Function for initializing services that will be used by the application.
*/
static void services_init(void)
{
uint32_t err_code;
ble_gatts_char_md_t gatts_char_md;
ble_gatts_attr_t gatts_attr;
ble_gatts_attr_md_t gatts_attr_md;
memset(&gatts_char_md, 0, sizeof(gatts_char_md));
memset(&gatts_attr, 0, sizeof(gatts_attr));
memset(&gatts_attr_md, 0, sizeof(gatts_attr_md)); // 初始化各个结构体
err_code = sd_ble_gatts_service_add(type, m_switch_server_uuids,&m_switch_server_handle);//定义一个服务
// ADV_LOG("[sd_ble_gatts_characteristic_add]: %d.\r\n", err_code);
APP_ERROR_CHECK(err_code);
//----------------------------填充GATT属性的元数据-------------------------//
gatts_char_md.char_props.notify = 1;
gatts_char_md.char_props.write_wo_resp = 1;//属性权限是写命令
//-------------------------以下是CCCD的元数据设置-------------------------//
// gatts_char_md.p_cccd_md->read_perm.sm = 1;
// gatts_char_md.p_cccd_md->read_perm.lv = 1;//No security is needed
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&gatts_char_md.p_cccd_md->read_perm);
// gatts_char_md.p_cccd_md->write_perm.sm = 1;
// gatts_char_md.p_cccd_md->write_perm.lv = 1;//No security is needed
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&gatts_char_md.p_cccd_md->write_perm);
gatts_char_md.p_cccd_md->vloc = BLE_GATTS_VLOC_STACK;
gatts_char_md.p_cccd_md->vlen = 1;
// gatts_char_md.p_cccd_md->vlen = 1;
gatts_char_md.p_cccd_md->rd_auth = 0;//No Read authorization
gatts_char_md.p_cccd_md->wr_auth = 0;//No Write authorization
//----------------------------设置具体属性的元数据-------------------------//
gatts_attr_md.rd_auth = 0;//No Read authorization
gatts_attr_md.wr_auth = 0;//No Write authorization
// gatts_attr_md.read_perm.lv = 1;
// gatts_attr_md.read_perm.sm = 1;//No security is needed
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&gatts_attr_md.read_perm);
gatts_attr_md.vloc = BLE_GATTS_VLOC_STACK;//Attribute Value is located in stack memory, no user memory is required
gatts_attr_md.vlen = 1;
// gatts_attr_md.write_perm.lv = 1;
// gatts_attr_md.write_perm.sm = 1;//No security is needed
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&gatts_attr_md.write_perm);
//----------------------------填充具体属性的元数据------------------------//
gatts_attr.p_uuid = m_switch_char_uuids;
gatts_attr.p_attr_md = &gatts_attr_md;
gatts_attr.init_len = GATT_ATTR_VALUE_SIZE_INIT;
gatts_attr.max_len = BLE_GATTS_VAR_ATTR_LEN_MAX;
/*定义一个属性及客户端属性特征描述符*/
err_code = sd_ble_gatts_characteristic_add(m_switch_server_handle,
&gatts_char_md, &gatts_attr, &m_switch_char_struct_handle);
// RTT_PRINTF("[sd_ble_gatts_characteristic_add]: %d.\r\n", err_code);
APP_ERROR_CHECK(err_code);
#ifdef BLE_DFU_APP_SUPPORT
/** @snippet [DFU BLE Service initialization] */
ble_dfu_init_t dfus_init;
// Initialize the Device Firmware Update Service.
memset(&dfus_init, 0, sizeof(dfus_init));
dfus_init.evt_handler = dfu_app_on_dfu_evt;
dfus_init.error_handler = NULL;
dfus_init.evt_handler = dfu_app_on_dfu_evt;
dfus_init.revision = DFU_REVISION;
err_code = ble_dfu_init(&m_dfus, &dfus_init);
APP_ERROR_CHECK(err_code);
dfu_app_reset_prepare_set(reset_prepare);
dfu_app_dm_appl_instance_set(m_app_handle);
/** @snippet [DFU BLE Service initialization] */
#endif // BLE_DFU_APP_SUPPORT
}