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

NON_CONNECTABLE_ADV_INTERVAL

I'm trying to change the advertising interval for a non connectable BLE device, but this does not seem to vary regardless of the value set.

The code I used is:

#define NON_CONNECTABLE_ADV_INTERVAL    MSEC_TO_UNITS(10000, UNIT_0_625_MS)

...

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; 
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.

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

The sd_ble_gap_adv_start seems to work correctly (return code 0x00) 

but any interval value I insert the advertising interval does not seem to vary ..

for example, using:

#define NON_CONNECTABLE_ADV_INTERVAL    MSEC_TO_UNITS(200, UNIT_0_625_MS)

or:

#define NON_CONNECTABLE_ADV_INTERVAL    MSEC_TO_UNITS(10000, UNIT_0_625_MS)

I get the same behavior

Parents Reply Children
  • Hi, I am using SDK_14.2.0 and I took inspiration from the example you refer to

    I'm evaluating the advertising time by looking at the received packets on a Linux system. In this way definitly I can not get a precise measure of the times but surely setting the maximum value that should correspond to an interval of 10.24S the interval is actually less than 2 sec; even considering a gross uncertainty of a second it is not possible for it to be araund 10 seconds.

  • Ok.

    Could you include your entire main file by using the insert code option. I've checked the example in the SDK and experience a change in advertising interval when i change the NON_CONNECTABLE_ADV_INTERVAL

  • #include <stdbool.h>
    #include <stdint.h>
    
    #define CONFIG_NFCT_PINS_AS_GPIOS 
    
    #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_gpio.h"
    
    //----------------------------------------
    #include "app_button.h"
    #define BUTTON_DETECTION_DELAY          APP_TIMER_TICKS(50)                     /**< Delay from a GPIOTE event until a button is reported as pushed (in number of timer ticks). */
    
    //----------------------------------------
    
    #define APP_BLE_CONN_CFG_TAG            1                                 /**< A tag identifying the SoftDevice BLE configuration. */
    
    #define NON_CONNECTABLE_ADV_INTERVAL    MSEC_TO_UNITS(400, 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               0x37                              /**< The Beacon's measured RSSI at 1 meter distance in dBm. ORIGINAL 0xC3*/
    #define APP_COMPANY_IDENTIFIER          0x0059                            /**< Company identifier for Nordic Semiconductor ASA. as per www.bluetooth.org. */
    #define APP_MAJOR_VALUE                 0x00, 0x0B                        /**< Major value used to identify Beacons. */
    #define APP_MINOR_VALUE                 0x04, 0x05                        /**< Minor value used to identify Beacons. */
     /* 0x45, 0x56, 0x67, 0x78, \ */
    #define APP_BEACON_UUID                 0x00, 0x00, 0x00, 0x00, \
                                            0x00, 0x00, 0x00, 0x31, \
                                            0x00, 0x13, 0x24, 0x31, \
                                            0xcd, 0xde, 0xef, 0xff            /**< 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
    
    static ble_gap_adv_params_t m_adv_params;                                 /**< Parameters to be passed to the stack when starting advertising. */
    
    uint8_t m_beacon_info[APP_BEACON_INFO_LENGTH] =                    /** 23 < Information advertised by the Beacon. */
    {
        APP_DEVICE_TYPE,     // 1- Manufacturer specific information. Specifies the device type in this
                             // implementation.
        APP_ADV_DATA_LENGTH, // 2 - Manufacturer specific information. Specifies the length of the
                             // manufacturer specific data in this implementation.
        APP_BEACON_UUID,     // 3-18 128 bit UUID value.
        APP_MAJOR_VALUE,     // 19-20 Major arbitrary value that can be used to distinguish between Beacons.
        APP_MINOR_VALUE,     // 21 - 22Minor arbitrary value that can be used to distinguish between Beacons.
        APP_MEASURED_RSSI    // 23 - Manufacturer specific information. The Beacon's measured TX power in
                             // this implementation.
    };
    
    
    
    #define IO_data 2
    #define BAT_data 4
    
    #define GBAT  6
    
    #define BUTTON_DELAY APP_TIMER_TICKS(2000)
    #define DO00_DELAY BUTTON_DELAY
    #define DO01_DELAY BUTTON_DELAY
    #define DO02_DELAY BUTTON_DELAY
    
    #define BAT_DELAY  APP_TIMER_TICKS(1000)
    
    
    APP_TIMER_DEF(my_timer_DI00);
    APP_TIMER_DEF(my_timer_DI01);
    APP_TIMER_DEF(my_timer_DI02);
    
    APP_TIMER_DEF(my_timer_BAT);
                         
    
    /**@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.
     */
    ble_advdata_t advdata;
    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;
    
    #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);
    
        // 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.
    }
    
    
    /**@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 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);
    }
    
    
    /**@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();
    
    
    }
    
    static void DO00_timeout_handler(void * p_context)
    {
        uint32_t             err_code;
    
        m_beacon_info[IO_data] ^= 0x01;
    
        nrf_gpio_pin_set(GBAT);
    
        sd_ble_gap_adv_stop();
        advertising_init();
        advertising_start();
        
    }
    static void DO01_timeout_handler(void * p_context)
    {
        uint32_t             err_code;
    
        m_beacon_info[IO_data] ^= 0x02;
    
        nrf_gpio_pin_clear(GBAT);
    
        sd_ble_gap_adv_stop();
        advertising_init();
        advertising_start();
        
    }
    static void DO02_timeout_handler(void * p_context)
    {
        uint32_t             err_code;
    
        m_beacon_info[IO_data] ^= 0x04;
    
        sd_ble_gap_adv_stop();
        advertising_init();
        advertising_start();
        
    }
    
    static void BAT_timeout_handler(void * p_context)
    {
        uint32_t             err_code;
    
        m_beacon_info[BAT_data] = nrf_gpio_pin_out_read(GBAT);
    
        sd_ble_gap_adv_stop();
        advertising_init();
        advertising_start();
        
    }
    
    /**@brief Function for initializing timers. */
    static void timers_init(void)
    {
        uint32_t             err_code;
    
        err_code = app_timer_init();
        APP_ERROR_CHECK(err_code);
    
    
        err_code = app_timer_create(&my_timer_DI00, APP_TIMER_MODE_SINGLE_SHOT , DO00_timeout_handler);
        APP_ERROR_CHECK(err_code);
    
        err_code = app_timer_create(&my_timer_DI01, APP_TIMER_MODE_SINGLE_SHOT , DO01_timeout_handler);
        APP_ERROR_CHECK(err_code);
    
        err_code = app_timer_create(&my_timer_DI02, APP_TIMER_MODE_SINGLE_SHOT , DO02_timeout_handler);
        APP_ERROR_CHECK(err_code);
    
        err_code = app_timer_create(&my_timer_BAT, APP_TIMER_MODE_REPEATED , BAT_timeout_handler);
        APP_ERROR_CHECK(err_code);
    
        err_code = app_timer_start(my_timer_BAT, BAT_DELAY, NULL);
        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 for handling events from the button handler module.
     *
     * @param[in] pin_no        The pin that the event applies to.
     * @param[in] button_action The button action (press/release).
     */
    static void button_event_handler(uint8_t pin_no, uint8_t button_action)
    {
        ret_code_t err_code;
       
    
        switch (pin_no)
        {
            case BSP_BUTTON_0:
                m_beacon_info[IO_data] |= 0x01;
                err_code = app_timer_start(my_timer_DI00, DO00_DELAY, NULL);
                APP_ERROR_CHECK(err_code);
    
    
                break;
            
            case BSP_BUTTON_1:
                m_beacon_info[IO_data] |= 0x02;
                err_code = app_timer_start(my_timer_DI01, DO01_DELAY, NULL);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BSP_BUTTON_2:
                m_beacon_info[IO_data] |= 0x04;
                err_code = app_timer_start(my_timer_DI02, DO02_DELAY, NULL);
                APP_ERROR_CHECK(err_code);
                break;
                         
    
            default:
                APP_ERROR_HANDLER(pin_no);
                break;
        }
    
        //advertising_stop();
        sd_ble_gap_adv_stop();
        advertising_init();
        advertising_start();
        
        //ble_advdata_set(&advdata, NULL);
    
        //err_code = ble_advdata_set(&advdata, NULL);
        //APP_ERROR_CHECK(err_code);
    
        //err_code = sd_ble_gap_adv_start(&m_adv_params, APP_BLE_CONN_CFG_TAG);
        //APP_ERROR_CHECK(err_code);
    }
    
    
    
    /**@brief Function for initializing the button handler module.
     */
    static void buttons_init(void)
    {
        ret_code_t err_code;
    
        /*
        NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
        *(uint32_t *)0x1000120C = 0xFFFFFFFE;
        NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
        */
    
        nrf_gpio_cfg_input	(9,  NRF_GPIO_PIN_PULLUP);
        nrf_gpio_cfg_input	(10, NRF_GPIO_PIN_PULLUP);
    
    
    
        //The array must be static because a pointer to it will be saved in the button handler module.
        static app_button_cfg_t buttons[] =
        {
            {BSP_BUTTON_0, false, BUTTON_PULL, button_event_handler},
            {BSP_BUTTON_1, false, BUTTON_PULL, button_event_handler},
            {BSP_BUTTON_2, false, BUTTON_PULL, button_event_handler}
        };
    
        err_code = app_button_init(buttons, sizeof(buttons) / sizeof(buttons[0]),
                                   BUTTON_DETECTION_DELAY);
        APP_ERROR_CHECK(err_code);
    
         nrf_gpio_cfg_output(GBAT);
         nrf_gpio_pin_clear(GBAT);
    
    
    }
    
    /**
     * @brief Function for application main entry.
     */
    int main(void)
    {
        // Initialize.
        
        log_init();
        timers_init();
        //leds_init();
    
    
        buttons_init(); //--------------------
        ble_stack_init();
        advertising_init();
        advertising_start();
        
    	
    	sd_ble_gap_tx_power_set(0); //accepted values are -40, -20, -16, -12, -8, -4, 0, and 4 dBm
    
        // Start execution.
        NRF_LOG_INFO("Beacon example started.");
    	
    	app_button_enable();
    	
    	//bsp_board_led_on(BSP_BOARD_LED_1);
    
        // Enter main loop.
        for (;; )
        {
         
            if (NRF_LOG_PROCESS() == false)
            {
                power_manage();
            }
        }
    }
    

    Thanks for the availability, attached the code, I also added sections for testing the timers and buttons, but the sections related to advertising, as you can see, are those that I had already included in the original message.

  • Hi again

    The reason that you don't see a change in the advertising interval is that you invoke a new advertising period by starting the timer my_timer_BAT in timers_init(void). I omitted the function call and saw a corresponding change in the interval period when i changed the  NON_CONNECTABLE_ADV_INTERVAL.

    Cheers

    Jared 

  • Hi Jared, many thanks ... 

    when you understand the problem it seems strange not to have noticed ... thank you so much

Related