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

Not able to get the Advertisement packet.

I am working with a proximity beacon example. In that runs scanning and advertisement code, And in testing, most of the cases, I am not able to get the Advertisement packet even in 1.5sec from the other beacon. I have verified the connection parameter for advertisement and scanning both. Can anyone please help?
I am using Nrfsdk 14.01 and Nrf52810chipset.
I have attached the beacon_example file here.

/**
 * Copyright (c) 2014 - 2017, Nordic Semiconductor ASA
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 * 
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 * 
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 * 
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 * 
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
/** @file
 *
 * @defgroup ble_sdk_app_beacon_main main.c
 * @{
 * @ingroup ble_sdk_app_beacon
 * @brief Beacon Transmitter Sample Application main file.
 *
 * This file contains the source code for an Beacon transmitter sample application.
 */

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "nordic_common.h"
#include "bsp.h"
#include "nrf_soc.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "ble_advdata.h"
#include "app_timer.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_delay.h"
#include "math.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. */

#define DEAD_BEEF                       0xDEADBEEF                        /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */

#if defined(USE_UICR_FOR_MAJ_MIN_VALUES)
#define MAJ_VAL_OFFSET_IN_BEACON_INFO   18                                /**< Position of the MSB of the Major Value in m_beacon_info array. */
#define UICR_ADDRESS                    0x10001080                        /**< Address of the UICR register used by this example. The major and minor versions to be encoded into the advertising data will be picked up from this location. */
#endif

#define SCAN_DURATION_WITELIST          0x0000                              /**< Duration of the scanning in units of 10 milliseconds. */
#define NRF_BLE_SCAN_SCAN_INTERVAL		0x00A0
#define NRF_BLE_SCAN_SCAN_DURATION 		0
#define NRF_BLE_SCAN_SCAN_WINDOW		0x0050

#define BLE_GAP_ADV_SET_HANDLE_NOT_SET (0xFF)
  
#define APP_BLE_OBSERVER_PRIO           0

NRF_BLE_SCAN_DEF(m_scan);                                 /**< Scanning Module instance. */

static ble_gap_scan_params_t m_scan_param =               /**< Scan parameters requested for scanning and connection. */
{
    .active        = 1,
    .interval      = NRF_BLE_SCAN_SCAN_INTERVAL,
    .window        = NRF_BLE_SCAN_SCAN_WINDOW,
    .timeout       = SCAN_DURATION_WITELIST,
    #if (NRF_SD_BLE_API_VERSION <= 2)
        .selective   = 0,
        .p_whitelist = NULL,
    #endif
    #if (NRF_SD_BLE_API_VERSION >= 3)
        .use_whitelist = 0,
    #endif
};



#if 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.
};
#endif

/**@brief Callback function for asserts in the SoftDevice.
 *
 * @details This function will be called in case of an assert in the SoftDevice.
 *
 * @warning This handler is an example only and does not fit a final product. You need to analyze
 *          how your product is supposed to react in case of Assert.
 * @warning On assert from the SoftDevice, the system can only recover on reset.
 *
 * @param[in]   line_num   Line number of the failing ASSERT call.
 * @param[in]   file_name  File name of the failing ASSERT call.
 */
void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
{
    app_error_handler(DEAD_BEEF, line_num, p_file_name);
}


/**@brief Function for initializing the Advertising functionality.
 *
 * @details Encodes the required advertising data and passes it to the stack.
 *          Also builds a structure to be passed to the stack when starting advertising.
 */	
