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

Beacon Scanner Problem

Hello,

I have beacon advertiser and scanner. Scanner is scanning the devices from around. But its not scanning the device that i used for advertising. On the other side, NRF Connet application able to scan my advertising device. I have attached my Scanner and Advertising beacon code. Please, can you check why i am not getting the my advertising device on scanner side. Can you guide me to add the filter so it can scan only device from NRF and NRF Beacon.

Thanks

Scanner.cpp

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "nrf_sdh_soc.h"
#include "nrf_pwr_mgmt.h"
#include "ble.h"
#include "nrf_delay.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.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_DURATION                   0x0000                              /**< Timout when scanning. 0x0000 disables timeout. */
#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. */

static ble_gap_scan_params_t const m_scan_params =
{
    .extended = 0,
    .report_incomplete_evts = 0,
    .active   = 1,
    .filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL,
    .scan_phys = BLE_GAP_PHY_1MBPS,
    .interval = SCAN_INTERVAL,
    .window   = SCAN_WINDOW,
    .timeout  = SCAN_DURATION
};

static uint8_t m_scan_buffer_data[BLE_GAP_SCAN_BUFFER_MIN]; /**< buffer where advertising reports will be stored by the SoftDevice. */

static ble_data_t m_scan_buffer =
{
    m_scan_buffer_data,
    BLE_GAP_SCAN_BUFFER_MIN
};

static void scan_start(void)
{
    (void) sd_ble_gap_scan_stop();
    sd_ble_gap_scan_start(&m_scan_params, &m_scan_buffer);
}

uint8_t * adv_address = 0; int8_t rssi = 0; uint8_t * adv_data = 0;

static void on_adv_report(ble_gap_evt_adv_report_t const * p_adv_report)
{
    adv_address = (uint8_t *)p_adv_report->peer_addr.addr;
    rssi = (int8_t)p_adv_report->rssi;
    adv_data = (uint8_t *)p_adv_report->data.p_data;
    
    //if(adv_address[0] == 0x73 && adv_address[1] == 0xE8)
    //{
    printf("MAC ID");
    for(int i=0;i<6;i++)
    {
    printf(":%02X",adv_address[i]);
    }
    printf("\tDATA: ");
    for(int j=0;j<16;j++)
    {
    printf("%02X",adv_data[j]);
    }
    printf("\tRSSI: %d DB\n", rssi);
    nrf_delay_ms(1000);
    //}
}

static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;

    switch (p_ble_evt->header.evt_id)
    {

        case BLE_GAP_EVT_ADV_REPORT:
        {
            on_adv_report(&p_gap_evt->params.adv_report);

        } break;

        default:
          break;
    }
}

static void ble_stack_init(void)
{ 
  uint32_t ram_start = 0;
  nrf_sdh_enable_request();
  nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
  nrf_sdh_ble_enable(&ram_start);
  NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
}

int main(void)
{
    NRF_LOG_INIT(NULL);
    NRF_LOG_DEFAULT_BACKENDS_INIT();
    nrf_pwr_mgmt_init();
    ble_stack_init();
    
    while(1)
    {
      scan_start();
      NRF_LOG_FLUSH();
      nrf_pwr_mgmt_run();
    }
}
Adv_beacon.cpp

#include <stdbool.h>
#include <stdint.h>
#include "nordic_common.h"
#include "nrf_soc.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "ble_advdata.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"


