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

how to read the target device name and address in s120,nrf51

hii,

i am working on ble project where i would want to read the name and address of a slave.The example i am looking at is "nrf51_sdk_v6_1_0_b2ec2e6\nrf51822\Board\nrf6310\s120\ble_app_multilink_central". In the main() you can see function definition for "on_ble_evt". On going through the example i found out that we get the target device name in adv_data.p_data( adv_data.p_data =p_ble_evt->evt.gap_evt.params.adv_report.data;). Then i edited the code like

ble_gap_evt_adv_report_t x;///added extra

adv_data.p_data =x.data;////instead of adv_data.p_data =p_ble_evt->evt.gap_evt.params.adv_report.data;///

it also points to the same location as before the code was edited,but i don't get the target device name now as expected in adv_data.p_data.The edited code is pasted below .why does this happen,ie, i can't access the device name even though i point to the same memory location 'data' using 'x',instead of 'p_ble_evt' (i realise that p_ble_evt is a passed parameter for the function).

please help

static void on_ble_evt(ble_evt_t * p_ble_evt) {

uint8_t *p;

uint32_t        err_code;

ble_gap_evt_adv_report_t x;

switch (p_ble_evt->header.evt_id)
{				
		
    case BLE_GAP_EVT_ADV_REPORT:
    {
      data_t adv_data;
      data_t type_data;

       ble_gap_evt_adv_report_t  x; 

        // Initialize advertisement report for parsing.
        adv_data.p_data =x.data;//p_ble_evt->evt.gap_evt.params.adv_report.data;
        adv_data.data_len = p_ble_evt->evt.gap_evt.params.adv_report.dlen;

        err_code = adv_report_parse(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
                                  &adv_data,
                                  &type_data);
        if (err_code != NRF_SUCCESS)
        {
            // Compare short local name in case complete name does not match.
            err_code = adv_report_parse(BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME,
                                      &adv_data,
                                      &type_data);
        }

        // Verify if short or complete name matches target.
        if ((err_code == NRF_SUCCESS) &&
           (0 == memcmp(TARGET_DEV_NAME,type_data.p_data,type_data.data_len)))
        {
            err_code = sd_ble_gap_scan_stop();
            if (err_code != NRF_SUCCESS)
            {
                APPL_LOG("[APPL]: Scan stop failed, reason %d\r\n", err_code);
            }

            err_code = sd_ble_gap_connect(&gs_hb_peripheral_address[0],
                                          &m_scan_param,
                                          &m_connection_param);
							               /* err_code = sd_ble_gap_connect(&p_ble_evt->evt.gap_evt.params.adv_report.\
                                          peer_addr,
                                          &m_scan_param,
                                          &m_connection_param);*/

            if (err_code != NRF_SUCCESS)
            {
                APPL_LOG("[APPL]: Connection Request Failed, reason %d\r\n", err_code);
            }
        }
        break;
    }
    
    default:
        break;
}

}

  • Hi

    Sorry for a very late response. Not sure if my response will be useful by now, but I will give it a try.

    The p_ble_evt->evt.gap_evt.params.adv_report.data struct contains the advertising data so if you comment that out you will not get any advertising data in adv_data.p_data.

    The complete local name is then parsed and put into type_data.

    I have quickly seen the name in the debugger with defining an array and copying the name into the array, with the following code:

            uint8_t complete_name[20];
    
            // Initialize advertisement report for parsing.
            adv_data.p_data = p_ble_evt->evt.gap_evt.params.adv_report.data;
            adv_data.data_len = p_ble_evt->evt.gap_evt.params.adv_report.dlen;
    
            err_code = adv_report_parse(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
                                      &adv_data,
                                      &type_data);
            memcpy(complete_name, type_data.p_data, type_data.data_len);
    

    Then if I set a breakpoint after the memcpy line and add complete_name to a watch window, then I can see the whole name in the watch window.

Related