static void advertising_init(void)
{
    uint32_t      err_code;
    ble_advdata_t advdata;
    uint8_t       flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
	int8_t tx_power_level = -20;
    ble_advdata_manuf_data_t manuf_specific_data;

/*#if defined(USE_UICR_FOR_MAJ_MIN_VALUES)
    // If USE_UICR_FOR_MAJ_MIN_VALUES is defined, the major and minor values will be read from the
    // UICR instead of using the default values. The major and minor values obtained from the UICR
    // are encoded into advertising data in big endian order (MSB First).
    // To set the UICR used by this example to a desired value, write to the address 0x10001080
    // using the nrfjprog tool. The command to be used is as follows.
    // nrfjprog --snr <Segger-chip-Serial-Number> --memwr 0x10001080 --val <your major/minor value>
    // For example, for a major value and minor value of 0xabcd and 0x0102 respectively, the
    // the following command should be used.
    // nrfjprog --snr <Segger-chip-Serial-Number> --memwr 0x10001080 --val 0xabcd0102
    uint16_t major_value = ((*(uint32_t *)UICR_ADDRESS) & 0xFFFF0000) >> 16;
    uint16_t minor_value = ((*(uint32_t *)UICR_ADDRESS) & 0x0000FFFF);

    uint8_t index = MAJ_VAL_OFFSET_IN_BEACON_INFO;

    m_beacon_info[index++] = MSB_16(major_value);
    m_beacon_info[index++] = LSB_16(major_value);

    m_beacon_info[index++] = MSB_16(minor_value);
    m_beacon_info[index++] = LSB_16(minor_value);
#endif

    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;
	
	err_code = ble_advdata_set(&advdata, NULL);
    APP_ERROR_CHECK(err_code);
	
	err_code = sd_ble_gap_tx_power_set(tx_power_level);
	
    // Initialize advertising parameters (used when starting advertising).
    memset(&m_adv_params, 0, sizeof(m_adv_params));

    m_adv_params.type        = BLE_GAP_ADV_TYPE_ADV_NONCONN_IND;
    m_adv_params.p_peer_addr = NULL;    // Undirected advertisement.
    m_adv_params.fp          = BLE_GAP_ADV_FP_ANY;
    m_adv_params.interval    = NON_CONNECTABLE_ADV_INTERVAL;
    m_adv_params.timeout     = 0;       // Never time out.
	m_adv_params.channel_mask.ch_37_off = 0x00;
	m_adv_params.channel_mask.ch_38_off = 0xFF;
	m_adv_params.channel_mask.ch_39_off = 0xFF;
    //Advertising only on channel 37, Disables Channel 38 & 39
}

/**@brief Function for starting timer.
 */
static void timer_start(void)
{
    ret_code_t err_code;

	// Start application timers.
	APP_ERROR_CHECK(err_code);
}

/**@brief Function for starting advertising.
 */
static void advertising_start(void)
{
    ret_code_t err_code;

    err_code = sd_ble_gap_adv_start(&m_adv_params, APP_BLE_CONN_CFG_TAG);
    APP_ERROR_CHECK(err_code);

    err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
    APP_ERROR_CHECK(err_code);
}

/**@brief Function for handling BLE events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 * @param[in]   p_context   Unused.
 */
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)
    {
        // Upon connection, initiate secure bonding.
        case BLE_GAP_EVT_CONNECTED:
            NRF_LOG_INFO("Connected.");
            break;
			
        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO("Disconnected.");
            //LEDS_ON(BSP_LED_0_MASK);
			break;
		
		case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            break; // BLE_GAP_EVT_SEC_PARAMS_REQUEST
		
        case BLE_GAP_EVT_ADV_REPORT:{
            
		break;
			
        default:
            // No implementation needed.
            break;
    }
}

/**@brief Function for initializing the BLE stack.
 *
 * @details Initializes the SoftDevice and the BLE event interrupt.
 */
static void ble_stack_init(void)
{
    ret_code_t err_code;

    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);
	
	// Register a handler for BLE events.
    NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
}

/**@brief Function for initializing logging. */
static void log_init(void)
{
    ret_code_t err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();
}

/**@brief Function for initializing LEDs. */
static void leds_init(void)
{
	bsp_board_leds_init();
    //ret_code_t err_code = bsp_init(BSP_INIT_LED, NULL);
    //APP_ERROR_CHECK(err_code);
}

/**@brief Function for initializing timers. */
static void timers_init(void)
{
    ret_code_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);
	/*Create a timer instance*/

	APP_ERROR_CHECK(err_code);
}

/**@brief Function for doing power management. */
static void power_manage(void)
{
    ret_code_t err_code = sd_app_evt_wait();
    APP_ERROR_CHECK(err_code);
}

/**@brief Function to start scanning.
 */
