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_t    err_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中提供的廣播流程:
在這裏插入圖片描述

轉換之後就是例程中的兩行:

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_handle    Provide 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_data  	Advertising data. If set to NULL, no advertising data will be used. See ble_gap_adv_data_t.
[in]    	p_adv_params    Advertising 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 =
{
    .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_advdata   Pointer to the structure for specifying the content of encoded data.
[out]   p_encoded_data  Pointer to the buffer where encoded data will be returned.
[in,out]    p_len   in: 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 例程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章