#define APP_BLE_CONN_CFG_TAG            1                                  /**< A tag identifying the SoftDevice BLE configuration. */
#define NON_CONNECTABLE_ADV_INTERVAL    MSEC_TO_UNITS(100, UNIT_0_625_MS)  /**< The advertising interval for non-connectable advertisement (100 ms). This value can vary between 100ms to 10.24s). */
#define APP_BEACON_INFO_LENGTH          0x17                               /**< Total length of information advertised by the Beacon. */
#define APP_ADV_DATA_LENGTH             0x15                               /**< Length of manufacturer specific data in the advertisement. */
#define APP_DEVICE_TYPE                 0x02                               /**< 0x02 refers to Beacon. */
#define APP_MEASURED_RSSI               0xC3                               /**< The Beacon's measured RSSI at 1 meter distance in dBm. */
#define APP_COMPANY_IDENTIFIER          0x0059                             /**< Company identifier for Nordic Semiconductor ASA. as per www.bluetooth.org. */
#define APP_MAJOR_VALUE                 0x01, 0x02                         /**< Major value used to identify Beacons. */
#define APP_MINOR_VALUE                 0x03, 0x04                         /**< Minor value used to identify Beacons. */
#define APP_BEACON_UUID                 0x01, 0x12, 0x23, 0x34, \
                                        0x45, 0x56, 0x67, 0x78, \
                                        0x89, 0x9a, 0xab, 0xbc, \
                                        0xcd, 0xde, 0xef, 0xf0            /**< Proprietary UUID for Beacon. */

static ble_gap_adv_params_t m_adv_params;                                  /**< Parameters to be passed to the stack when starting advertising. */
static uint8_t              m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET; /**< Advertising handle used to identify an advertising set. */
static uint8_t              m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX];  /**< Buffer for storing an encoded advertising set. */

static ble_gap_adv_data_t m_adv_data =
{
    .adv_data =
    {
        .p_data = m_enc_advdata,
        .len    = BLE_GAP_ADV_SET_DATA_SIZE_MAX
    },
    .scan_rsp_data =
    {
        .p_data = NULL,
        .len    = 0

    }
};


static uint8_t m_beacon_info[APP_BEACON_INFO_LENGTH] =                    /**< Information advertised by the Beacon. */
{
    APP_DEVICE_TYPE,     // Manufacturer specific information. Specifies the device type in this
                         // implementation.
    //APP_ADV_DATA_LENGTH, // Manufacturer specific information. Specifies the length of the
                         // manufacturer specific data in this implementation.
    //APP_BEACON_UUID,     // 128 bit UUID value.
    //APP_MAJOR_VALUE,     // Major arbitrary value that can be used to distinguish between Beacons.
    //APP_MINOR_VALUE,     // Minor arbitrary value that can be used to distinguish between Beacons.
    //APP_MEASURED_RSSI    // Manufacturer specific information. The Beacon's measured TX power in
                         // this implementation.
};

static void advertising_init(void)
{
    uint32_t      err_code;
    ble_advdata_t advdata;
    uint8_t       flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
    ble_advdata_manuf_data_t manuf_specific_data;

    manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
    manuf_specific_data.data.p_data = (uint8_t *) m_beacon_info;
    manuf_specific_data.data.size   = APP_BEACON_INFO_LENGTH;

    // Build and set advertising data.
    memset(&advdata, 0, sizeof(advdata));

    advdata.name_type             = BLE_ADVDATA_NO_NAME;
    advdata.flags                 = flags;
    advdata.p_manuf_specific_data = &manuf_specific_data;

    // Initialize advertising parameters (used when starting advertising).
    memset(&m_adv_params, 0, sizeof(m_adv_params));

    m_adv_params.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
    m_adv_params.p_peer_addr     = NULL;    // Undirected advertisement.
    m_adv_params.filter_policy   = BLE_GAP_ADV_FP_ANY;
    m_adv_params.interval        = NON_CONNECTABLE_ADV_INTERVAL;
    m_adv_params.duration        = 0;       // Never time out.

    ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
    sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
}

static void advertising_start(void)
{
   sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
}

static void ble_stack_init(void)
{
   uint32_t ram_start = 0;
   nrf_sdh_enable_request();
   nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
   nrf_sdh_ble_enable(&ram_start);
}

static void log_init(void)
{
    NRF_LOG_INIT(NULL);
    NRF_LOG_DEFAULT_BACKENDS_INIT();
}

int main(void)
{
    log_init();
    nrf_pwr_mgmt_init();
    ble_stack_init();
    advertising_init();
    advertising_start();
    while(1)
    {
        nrf_pwr_mgmt_run();
    }
}

