-
DEMO下载地址
https://download.csdn.net/download/mygod2008ok/12740643
-
定义广播所需要的变量
static ble_gap_adv_params_t adv_params;
static ble_gap_adv_data_t advdata_enc;
static ble_advdata_t advdata;
static ble_advdata_t rspdata;
static ble_advdata_manuf_data_t m_spec_data; // ADV厂商广播变量
static uint8_t s_specData[2]; // ADV厂商广播数据数组
static ble_advdata_manuf_data_t m_spec_rspdata; // rsp厂商广播变量
static uint8_t s_specrspData[20]; // rsp厂商广播数据数组
ble_uuid_t adv_uuids[] =
{
{BLE_UUID_HEART_RATE_SERVICE, BLE_UUID_TYPE_BLE},
};
-
广播初时化函数,上电仅调用一次
/**@brief Advertising functionality initialization.
*
* @details Encodes the required advertising data and passes it to the stack.
* Also builds a structure to be passed to the stack when starting advertising.
*/
static void advertising_init(void)
{
uint32_t err_code;
static uint8_t advdata_buff[BLE_GAP_ADV_SET_DATA_SIZE_MAX];
uint16_t advdata_buff_len = BLE_GAP_ADV_SET_DATA_SIZE_MAX;
static uint8_t rspdata_buff[BLE_GAP_ADV_SET_DATA_SIZE_MAX];
uint16_t rspdata_buff_len = BLE_GAP_ADV_SET_DATA_SIZE_MAX;
uint8_t flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
// Build and set advertising data.
memset(&advdata, 0, sizeof(advdata));
advdata.name_type = BLE_ADVDATA_FULL_NAME;
advdata.include_appearance = false;
advdata.flags = flags;
#if UUID_BROADCAST_ENABLED
advdata.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
advdata.uuids_complete.p_uuids = adv_uuids;
#endif
//-----------------------广播指定数据-----------------------------------------
#if SPEC_DATA_FLAG_ENABLED
m_spec_data.company_identifier = COMPANY_IDENTIFIER_ID;
s_specData[0] = 0;
s_specData[1] = 60;
m_spec_data.data.p_data = s_specData;
m_spec_data.data.size = sizeof(s_specData);
advdata.p_manuf_specific_data = &m_spec_data;
#endif
err_code = ble_advdata_encode(&advdata, advdata_buff, &advdata_buff_len);
APP_ERROR_CHECK(err_code);
memset(&rspdata, 0, sizeof(rspdata));
m_spec_rspdata.company_identifier = COMPANY_IDENTIFIER_ID;
m_spec_rspdata.data.p_data = s_specrspData;
m_spec_rspdata.data.size = sizeof(s_specrspData);
for(uint8_t i=0; i<sizeof(s_specrspData);i++)
{
s_specrspData[i] = i;
}
rspdata.p_manuf_specific_data = &m_spec_rspdata;
err_code = ble_advdata_encode(&rspdata, rspdata_buff, &rspdata_buff_len);
APP_ERROR_CHECK(err_code);
m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
memset(&advdata_enc, 0, sizeof(advdata_enc));
advdata_enc.adv_data.p_data = advdata_buff;
advdata_enc.adv_data.len = advdata_buff_len;
advdata_enc.scan_rsp_data.p_data = rspdata_buff;
advdata_enc.scan_rsp_data.len = rspdata_buff_len;
// Initialise advertising parameters (used when starting advertising).
memset(&adv_params, 0, sizeof(adv_params));
adv_params.primary_phy = BLE_GAP_PHY_1MBPS;
adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
adv_params.interval = FAST_ADV_INTERVAL;
adv_params.duration = FAST_ADV_DURATION;
err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &advdata_enc, &adv_params);
APP_ERROR_CHECK(err_code);
}
1.首先初时化ADV广播的厂商数据
s_specData[0] = 0; // 自定义数据1
s_specData[1] = 60; // 自定义数据2
m_spec_data.company_identifier = COMPANY_IDENTIFIER_ID; // 厂商ID
m_spec_data.data.p_data = s_specData; // 指向自定义数据
m_spec_data.data.size = sizeof(s_specData); // 自定义数据长度
advdata.p_manuf_specific_data = &m_spec_data; // 指向ADV厂商变量
err_code = ble_advdata_encode(&advdata, advdata_buff, &advdata_buff_len); // 设置ADV广播数据及缓冲buffer
APP_ERROR_CHECK(err_code);
2.初时化rsp广播的厂商数据
for(uint8_t i=0; i<sizeof(s_specrspData);i++) // 自定义数据,测量以0~19作为测试数据
{
s_specrspData[i] = i;
}
m_spec_rspdata.company_identifier = COMPANY_IDENTIFIER_ID; // 厂商ID
m_spec_rspdata.data.p_data = s_specrspData; // 指向rsp自定义数据
m_spec_rspdata.data.size = sizeof(s_specrspData); // rsp自定义数据长度
rspdata.p_manuf_specific_data = &m_spec_rspdata; //指向rsp厂商变量
err_code = ble_advdata_encode(&rspdata, rspdata_buff, &rspdata_buff_len); // 设置rsp广播数据及缓冲buffer
APP_ERROR_CHECK(err_code);
3.关联广播变量
advdata_enc.adv_data.p_data = advdata_buff; //分配ADV广播数据缓冲buffer
advdata_enc.adv_data.len = advdata_buff_len; // ADV广播数据长度
advdata_enc.scan_rsp_data.p_data = rspdata_buff; //分配rsp广播数据缓冲buffer
advdata_enc.scan_rsp_data.len = rspdata_buff_len; // rsp广播数据长度
-
beacon_advertising_init函数,广播超时调用
static void beacon_advertising_init(void)
{
uint32_t err_code;
-----------------------广播指定数据-----------------------------------------
err_code = ble_advdata_encode(&advdata, advdata_enc.adv_data.p_data, &advdata_enc.adv_data.len);
APP_ERROR_CHECK(err_code);
err_code = ble_advdata_encode(&rspdata, advdata_enc.scan_rsp_data.p_data, &advdata_enc.scan_rsp_data.len);
APP_ERROR_CHECK(err_code);
#if CON_SPEC_DATA_FLAG_ENABLED
if(m_conn_handle != BLE_CONN_HANDLE_INVALID)
adv_params.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_SCANNABLE_UNDIRECTED; //连接时的广播类型
else
#endif
adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED; //未连接时的广播类型
err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &advdata_enc, &adv_params);
APP_ERROR_CHECK(err_code);
advertising_start();
}
-
beacon_advertising_init函数调用
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
ret_code_t err_code = NRF_SUCCESS;
switch (p_ble_evt->header.evt_id)
{
case BLE_GAP_EVT_ADV_SET_TERMINATED://蓝牙超时
if (p_ble_evt->evt.gap_evt.params.adv_set_terminated.reason ==
BLE_GAP_EVT_ADV_SET_TERMINATED_REASON_TIMEOUT)
{
// NRF_LOG_INFO("BLE_GAP_EVT_ADV_SET_TERMINATED");
#if SPEC_DATA_FLAG_ENABLED
beacon_advertising_init(); // 厂商数据广播更新
#else
advertising_start();
#endif
//NRF_LOG_INFO("BLE_START2=%d",s_powerOffFlag);
// Go to system-off mode (this function will not return; wakeup will cause a reset)
// err_code = sd_power_system_off();
// APP_ERROR_CHECK(err_code);
}
break;
......
}
}
-
厂商数据内容更新
- ADV厂商数据内容更新,操作更新specData数组里的内容即可
- rsp厂商数据内容更新,操作更新s_specrspData数组里的内容即可
-
运行结果效果图