nRF 扫描条件过滤做法

SDK15.2及以上:

如若设置如下,则使用了官方的扫描过滤器

满足条件后扫描回调事件会是NRF_BLE_SCAN_EVT_FILTER_MATCH再做对应操作,具体参考探锦科技项目

/**@brief Function for handling Scanning Module events.
 */
static void scan_evt_handler(scan_evt_t const * p_scan_evt)
{

    ret_code_t err_code;

    p_not_found_adv = p_scan_evt->params.p_not_found;
    ble_gap_scan_params_t const * p_scan_param = p_scan_evt->p_scan_params;

//    NRF_LOG_INFO("%02X" ,p_scan_evt->scan_evt_id);

    switch(p_scan_evt->scan_evt_id)
    {
        case NRF_BLE_SCAN_EVT_CONNECTING_ERROR:
        {
            err_code = p_scan_evt->params.connecting_err.err_code;
            APP_ERROR_CHECK(err_code);
        } break;

        case NRF_BLE_SCAN_EVT_CONNECTED:
        {
            ble_gap_evt_connected_t const * p_connected =
            p_scan_evt->params.connected.p_connected;

//            Scan is automatically stopped by the connection.
//            NRF_LOG_INFO("Connecting to target %02x%02x%02x%02x%02x%02x",
//                            p_connected->peer_addr.addr[0],
//                            p_connected->peer_addr.addr[1],
//                            p_connected->peer_addr.addr[2],
//                            p_connected->peer_addr.addr[3],
//                            p_connected->peer_addr.addr[4],
//                            p_connected->peer_addr.addr[5]);
        } break;

        case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT:
            NRF_LOG_INFO("Scan timed out.");
            scan_start();
            break;

        case NRF_BLE_SCAN_EVT_FILTER_MATCH:

            break;

        case NRF_BLE_SCAN_EVT_NOT_FOUND:

            break;

        default:

            break;

    }

}



static void scan_init(void)
{
    ret_code_t err_code;

    nrf_ble_scan_init_t init_scan;

    memset(&init_scan, 0, sizeof(init_scan));

    init_scan.connect_if_match = true;

    init_scan.conn_cfg_tag = APP_BLE_CONN_CFG_TAG;

    err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
    APP_ERROR_CHECK(err_code);

    if (strlen(m_target_periph_name) != 0)
    {
        err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_NAME_FILTER, m_target_periph_name);

        APP_ERROR_CHECK(err_code);

    }
    else
    {

        err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_UUID_FILTER, &m_nus_uuid);
        APP_ERROR_CHECK(err_code);

    }

    err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_NAME_FILTER, false);
    APP_ERROR_CHECK(err_code);
}

 

 

如若不设置官方的扫描过滤器

if (strlen(m_target_periph_name) != 0)
{
    err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_NAME_FILTER, m_target_periph_name);
    APP_ERROR_CHECK(err_code);
}
else
{
    err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_UUID_FILTER, &m_nus_uuid);
    APP_ERROR_CHECK(err_code);
}

err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_NAME_FILTER, false);
APP_ERROR_CHECK(err_code);

则扫描回调事件会是NRF_BLE_SCAN_EVT_NOT_FOUND,此时可以自行对扫描到的广播数据做解析方法如下:


 

uint16_t custom_ble_advdata_content_len(uint8_t const * p_encoded_data, uint16_t data_len, uint8_t ad_type)
{
    uint32_t index = 0;
    uint16_t p_data_len_1 = 0;
    uint8_t field_type = 0;
    uint8_t field_length = 0;

    p_data_len_1 = data_len;

    while (index < p_data_len_1)
    {
        field_length = p_encoded_data[index];
        field_type = p_encoded_data[index+1];

        if (field_type == ad_type)
        {
            return field_length-1;
        }

        index += field_length + 1;
    }
}



uint8_t * custom_ble_advdata_parse(uint8_t * p_encoded_data, uint16_t data_len, uint8_t ad_type)
{
    uint16_t len = custom_ble_advdata_content_len(p_encoded_data, data_len, ad_type);
    uint32_t index = 0;
    uint16_t p_data_len_2 = 0;

    if (len == 0)
    {
        return NULL;
    }
    else
    {
//        p_data_len = data_len;
        p_data_len_2 = len;

        while (index < p_data_len_2)
        {
            uint8_t field_length = p_encoded_data[index];
            uint8_t field_type = p_encoded_data[index+1];
            if (field_type == ad_type)
            {
                return &p_encoded_data[index+2];
            }

            index += field_length + 1;
        }
    }
}


p_parsed_name_data_len = custom_ble_advdata_content_len(p_not_found_adv->data.p_data, p_not_found_adv->data.len, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME);



p_parsed_name_data = custom_ble_advdata_parse(p_not_found_adv->data.p_data, p_not_found_adv->data.len, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME);

 

即可获取到广播包中各类型数据的长度及数据内容

具体参考多度项目

 

你可能感兴趣的:(nRF,SCAN)