nRF 蓝牙广播数据及操作相关

若使用 advdata 中 sd_ble_gap_adv_set_configure 的方法设置广播,仍可以按照advertising的配置方法,广播数据结构体用 ble_advdata_t ,将各值填入后,要解析一遍才可 sd_ble_gap_adv_set_configure 配置广播数据:


 

/**@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 = NULL,
        .len = 0
    }
};


/**@brief Function for initializing the Advertising functionality.
 *
 * @details Encodes the required advertising data and passes it to the stack.
 * Also builds a structure, m_adv_params, to be passed to the stack
 * when starting advertising.
 */
static void advertising_init(void)
{
    uint32_t err_code;
    ble_advdata_t advdata;

    // Build and set advertising data.
    memset(&advdata, 0, sizeof(advdata));

    advdata.name_type = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance = false;
    advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;

    err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
    APP_ERROR_CHECK(err_code);

    // Initialize advertising parameters (used when starting advertising).
    memset(&m_adv_params, 0, sizeof(m_adv_params));

    m_adv_params.primary_phy = BLE_GAP_PHY_1MBPS;
    m_adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
    m_adv_params.p_peer_addr = NULL; // Undirected advertisement.
    m_adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
    m_adv_params.interval = APP_ADV_INTERVAL;
    m_adv_params.duration = APP_ADV_DURATION;

    err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
    APP_ERROR_CHECK(err_code);
}

 

advdata 方法的广播超时:

 

m_adv_params.duration = APP_ADV_DURATION;

 

广播超时后蓝牙事件 ble_evt_handler 为 BLE_GAP_EVT_ADV_SET_TERMINATED,超时原因为BLE_GAP_EVT_ADV_SET_TERMINATED_REASON_TIMEOUT

 

则进入此事件要做判断

if (p_ble_evt->evt.gap_evt.params.adv_set_terminated.reason == BLE_GAP_EVT_ADV_SET_TERMINATED_REASON_TIMEOUT)

 

另,由于最大广告事件导致的广播超时 max_adv_evts ,广播超时后蓝牙事件 ble_evt_handler 也为 BLE_GAP_EVT_ADV_SET_TERMINATED,超时原因为BLE_GAP_EVT_ADV_SET_TERMINATED_REASON_LIMIT_REACHED

 

 

广播数据中包含外观(include_appearance = true;)的,则可以在 gap_params_init 中用err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_HEART_RATE_SENSOR_HEART_RATE_BELT);

APP_ERROR_CHECK(err_code);

做相应的外观标识设置(参考官方心率例程)

 

参考骰子项目的发送端,多协议gzll的程序

 

你可能感兴趣的:(nRF 蓝牙广播数据及操作相关)