static void scan_start(void)
{
    ret_code_t err_code;

    (void) sd_ble_gap_scan_stop();

    err_code = sd_ble_gap_scan_start(&m_scan_param);
    APP_ERROR_CHECK(err_code);
}

/**
 * @brief Function for application main entry.
 */
int main(void)
{
    // Initialize.
    log_init();
    timers_init();
    leds_init();
    ble_stack_init();
    advertising_init();
	advertising_start();
	timer_start();
	scan_start();
    // Start execution.
	
	NRF_LOG_INFO("Beacon example started.");
    
	// Enter main loop.
    for (;; )
    {
        if (NRF_LOG_PROCESS() == false)
        {
            power_manage();
        }
    }
}


/**
 * @}
 */

  • Hello,

    So you are scanning for the devices similar to itself, right? That is, is the packets that you are scanning for also advertised with 100ms advertising interval?

    I see that you use a scan interval and window of 0xA0 and 0x50, which translates to 1600ms and 800ms.

    This means you scan 800ms every 1600ms.

    This means that you should get about 7-8 advertisements for 800ms, and then no advertisements for 800ms, if you pick up all the advertisements.

    What you are probably seeing is that the scan window is not scheduled from time to time. Since there is only one physical radio on the nRF, the device can only scan or advertise at a given point in time. When you do concurrenty scanning or advertising the softdevice has to prioritize which one of these to do. The softdevice does only schedule entire scan windows. 

    The scheduler in the softdevice is dynamic, so if you skip a scan window, then the scan_window will have a higher priority in the future. If it skips an advertisement, the next advertising event will have a higher priority.

    So what you are seeing is that the sofdevice is skipping some scan windows, which will happen from time to time if you do concurrently advertising and scanning. If you use very long scan window/intervals, this means that you will loose a lot of advertisements. What you can try to do is to reduce the scan interval and scan window. If you use e.g. 100/50ms (interval: 0x64, window: 0x32), this means that you will still scan for 50% of the time (same current consumption), but if the softdevice skips a scan window, you will only miss 100ms of scanning. 

    If you reduce the scan window/interval too much, you will start to see another type of packet loss, because you start picking up only parts of packets, because the scan window will end mid-packet. One advertising packet containing 31 bytes takes about 0.7ms (which you can see here). So the packet/advertisement loss will increase if you reduce the scan window to only a couple of ms.

    One other thing to consider is that the nRF will not advertise if an advertising packet collides with a scan window, so you may want to use an advertising/scan interval that doesn't overlap for too long (each advertising packet has a 0-10ms random delay added to the advertising interval, according to the specification). E.g. an advertising interval of 100ms, I would recommend using a scan interval of about 60ms, so you don't miss too many advertisements in a row.

    Best regards,

    Edvin

  • Edvin,
    Thanks for your reply.
    Yes, scanning and advertising both concurrently runs, and in another beacon device also runs the same program.
    I have set NON_CONNECTABLE_ADV_INTERVAL = 100ms, SCAN_INTERVAL = 100(0x64),and SCAN_WINDOW = 50(0x32) in that too much packet loss, most of cases even in 1.5 sec also not getting advertised packet.

    I have also tested in NON_CONNECTABLE_ADV_INTERVAL = 50(0x32),SCAN_INTERVAL = 50(0x32),and SCAN_WINDOW = 25(0x19) in that result better than above configuration. but in some cases, it takes 900ms or 1 sec to get the advertised packet.

  • jaimin said:
    I have set NON_CONNECTABLE_ADV_INTERVAL = 100ms, SCAN_INTERVAL = 100(0x64),and SCAN_WINDOW = 50(0x32) in that too much packet loss, most of cases even in 1.5 sec also not getting advertised packet.

     I am sorry. I gave the incorrect advice. The scan interval and scan window is given in units of 0.625ms. Setting the scan interval to 100(0x64) will give a scan interval of 62.5ms.

    However, if you want to pick up more advertisements, you should set the advertising interval so that you have several chances to pick up an advertisement per scan window. Set the advertising inteval to 10ms or so, and then keep the scan interval and window at 50 and 25.

    BR,

    Edvin

Related