Parents
  • Hi

    I run the above code to scan the beacons but it doesn't receive any data packet. I see you checked the code and it seems it works for you. do you know what is the reason that I didn't receive any packets?

    Thank you.

  •  I do not remember the details of this old post, but based on the logs it seems like it worked for me out of the box with the given code snippet.

    On the later version of the softdevice the scanner stops automatically on the following events which is a change from older version

    /**@brief Start or continue scanning (GAP Discovery procedure, Observer Procedure).
     *
     * @note    A call to this function will require the application to keep the memory pointed by
     *          p_adv_report_buffer alive until the buffer is released. The buffer is released when the scanner is stopped
     *          or when this function is called with another buffer.
     *
     * @note    The scanner will automatically stop in the following cases:
     *           - @ref sd_ble_gap_scan_stop is called.
     *           - @ref sd_ble_gap_connect is called.
     *           - A @ref BLE_GAP_EVT_TIMEOUT with source set to @ref BLE_GAP_TIMEOUT_SRC_SCAN is received.
     *           - When a @ref BLE_GAP_EVT_ADV_REPORT event is received.
     *             In this case scanning is only paused to let the application access received data.
     *             The application must call this function to continue scanning, or call
     *             @ref sd_ble_gap_scan_stop to stop scanning.

    So when you get BLE_GAP_EVT_ADV_REPORT in the ble_evt_handler you should start the scanner again like below.

        switch (p_ble_evt->header.evt_id)
        {
    
            case BLE_GAP_EVT_ADV_REPORT:
            {
                on_adv_report(&p_gap_evt->params.adv_report);
                scan_start(); // <-- NEW ADDITION
    
            } break;

    Try to see if it works.

  • #include <stdint.h>
    #include <stdio.h>
    #include <string.h>
    #include "nrf_sdh.h"
    #include "nrf_sdh_ble.h"
    #include "nrf_sdh_soc.h"
    #include "nrf_pwr_mgmt.h"
    #include "ble.h"
    #include "nrf_delay.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.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_DURATION                   0x0000                              /**< Timout when scanning. 0x0000 disables timeout. */
    #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. */
    
    static ble_gap_scan_params_t const m_scan_params =
    {
        .extended = 0,
        .report_incomplete_evts = 0,
        .active   = 1,
        .filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL,
        .scan_phys = BLE_GAP_PHY_1MBPS,
        .interval = SCAN_INTERVAL,
        .window   = SCAN_WINDOW,
        .timeout  = SCAN_DURATION
    };
    
    static uint8_t m_scan_buffer_data[BLE_GAP_SCAN_BUFFER_MIN]; /**< buffer where advertising reports will be stored by the SoftDevice. */
    
    static ble_data_t m_scan_buffer =
    {
        m_scan_buffer_data,
        BLE_GAP_SCAN_BUFFER_MIN
    };
    
    static void scan_start(void)
    {
        (void) sd_ble_gap_scan_stop();
        sd_ble_gap_scan_start(&m_scan_params, &m_scan_buffer);
    }
    
    uint8_t * adv_address = 0; int8_t rssi = 0; uint8_t * adv_data = 0;
    
    static void on_adv_report(ble_gap_evt_adv_report_t const * p_adv_report)
    {
        adv_address = (uint8_t *)p_adv_report->peer_addr.addr;
        rssi = (int8_t)p_adv_report->rssi;
        adv_data = (uint8_t *)p_adv_report->data.p_data;
        
        //if(adv_address[0] == 0x73 && adv_address[1] == 0xE8)
        //{
        printf("MAC ID");
        for(int i=0;i<6;i++)
        {
        printf(":%02X",adv_address[i]);
        }
        printf("\tDATA: ");
        for(int j=0;j<16;j++)
        {
        printf("%02X",adv_data[j]);
        }
        printf("\tRSSI: %d DB\n", rssi);
        nrf_delay_ms(1000);
        //}
    }
    
    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
    {
        ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
    
        switch (p_ble_evt->header.evt_id)
        {
    
            case BLE_GAP_EVT_ADV_REPORT:
            {
                on_adv_report(&p_gap_evt->params.adv_report);
                scan_start(); // <-- NEW ADDITION
    
            } break;
    
            default:
              break;
        }
    }
    
    static void ble_stack_init(void)
    { 
      uint32_t ram_start = 0;
      nrf_sdh_enable_request();
      nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
      nrf_sdh_ble_enable(&ram_start);
      NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
    }
    
    int main(void)
    {
        NRF_LOG_INIT(NULL);
        NRF_LOG_DEFAULT_BACKENDS_INIT();
        nrf_pwr_mgmt_init();
        ble_stack_init();
        
        while(1)
        {
          scan_start();
          NRF_LOG_FLUSH();
          nrf_pwr_mgmt_run();
        }
    }

    Thank you for information.

    I changed the code as you said but again I did not get any result on the log screen.

