Ble_scan.h header file is not found while I`m working on the code below I`m searching for BLE scan code for nearby devices on PCA10040e board.

#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "app_error.h"
#include "ble_scan.h"
#include "ble_gap.h"

#define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
#define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */

#define SCAN_WINDOW 15000 /**< Scan window duration in milliseconds. */
#define SCAN_INTERVAL 12000 /**< Scan interval duration in milliseconds. */
#define SCAN_TIMEOUT 120000 /**< Scan timeout duration in milliseconds. */

#define SCAN_BUFFER_SIZE 10 /**< Maximum number of scan results to buffer. */

NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_scan_init, NULL);

static ble_scan_t m_scan;

static ble_gap_scan_params_t const m_scan_params =
{
.active = 1,
.interval = SCAN_INTERVAL,
.window = SCAN_WINDOW,
.timeout = SCAN_TIMEOUT,
.scan_phys = BLE_GAP_PHY_1MBPS,
.buffer = {SCAN_BUFFER_SIZE, 0},
};

/**@brief Scan event handler.
*
* @param[in] p_scan_evt Scan event.
*/
static void scan_evt_handler(scan_evt_t const * p_scan_evt)
{
switch (p_scan_evt->scan_evt_id)
{
case NRF_BLE_SCAN_EVT_CONNECTING_ERROR:
// Error while connecting to peer.
break;

case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT:
// The scan has timed out.
break;

case NRF_BLE_SCAN_EVT_FILTER_MATCH:
{
// A device has been found that matches the filter criteria.
// Extract the advertising report from the event.
nrf_ble_scan_evt_filter_match_t * p_filter_match = &p_scan_evt->data.filter_match;
nrf_ble_scan_evt_filter_match_t filter_match = *p_filter_match;

// Extract the advertising data from the report.
ble_gap_evt_adv_report_t * p_adv_report = &filter_match.p_adv_report;

// Check if the found device is the desired device.
if (is_desired_device(p_adv_report))
{
// The desired device has been found.
// Extract the MAC address of the device.
ble_gap_addr_t gap_address = p_adv_report->peer_addr;

// Print the MAC address of the device.
printf("MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
gap_address.addr[0], gap_address.addr[1], gap_address.addr[2],
gap_address.addr[3], gap_address.addr[4], gap_address.addr[5]);

// Print the RSSI value of the device.
printf("RSSI value: %d\n", p_adv_report->rssi);
}
break;
}
/**@brief Initialize the BLE scan module.
*/
void ble_scan_init(void)
{
ret_code_t err_code;

err_code = ble_scan_init(&m_scan, &m_scan_params, scan_evt_handler);
APP_ERROR_CHECK(err_code);
}

int main(void)
{
ret_code_t err_code = nrf_sdh_enable_request();
APP_ERROR_CHECK(err_code);

// Configure the BLE stack using the default settings.
// Fetch the start address of the application RAM.
uint32_t ram_start = 0;
err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
APP_ERROR_CHECK(err_code);

// Enable BLE stack.
err_code = nrf_sdh_ble_enable(&ram_start);
APP_ERROR_CHECK(err_code);

// Start BLE scanning.
err_code = ble_scan_start(&m_scan);
APP_ERROR_CHECK(err_code);

// Enter main loop.
for (;;)
{
// Wait for an event.
}
}   

  • Hi,

    If the compiler does not find the header file it means it is not in the include path. There is no ble_scan.h file in the SDK, though. Did you mean nrf_ble_scan.h? This is for the module (components/ble/nrf_ble_scan/).

    Also, remember that in order to scan you need to use a SoftDevice that supports the central role. See the overview of the SoftDevices.

  • Thanks Einer for answering, Basically the problem is that while I`m working on this code . There is no such example for BLE Scanning present in the examples and then when I switched to nrf_ble_scan.h . The Header source file are showing errors like this is my modified code - 

    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf.h"
    #include "nordic_common.h"
    #include "nrf_gpio.h"
    #include "boards.h"
    #include "nrf_ble_scan.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    #include "nrf_delay.h"
    #include "nrf_drv_rtc.h"
    #include "nrf_drv_clock.h"
    #include "nrf_drv_gpiote.h"
    #include "nrfx_saadc.h"
    #include "nrfx_ppi.h"
    #include "nrfx_timer.h"
    #include "nrfx_ppi.h"
    #include "nrfx_uarte.h"
    #include "nrfx_prs.h"

    #define SCAN_INTERVAL 0x00A0 // Determines scan interval in units of 0.625 millisecond.
    #define SCAN_WINDOW 0x0050 // Determines scan window in units of 0.625 millisecond.
    #define SCAN_TIMEOUT 0 // Timout when scanning. 0 means no timeout.

    #define APP_BLE_CONN_CFG_TAG 1 // A tag identifying the SoftDevice BLE configuration.

    NRF_BLE_SCAN_DEF(m_scan); // Scan module instance.

    static uint32_t ble_counter = 0; // Counter for the number of BLE devices found.

    /**
    * @brief Function for handling the BLE scan events.
    */
    static void scan_evt_handler(scan_evt_t const * p_scan_evt)
    {
    switch (p_scan_evt->scan_evt_id)
    {
    case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT:
    NRF_LOG_INFO("Scan timed out.");
    break;

    case NRF_BLE_SCAN_EVT_FILTER_MATCH:
    NRF_LOG_INFO("BLE device found! Address: 0x%x", p_scan_evt->params.filter_match.p_adv_report->peer_addr.addr[0]);
    ble_counter++;
    break;

    case NRF_BLE_SCAN_EVT_NOT_FOUND:
    NRF_LOG_INFO("No BLE device found that matches the scan filter.");
    break;

    default:
    // No implementation needed.
    break;
    }
    }

    /**
    * @brief Function for initializing the BLE scan module.
    */
    static void ble_scan_init(void)
    {
    ret_code_t err_code;

    nrf_ble_scan_init_t scan_init = {
    .scan_param = {
    .interval = SCAN_INTERVAL,
    .window = SCAN_WINDOW,
    .timeout = SCAN_TIMEOUT,
    .active = 1
    },
    .connect_if_match = false,
    .scan_evt_handler = scan_evt_handler
    };

    err_code = nrf_ble_scan_init(&m_scan, &scan_init);
    APP_ERROR_CHECK(err_code);
    }

    /**
    * @brief Function for starting the BLE scan module.
    */
    static void ble_scan_start(void)
    {
    ret_code_t err_code;

    err_code = nrf_ble_scan_start(&m_scan);
    APP_ERROR_CHECK(err_code);
    }

    int main(void)
    {
    ret_code_t err_code;

    // Initialize logging.
    err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    // Initialize the BLE stack.
    err_code = nrf_sdh_enable_request();
    APP_ERROR_CHECK(err_code);

    // Initialize the BLE scan module.
    nrf_ble_scan_init_t scan_init = {
    .scan_param = {
    .interval = SCAN_INTERVAL,
    .window = SCAN_WINDOW,
    .timeout = SCAN_TIMEOUT,
    .active = 1
    },
    .connect_if_match = false,
    .evt_handler = scan_evt_handler
    };
    err_code = nrf_ble_scan_init(&m_scan, &scan_init);
    APP_ERROR_CHECK(err_code);

    // Start scanning for nearby BLE devices.
    err_code = nrf_ble_scan_start(&m_scan);
    APP_ERROR_CHECK(err_code);

    while (true)
    {
    NRF_LOG_FLUSH();
    nrf_delay_ms(10);
    }
    }                                                              

    And the errors I`m getting in the nrf_ble_scan.h file associated with my code =            Rebuilding ‘ble_app_hrs_pca10040e_s112’ from solution ‘ble_app_hrs_pca10040e_s112’ in configuration ‘Release’
    Assembling ‘thumb_crt0.s’
    Compiling ‘bsp.c’
    Compiling ‘bsp_btn_ble.c’
    Compiling ‘nrf_hw_backend_init.c’
    Compiling ‘nrf_hw_backend_rng.c’
    Compiling ‘nrf_hw_backend_rng_mbedtls.c’
    Compiling ‘app_button.c’
    Compiling ‘app_error.c’
    Compiling ‘app_error_handler_gcc.c’
    Compiling ‘app_error_weak.c’
    Compiling ‘app_scheduler.c’
    Compiling ‘app_timer2.c’
    Compiling ‘app_util_platform.c’
    Compiling ‘crc16.c’
    Compiling ‘drv_rtc.c’
    Compiling ‘fds.c’
    Compiling ‘hardfault_implementation.c’
    Compiling ‘mem_manager.c’
    Compiling ‘nrf_assert.c’
    Compiling ‘nrf_atfifo.c’
    Compiling ‘nrf_atflags.c’
    Compiling ‘nrf_atomic.c’
    Compiling ‘nrf_balloc.c’
    Compiling ‘nrf_fprintf.c’
    Compiling ‘nrf_fprintf_format.c’
    Compiling ‘nrf_fstorage.c’
    Compiling ‘nrf_fstorage_sd.c’
    Compiling ‘nrf_memobj.c’
    Compiling ‘nrf_pwr_mgmt.c’
    Compiling ‘nrf_queue.c’
    Compiling ‘nrf_ringbuf.c’
    Compiling ‘nrf_section_iter.c’
    Compiling ‘nrf_sortlist.c’
    Compiling ‘nrf_strerror.c’
    Compiling ‘sensorsim.c’
    Compiling ‘nrf_drv_clock.c’
    Compiling ‘nrf_drv_rng.c’
    Compiling ‘nrf_drv_uart.c’
    Compiling ‘nrfx_atomic.c’
    Compiling ‘nrfx_clock.c’
    Compiling ‘nrfx_gpiote.c’
    Compiling ‘nrfx_prs.c’
    Compiling ‘nrfx_rng.c’
    Compiling ‘nrfx_uart.c’
    Compiling ‘nrfx_uarte.c’
    Compiling ‘boards.c’
    Compiling ‘nrf_log_backend_rtt.c’
    Compiling ‘nrf_log_backend_serial.c’
    Compiling ‘nrf_log_backend_uart.c’
    Compiling ‘nrf_log_default_backends.c’
    Compiling ‘nrf_log_frontend.c’
    Compiling ‘nrf_log_str_formatter.c’
    Compiling ‘aes.c’
    Compiling ‘ctr_drbg.c’
    Compiling ‘platform_util.c’
    Compiling ‘nrf_crypto_aead.c’
    Compiling ‘nrf_crypto_aes.c’
    Compiling ‘nrf_crypto_aes_shared.c’
    Compiling ‘nrf_crypto_ecc.c’
    Compiling ‘nrf_crypto_ecdh.c’
    Compiling ‘nrf_crypto_ecdsa.c’
    Compiling ‘nrf_crypto_eddsa.c’
    Compiling ‘nrf_crypto_error.c’
    Compiling ‘nrf_crypto_hash.c’
    Compiling ‘nrf_crypto_hkdf.c’
    Compiling ‘nrf_crypto_hmac.c’
    Compiling ‘nrf_crypto_init.c’
    Compiling ‘nrf_crypto_rng.c’
    Compiling ‘nrf_crypto_shared.c’
    Compiling ‘main.c’
    main.c
    expected specifier-qualifier-list before 'ble_gap_scan_params_t'
    expected specifier-qualifier-list before 'ble_gap_evt_adv_report_t'
    expected specifier-qualifier-list before 'ble_gap_evt_adv_report_t'
    expected specifier-qualifier-list before 'ble_gap_scan_params_t'
    unknown type name 'ble_gap_scan_params_t'
    'NRF_BLE_SCAN_BUFFER' undeclared here (not in a function); did you mean 'NRF_BLE_SCAN_DEF'?
    unknown type name 'ble_gap_scan_params_t'; did you mean 'ble_gap_sec_params_t'?
    app_pwm.h
    nrf_timer.h
    nrfx_timer.h
    main.c
    unknown type name 'nrfx_timer_t'
    unknown type name 'nrfx_timer_config_t'
    implicit declaration of function 'NRFX_TIMER_INSTANCE'; did you mean 'NRF_DRV_TIMER_INSTANCE'? [-Wimplicit-function-declaration]
    in expansion of macro 'NRF_DRV_TIMER_INSTANCE'
    in expansion of macro 'APP_PWM_INSTANCE'
    initializer element is not constant
    in expansion of macro 'NRF_DRV_TIMER_INSTANCE'
    in expansion of macro 'APP_PWM_INSTANCE'
    main.c
    unknown type name 'nrf_timer_frequency_t'
    unknown type name 'nrf_timer_mode_t'
    unknown type name 'nrf_timer_bit_width_t'
    unknown type name 'nrf_timer_event_t'; did you mean 'nrf_saadc_event_t'?
    unknown type name 'nrfx_timer_event_handler_t'; did you mean 'nrf_timer_event_handler_t'?
    unknown type name 'nrf_timer_task_t'; did you mean 'nrf_ppi_task_t'?
    unknown type name 'nrf_timer_event_t'; did you mean 'nrf_saadc_event_t'?
    unknown type name 'nrf_timer_cc_channel_t'; did you mean 'nrf_ppi_channel_t'?
    unknown type name 'nrf_timer_cc_channel_t'; did you mean 'nrf_ppi_channel_t'?
    unknown type name 'nrf_timer_cc_channel_t'; did you mean 'nrf_ppi_channel_t'?
    unknown type name 'nrf_timer_cc_channel_t'; did you mean 'nrf_ppi_channel_t'?
    unknown type name 'nrf_timer_short_mask_t'
    unknown type name 'nrf_timer_task_t'; did you mean 'nrf_ppi_task_t'?
    implicit declaration of function 'nrf_timer_task_address_get'; did you mean 'nrf_drv_timer_task_address_get'? [-Wimplicit-function-declaration]
    implicit declaration of function 'nrf_timer_capture_task_get'; did you mean 'nrfx_timer_capture_task_address_get'? [-Wimplicit-function-declaration]
    unknown type name 'nrf_timer_event_t'; did you mean 'nrf_saadc_event_t'?
    implicit declaration of function 'nrf_timer_event_address_get'; did you mean 'nrf_drv_timer_event_address_get'? [-Wimplicit-function-declaration]
    implicit declaration of function 'nrf_timer_compare_event_get'; did you mean 'nrf_rtc_compare_event_get'? [-Wimplicit-function-declaration]
    unknown type name 'nrf_timer_cc_channel_t'; did you mean 'nrf_ppi_channel_t'?
    implicit declaration of function 'nrf_timer_us_to_ticks'; did you mean 'nrfx_timer_us_to_ticks'? [-Wimplicit-function-declaration]
    implicit declaration of function 'nrf_timer_frequency_get' [-Wimplicit-function-declaration]
    implicit declaration of function 'nrf_timer_ms_to_ticks'; did you mean 'nrfx_timer_ms_to_ticks'? [-Wimplicit-function-declaration]
    nrfx_config.h
    nrfx.h
    nrf_gpio.h
    main.c
    expected ')' before numeric constant
    in expansion of macro 'NRF_BLE_SCAN_OBSERVER_PRIO'
    in expansion of macro 'NRF_BLE_SCAN_DEF'
    nrf_log.h
    main.c
    'nrf_ble_scan_evt_filter_match_t' has no member named 'p_adv_report'
    in definition of macro 'LOG_INTERNAL_1'
    in expansion of macro 'LOG_INTERNAL_X'
    in expansion of macro 'LOG_INTERNAL'
    in expansion of macro 'NRF_LOG_INTERNAL_MODULE'
    in expansion of macro 'NRF_LOG_INTERNAL_INFO'
    in expansion of macro 'NRF_LOG_INFO'
    'nrf_ble_scan_init_t' has no member named 'scan_param'
    extra brace group at end of initializer
    (near initialization for 'scan_init')
    excess elements in struct initializer
    (near initialization for 'scan_init')
    'nrf_ble_scan_init_t' has no member named 'connect_if_match'
    excess elements in struct initializer
    (near initialization for 'scan_init')
    'nrf_ble_scan_init_t' has no member named 'scan_evt_handler'
    excess elements in struct initializer
    (near initialization for 'scan_init')
    too few arguments to function 'nrf_ble_scan_init'
    main.c
    declared here
    redefinition of 'main'
    nrfx_timer.h
    main.c
    previous definition of 'main' was here
    implicit declaration of function 'nrf_sdh_enable_request' [-Wimplicit-function-declaration]
    'nrf_ble_scan_init_t' has no member named 'scan_param'
    extra brace group at end of initializer
    (near initialization for 'scan_init')
    excess elements in struct initializer
    (near initialization for 'scan_init')
    'nrf_ble_scan_init_t' has no member named 'connect_if_match'
    excess elements in struct initializer
    (near initialization for 'scan_init')
    'nrf_ble_scan_init_t' has no member named 'evt_handler'
    excess elements in struct initializer
    (near initialization for 'scan_init')
    too few arguments to function 'nrf_ble_scan_init'
    main.c
    declared here
    Compiling ‘SEGGER_RTT.c’
    Compiling ‘SEGGER_RTT_Syscalls_SES.c’
    Compiling ‘SEGGER_RTT_printf.c’
    Assembling ‘ses_startup_nrf52810.s’
    Assembling ‘ses_startup_nrf_common.s’
    Compiling ‘system_nrf52810.c’
    Compiling ‘auth_status_tracker.c’
    Compiling ‘ble_advdata.c’
    Compiling ‘ble_advertising.c’
    Compiling ‘ble_conn_params.c’
    Compiling ‘ble_conn_state.c’
    Compiling ‘ble_srv_common.c’
    Build failed

  • Hi,

    Anshajm9 said:
    There is no such example for BLE Scanning

    All central examples use scanning. Which exactly is it you are not finding in the examples?

    Anshajm9 said:
    And the errors I`m getting in the nrf_ble_scan.h file associated with my code

    Look at the top-most errors first. These are of the type unknown, undeclared. Take this:

    unknown type name 'ble_gap_scan_params_t'

    ble_gap_scan_params_t is defined in ble_gap.h. But only for SoftDevices that actually support the central role. Which SoftDevice are you using? And critically, which SoftDevice is in your project's include path? If it is one of the non-central SoftDevices, then the errors you are getting are expected. If you still are using S112 headers this is expected, as S112 does not support the central role (including no scanning).

    PS: Please use Insert -> Code for copy pasting code as that is much more readable.

  • Einar , I`m using nRF 52810, PCA 10040e board and before I was trying for soft device s112 . Can you please tell me what changes do we need to make in this code to make it implementable . Or How could I write a code for BLE Scanner that scans all nearby ble devices and dumps complete data on RTT viewer.

    Thanks for helping.

  • Anshajm9 said:
    Can you please tell me what changes do we need to make in this code to make it implementable .

    You need to move to a SoftDevice that support the central role. Please refer to the table I linked to in my initial post to see which that are.

    In practice, you need to change the include path of the SoftDevice headers to the path to the headers for the new SoftDevice. And change the application start address to the size of the new SoftDevice (you can see this in the release notes of the new SoftDevice or refer to an example project that use that SoftDevice. And lastly, update the processor definition for the SoftDevice in your project configuration (now you have a S112, change that to whatever the new SoftDevice is).

    Anshajm9 said:
    Or How could I write a code for BLE Scanner that scans all nearby ble devices and dumps complete data on RTT viewer.

    Firs to of all, you must pick a SoftDevice that actually make this possible. Secondly,  take a look at a central example for how to do scanning. Essentially just remove most of the sample related to services etc, and scan without any filter. See this post for more details. You could also look at this old post which use SoftDevice APIs directly.

Related