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

Advertising and Scanning

SDK 15.2 API 6.1

I am advertising with 1 nRF module and trying to scan with a second nRF module and connect.

the advertiser

/**@brief Function for initializing the Advertising functionality.
 */
static void advertising_init(void)
{
    ret_code_t             err_code;
    ble_advertising_init_t init;
    
    is_advertising = false;

    memset(&init, 0, sizeof(init));
    
    ble_advdata_manuf_data_t                  manuf_data; //Variable to hold manufacturer specific data
    uint8_t data[]                            = "S"; //Our data to advertise
    manuf_data.company_identifier             =  MANUFACTURER_COMPANY_ID;
    manuf_data.data.p_data                    = data;
    manuf_data.data.size                      = sizeof(data);
    init.advdata.p_manuf_specific_data        = &manuf_data;

    init.advdata.name_type               = BLE_ADVDATA_SHORT_NAME;
    init.advdata.short_name_len          = DEVICE_SHORT_NAME_LEN;
    init.advdata.include_appearance      = true;
    init.advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    init.advdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
    init.advdata.uuids_complete.p_uuids  = m_adv_uuids;

    init.config.ble_adv_fast_enabled  = true;
    init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
    init.config.ble_adv_fast_timeout  = APP_ADV_DURATION;

    init.evt_handler = on_adv_evt;

    err_code = ble_advertising_init(&m_advertising, &init);
    APP_ERROR_CHECK(err_code);

    ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
}

the scanner

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

    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_NOT_FOUND:
            NRF_LOG_INFO("event 0x%x found", p_scan_evt->scan_evt_id);
            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]
                      );
             tb_led_set_debug(TB_LED_ON);
            
         } break;

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

         default:
            break;
    }
}


/**@brief Function for initializing the scanning and setting the filters.
 */
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);
    
    err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_NAME_FILTER, m_target_periph_name);
    APP_ERROR_CHECK(err_code);

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

When i start the advertiser and engage the scanner, the switch statement in the scan_evt_handler for p_scan_evt->scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND

the name is established for the scanner and advertiser as follows

#define DEVICE_NAME_TLBX                "ABCDEF" 
//for scanner
static char const m_target_periph_name[] = DEVICE_NAME_TLBX;
//for advertiser
strcpy(device_name, DEVICE_NAME_TLBX);
...
err_code = sd_ble_gap_device_name_set(&sec_mode,
                                          device_name,
                                          strlen(device_name));
APP_ERROR_CHECK(err_code);
...
init.advdata.name_type               = BLE_ADVDATA_SHORT_NAME;
init.advdata.short_name_len          = DEVICE_SHORT_NAME_LEN;

How come i get evt not found error instead of connecting? Also why does the switch statement break when the event is event not found. The switch statement never enters default and it goes into a break point infinite loop. It never enters the NRF_BLE_SCAN_EVT_NOT_FOUND case.

  • Have you studied these two examples that should do what you want to do:

    peripheral: \examples\ble_peripheral\ble_app_blinky
    central: \examples\ble_central\ble_app_blinky_c

  • i have used the ble_app_hrs and ble_app_uart examples. I have examined the one you requested and i may have had a reverse of code lines. but now i am getting a connecting error NRF_BLE_SCAN_EVT_CONNECTING_ERROR. the errror returned is 18 but i cannot find what this code is for on the website.

    Reviewing the ble_app_blinky they have advertising parameters but ble_app_uart does not. what is the purpose of the paramters and why does one use and the other does not

    Also the blinky app also does ble_adv_encode whereas uart does not. What is reason

  • Looks like 18 is due to:

    #define NRF_ERROR_CONN_COUNT                  (NRF_ERROR_BASE_NUM + 18) ///< Maximum connection count exceeded.

    What is NRF_SDH_BLE_CENTRAL_LINK_COUNT set to? 

    dmleone said:
    Reviewing the ble_app_blinky they have advertising parameters but ble_app_uart does not. what is the purpose of the paramters and why does one use and the other does not

    Looks like the ble_app_uart use the softdevice api directly (and thereby need to encode data manually), while the ble_app_blinky use the advertisement module (which wrap around encoding of data and softdevice calls). I think using the advertisement module may be the preferred.

    Best regards,
    Kenneth

Related