I want to get pheriphetial device name..

I am a beginner developer.


I am working on ble_app_multilink_central and ble_app_uart-based projects and succeeded in BLE multi-connecting. But there is a problem. An error occurs in the BLE connection. In my project, when the device names of central and peripheral match, sd_ble_gap_connect is executed to establish a BLE connection between devices. However, even though the device name between the two devices is different, it sometimes becomes a BLE connection. It's not always like that, so I'm very frustrated. So I want to check the device name of peripheral in central.


I think the solution may be related to "static void on_adv_report (ble_gap_evt_adv_report_t const * p_adv_report)".

This is my code..

static void on_adv_report(ble_gap_evt_adv_report_t const * p_adv_report)
{
    ret_code_t err_code;


    if ( ble_advdata_name_find(p_adv_report->data.p_data, p_adv_report->data.len, m_target_periph_name) || ble_advdata_name_find(p_adv_report->data.p_data, p_adv_report->data.len, DEVICE_NAME))
    {
        scan_stop();
        // Name is a match, initiate connection.

        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)
            {
#ifdef DEBUG 		
                NRF_LOG_ERROR("Connection Request Failed, reason %d", err_code);
#endif			
            }
#ifdef DEBUG 		
                NRF_LOG_ERROR("Connection Request Failed, reason %d", err_code);
#endif

    }   

}
    

And I checked 'bool ble_advdata_name_find' at C:\Users\Root\Desktop\nordic_15.2\components\ble\common\ble_advdata.c

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)
        && (strlen(p_target_name) == parsed_name_len)
        && (memcmp(p_target_name, p_parsed_name, parsed_name_len) == 0))
    {
        return true;
    }

    return false;
}

That's all I found out.
I want to debug the device name that peripheral advertises.

Please help me solve this problem. T^T

Parents
  • Thanks for your reply.
    I can check the log and I tried it.

    Before that I found a solution.
    Through many posts, I found information that the peripheral device name can be obtained by using ble_advdata_parse() or ble_advdata_search() from BLE_GAP_EVT_ADV_REPORT of 'static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)'.

    However, when I actually debugged, I don't know why, but the next operation of 'ble_advdata_parse()' did not proceed.

    So I got the peripheral name from on_adv_report().

    This is my code.

    static void on_adv_report(ble_gap_evt_adv_report_t const * p_adv_report)
    {
        ret_code_t err_code;
        uint16_t data_offset = 0;
        int parsed_name_len = ble_advdata_search(p_adv_report->data.p_data, p_adv_report->data.len, &data_offset, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME);
        uint8_t *buf = ble_advdata_parse(p_adv_report->data.p_data, p_adv_report->data.len, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME);
    
        if ( ble_advdata_name_find(p_adv_report->data.p_data, parsed_name_len, m_target_periph_name) || ble_advdata_name_find(p_adv_report->data.p_data, parsed_name_len, DEVICE_NAME))    
        {
            scan_stop();
            // Name is a match, initiate connection.
            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)
                {
    #ifdef DEBUG 		
                    printf("Connection Request Failed, reason %d \r\n", err_code);
    #endif			
                }
                else
                {
    #ifdef DEBUG 		
                    printf("\r\n P name : ");
                    for(int i  = 0 ; i < parsed_name_len; i++)
                    {
                        printf("%c", buf[i]);
                    }
                    printf("\r\n");
                    printf("Connection Request SUCESSED, reason %d \r\n", err_code);
    #endif            
                }
    
            }
        }
        else
        {
    
            scan_stop();
            scan_start();
    
        }
        
    }

    The difference from the previous code is 'ble_advdata_name_find()'.
    [before]
    ble_advdata_name_find(p_adv_report->data.p_data, p_adv_report->data.len, m_target_periph_name)

    [after]
    ble_advdata_name_find(p_adv_report->data.p_data, parsed_name_len, m_target_periph_name)

    When I printed 'p_adv_report->data.len', the length of the device name did not match.
    Therefore, the length of the device name was obtained using ble_advdata_search() and then debugged.


    Now I have to go back to the original purpose and check if the BLE connection is made with a peripheral device with a different name.
    I don't know if it's a MAC address problem or the ad packet of the peripheral is broken, but I'll try to solve this problem.

Reply
  • Thanks for your reply.
    I can check the log and I tried it.

    Before that I found a solution.
    Through many posts, I found information that the peripheral device name can be obtained by using ble_advdata_parse() or ble_advdata_search() from BLE_GAP_EVT_ADV_REPORT of 'static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)'.

    However, when I actually debugged, I don't know why, but the next operation of 'ble_advdata_parse()' did not proceed.

    So I got the peripheral name from on_adv_report().

    This is my code.

    static void on_adv_report(ble_gap_evt_adv_report_t const * p_adv_report)
    {
        ret_code_t err_code;
        uint16_t data_offset = 0;
        int parsed_name_len = ble_advdata_search(p_adv_report->data.p_data, p_adv_report->data.len, &data_offset, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME);
        uint8_t *buf = ble_advdata_parse(p_adv_report->data.p_data, p_adv_report->data.len, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME);
    
        if ( ble_advdata_name_find(p_adv_report->data.p_data, parsed_name_len, m_target_periph_name) || ble_advdata_name_find(p_adv_report->data.p_data, parsed_name_len, DEVICE_NAME))    
        {
            scan_stop();
            // Name is a match, initiate connection.
            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)
                {
    #ifdef DEBUG 		
                    printf("Connection Request Failed, reason %d \r\n", err_code);
    #endif			
                }
                else
                {
    #ifdef DEBUG 		
                    printf("\r\n P name : ");
                    for(int i  = 0 ; i < parsed_name_len; i++)
                    {
                        printf("%c", buf[i]);
                    }
                    printf("\r\n");
                    printf("Connection Request SUCESSED, reason %d \r\n", err_code);
    #endif            
                }
    
            }
        }
        else
        {
    
            scan_stop();
            scan_start();
    
        }
        
    }

    The difference from the previous code is 'ble_advdata_name_find()'.
    [before]
    ble_advdata_name_find(p_adv_report->data.p_data, p_adv_report->data.len, m_target_periph_name)

    [after]
    ble_advdata_name_find(p_adv_report->data.p_data, parsed_name_len, m_target_periph_name)

    When I printed 'p_adv_report->data.len', the length of the device name did not match.
    Therefore, the length of the device name was obtained using ble_advdata_search() and then debugged.


    Now I have to go back to the original purpose and check if the BLE connection is made with a peripheral device with a different name.
    I don't know if it's a MAC address problem or the ad packet of the peripheral is broken, but I'll try to solve this problem.

Children
No Data
Related