This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Find short name in advertising packet using ble_advdata_short_name_find(...) getting always false?

Hello to all,

I am using nRF52832 sdk15.0 version with SES IDE. I want to find adverting with short for e.g.--->

we have BLE app_uart peripherals and one multilink central.

ble peripheral adverting with name of "Nordic-XXXXXX" where x is BLE 48 bit mac address. In central i want to scan ble peripheral device as short Name set is "Nordic-".

for achieving above i am using program like this:

 if (ble_advdata_short_name_find(p_adv_report->data.p_data,
                                    p_adv_report->data.len,
                                    SCAN_DEVICE_NAME,sizeof(SCAN_DEVICE_NAME)))
    {
        // Name is a match, initiate connection.
        NRF_LOG_INFO("Short name found");
        err_code = sd_ble_gap_connect(&p_adv_report->peer_addr,
                                      &m_scan_params,
                                      &m_connection_param,
                                      APP_BLE_CONN_CFG_TAG);
        if (err_code != NRF_SUCCESS)
        {
            NRF_LOG_ERROR("Connection Request Failed, reason %d", err_code);
        }
    }

where SCAN_DEVICE_NAME = "Nordic-"

If i use ble_advdata_name_find with full advertising name working well. But why short name find is not working please any one tell me what is proper way for finding short name in whole adverting name.
Whats wrong in my program.... When i debugging so getting in ble_advdata.c data_offset is NULL always. 

Please suggest me better way or any changes in my program..

Thanks..

Parents
  • Hi,

    This is not how ble_advdata_short_name_find works. This function will parse the advertising data, looking for the Bluetooth SIG specified advertising data type "Shortened Local Name". In your case, the advertising name is of type "Complete Local Name", which is why the function ble_advdata_short_name_find always return false.

    If you want to look for a specific string within the advertising name, you need to write your own advertising data parsing function.

    Best regards,
    Jørgen

  • You are correct, ble_advdata_short_name_find will not work as the advertising name is of the type "Complete Local Name". ble_advdata_short_name_find will look for names of the type "Shortened Local Name" in the advertising packet. You need to use a function very similar to ble_advdata_name_find, but instead of checking the found name towards the complete length of the found name, you should check it towards the length of your "search string". Note that this will only work if your "search string" is located in the beginning of the name. 

    bool ble_advdata_name_find(uint8_t const * p_encoded_data,
                               uint16_t        data_len,
                               char    const * p_target_name)
    {
        uint16_t        parsed_name_len;
        uint8_t const * p_parsed_name;
        uint16_t        data_offset          = 0;
    
        if (p_target_name == NULL)
        {
            return false;
        }
    
    
        parsed_name_len = ble_advdata_search(p_encoded_data,
                                             data_len,
                                             &data_offset,
                                             BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME);
    
        p_parsed_name = &p_encoded_data[data_offset];
    
        if (   (data_offset != 0)
            && (parsed_name_len != 0)
            && (memcmp(p_target_name, p_parsed_name, strlen(p_target_name)) == 0))
        {
            return true;
        }
    
        return false;
    }

    (Note that I have not tested this solution)

  • Hello,

    I have tested this above program not working set name like this:

    if (ble_advdata_name_find(p_adv_report->data.p_data,
                                  p_adv_report->data.len,
                                  m_target_periph_name[0])

    where static char const *m_target_periph_name[] = {sensor};

    And my sensor device advertising with name of sensor-XXXXXXX(ID) 

    central not connect with sensor because not getting true from above program.

    Will you have other option..

    Thanks,

Reply
  • Hello,

    I have tested this above program not working set name like this:

    if (ble_advdata_name_find(p_adv_report->data.p_data,
                                  p_adv_report->data.len,
                                  m_target_periph_name[0])

    where static char const *m_target_periph_name[] = {sensor};

    And my sensor device advertising with name of sensor-XXXXXXX(ID) 

    central not connect with sensor because not getting true from above program.

    Will you have other option..

    Thanks,

Children
No Data
Related