nRF52832 广播相关配置

nRF52832 广播相关配置 【nRF52832 广播相关配置】先上例程中和广播有关的代码吧:

/**@brief Function for initializing the Advertising functionality. * * @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. */ void advertising_init(void) { ret_code_terr_code; ble_advdata_t advdata; ble_advdata_t srdata; ble_uuid_t adv_uuids[] = {{LBS_UUID_SERVICE, m_lbs.uuid_type}}; // Build and set advertising data. memset(&advdata, 0, sizeof(advdata)); advdata.name_type= BLE_ADVDATA_FULL_NAME; advdata.include_appearance = true; advdata.flags= BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE; memset(&srdata, 0, sizeof(srdata)); srdata.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]); srdata.uuids_complete.p_uuids= adv_uuids; err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len); APP_ERROR_CHECK(err_code); err_code = ble_advdata_encode(&srdata, m_adv_data.scan_rsp_data.p_data, &m_adv_data.scan_rsp_data.len); APP_ERROR_CHECK(err_code); ble_gap_adv_params_t adv_params; // Set advertising parameters. memset(&adv_params, 0, sizeof(adv_params)); adv_params.primary_phy= BLE_GAP_PHY_1MBPS; adv_params.duration= APP_ADV_DURATION; adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED; adv_params.p_peer_addr= NULL; adv_params.filter_policy= BLE_GAP_ADV_FP_ANY; adv_params.interval= APP_ADV_INTERVAL; err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &adv_params); APP_ERROR_CHECK(err_code); }

然后调用err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);启动广播
SDK中提供的广播流程:
nRF52832 广播相关配置
文章图片

转换之后就是例程中的两行:
err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &adv_params); err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);

简单看看sd_ble_gap_adv_set_configure吧
广播配置函数sd_ble_gap_adv_set_configure
uint32_t sd_ble_gap_adv_set_configure(uint8_t *p_adv_handle,ble_gap_adv_data_t const *p_adv_data,ble_gap_adv_params_t const * p_adv_params)[in,out]p_adv_handleProvide a pointer to a handle containing BLE_GAP_ADV_SET_HANDLE_NOT_SET to configure a new advertising set. On success, a new handle is then returned through the pointer. Provide a pointer to an existing advertising handle to configure an existing advertising set. [in]p_adv_dataAdvertising data. If set to NULL, no advertising data will be used. See ble_gap_adv_data_t. [in]p_adv_paramsAdvertising parameters. When this function is used to update advertising data while advertising, this parameter must be NULL. See ble_gap_adv_params_t.

第一个参数p_adv_handle
这里创建新的广播设置,第一个参数p_adv_handle使用BLE_GAP_ADV_SET_HANDLE_NOT_SET
第二个参数ble_gap_adv_data_t const * p_adv_data
第二个参数ble_gap_adv_data_t const * p_adv_data为广播数据。首先配置一个空间给ble_gap_adv_data_t ,然后设置数据即可。
初始化ble_gap_adv_data_t 配置内存
/**@brief Struct that contains pointers to the encoded advertising data. */ static ble_gap_adv_data_t m_adv_data = https://www.it610.com/article/{ .adv_data = { .p_data = m_enc_advdata, .len= BLE_GAP_ADV_SET_DATA_SIZE_MAX }, .scan_rsp_data = { .p_data = m_enc_scan_response_data, .len= BLE_GAP_ADV_SET_DATA_SIZE_MAX} };

ble_gap_adv_data_t 写入数据 adv_data 为广播数据,
scan_rsp_data为扫描回应数据,
本质其实就是个指针和长度而已,关键就是里面的数据了。
初始化这些数据,可以使用函数ble_advdata_encode实现。因为有广播数据和扫描回应数据,所以需要执行两次分别初始化这两个变量。
ble_advdata_encode函数原型
ret_code_t ble_advdata_encode(ble_advdata_t const *constp_advdata,uint8_t *const p_encoded_data,uint16_t *const p_len) /* Function for encoding data in the Advertising and Scan Response data format (AD structures). This function encodes data into the Advertising and Scan Response data format (AD structures) based on the fields in the supplied structures. This function can be used to create a payload of Advertising packet or Scan Response packet, or a payload of NFC message intended for initiating the Out-of-Band pairing.Parameters [in]p_advdataPointer to the structure for specifying the content of encoded data. [out]p_encoded_dataPointer to the buffer where encoded data will be returned. [in,out]p_lenin: Size of p_encoded_data buffer. out: Length of encoded data. */

sdk说的还是很清楚了,所以去配置ble_advdata_t const *constp_advdata吧。
ble_advdata_t参数还是很多,详细看SDK,这里只看看例程中使用的几个参数:
ble_advdata_t advdata; ble_advdata_t srdata; ble_uuid_t adv_uuids[] = {{LBS_UUID_SERVICE, m_lbs.uuid_type}}; memset(&advdata, 0, sizeof(advdata)); advdata.name_type= BLE_ADVDATA_FULL_NAME; //广播名称类型,包括短名称,全称以及无名称 advdata.include_appearance = true; //Determines if Appearance shall be included. 应该涉及蓝牙的属性,还不清楚详细功能 advdata.flags= BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE; //广播标志涉及蓝牙属性,在这里配置为一般发现模式,不支持BR/EDR memset(&srdata, 0, sizeof(srdata)); srdata.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]); srdata.uuids_complete.p_uuids= adv_uuids; //List of UUIDs in the 'Complete' list.

BR:Basic Rate,EDR:Enhanced Data Rate,BR是正宗的蓝牙技术,似乎就是指经典蓝牙连接而已(待确认)
第三个参数ble_gap_adv_params_t const * p_adv_params
第三个参数ble_gap_adv_params_t const * p_adv_params,广播的一些配置信息
ble_gap_adv_params_t adv_params; // Set advertising parameters. memset(&adv_params, 0, sizeof(adv_params)); adv_params.primary_phy= BLE_GAP_PHY_1MBPS; //此版本SoftDevice只支持此选项 adv_params.duration= APP_ADV_DURATION; //广播超时时间 APP_ADV_DURATION为BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED 即无限制 adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED; //设置广播为可连接和可扫描的无定向广告事件 adv_params.p_peer_addr= NULL; adv_params.filter_policy= BLE_GAP_ADV_FP_ANY; //设置为允许任何设备的扫描和连接请求 adv_params.interval= APP_ADV_INTERVAL; //设置广播间隔时间,单位为0.625 ms,这里APP_ADV_INTERVAL为64,也就是65*0.625ms=40ms

ble_gap_adv_params_t除了这些参数外还有一些,一部分为experimental features,还有一些在properties.type设置为BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED时无效。除了这些还有以下两个参数;
ble_gap_ch_mask_t ble_gap_adv_params_t::channel_mask//屏蔽广播信道设置,广播使用信道37 38 39,只能针对这3个屏蔽,并且至少开启一个. uint8_t ble_gap_adv_params_t::scan_req_notification//设能广播扫描请求通知,当被扫描时,在蓝牙事件回调函数中激活(应该有更合适的词吧)BLE_GAP_EVT_SCAN_REQ_REPORT事件 //以上不需要屏蔽广播信道,不需要回调,所以都为0即可.

nRF52832 ble_app_blinky 例程

    推荐阅读