Reply
  • #include <stdint.h>
    #include <stdio.h>
    #include <string.h>
    #include "nrf_sdh.h"
    #include "nrf_sdh_ble.h"
    #include "nrf_sdh_soc.h"
    #include "nrf_pwr_mgmt.h"
    #include "ble.h"
    #include "nrf_delay.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.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_DURATION                   0x0000                              /**< Timout when scanning. 0x0000 disables timeout. */
    #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. */
    
    static ble_gap_scan_params_t const m_scan_params =
    {
        .extended = 0,
        .report_incomplete_evts = 0,
        .active   = 1,
        .filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL,
        .scan_phys = BLE_GAP_PHY_1MBPS,
        .interval = SCAN_INTERVAL,
        .window   = SCAN_WINDOW,
        .timeout  = SCAN_DURATION
    };
    
    static uint8_t m_scan_buffer_data[BLE_GAP_SCAN_BUFFER_MIN]; /**< buffer where advertising reports will be stored by the SoftDevice. */
    
    static ble_data_t m_scan_buffer =
    {
        m_scan_buffer_data,
        BLE_GAP_SCAN_BUFFER_MIN
    };
    
    static void scan_start(void)
    {
        (void) sd_ble_gap_scan_stop();
        sd_ble_gap_scan_start(&m_scan_params, &m_scan_buffer);
    }
    
    uint8_t * adv_address = 0; int8_t rssi = 0; uint8_t * adv_data = 0;
    
    static void on_adv_report(ble_gap_evt_adv_report_t const * p_adv_report)
    {
        adv_address = (uint8_t *)p_adv_report->peer_addr.addr;
        rssi = (int8_t)p_adv_report->rssi;
        adv_data = (uint8_t *)p_adv_report->data.p_data;
        
        //if(adv_address[0] == 0x73 && adv_address[1] == 0xE8)
        //{
        printf("MAC ID");
        for(int i=0;i<6;i++)
        {
        printf(":%02X",adv_address[i]);
        }
        printf("\tDATA: ");
        for(int j=0;j<16;j++)
        {
        printf("%02X",adv_data[j]);
        }
        printf("\tRSSI: %d DB\n", rssi);
        nrf_delay_ms(1000);
        //}
    }
    
    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
    {
        ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
    
        switch (p_ble_evt->header.evt_id)
        {
    
            case BLE_GAP_EVT_ADV_REPORT:
            {
                on_adv_report(&p_gap_evt->params.adv_report);
                scan_start(); // <-- NEW ADDITION
    
            } break;
    
            default:
              break;
        }
    }
    
    static void ble_stack_init(void)
    { 
      uint32_t ram_start = 0;
      nrf_sdh_enable_request();
      nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
      nrf_sdh_ble_enable(&ram_start);
      NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
    }
    
    int main(void)
    {
        NRF_LOG_INIT(NULL);
        NRF_LOG_DEFAULT_BACKENDS_INIT();
        nrf_pwr_mgmt_init();
        ble_stack_init();
        
        while(1)
        {
          scan_start();
          NRF_LOG_FLUSH();
          nrf_pwr_mgmt_run();
        }
    }

    Thank you for information.

    I changed the code as you said but again I did not get any result on the log screen.

Children
No Data
Related