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

beacon advertise interval

hello Nordic

i am working with nrf52832, sdk 6.0, softdevice s132 v7.0

i read around the forum that i can advertise a beacon packet every 20 ms 

but in the ble_app_beacon example the interval for the advertisement is written to be minimum 100ms ?? so where did that 20 ms go ?

however, since i don't know how to change the data of the message while advertising i put a timer handler that stops the advertise, update the data and start it again.

i put the timer to go every 50 ms and the advertisement interval every 50 ms i put a pin toggle every time i receive a ble from the address i am filtering..

1. the time is not fixed, maybe some messages are lost or maybe not sent, i don't know 

2. the intervals are larger then 50 ms .. more to the 100 ms  when they are sometimes fixed 

can you advice on this matter ?

i need to be sending changeable data in a beacon at list 30 times in one second

best regards

Ziv

Parents
  • Hello Ziv,

    i read around the forum that i can advertise a beacon packet every 20 ms 

    but in the ble_app_beacon example the interval for the advertisement is written to be minimum 100ms ?? so where did that 20 ms go ?

    I am not sure I follow your train of thoughts here - the lowest possible advertising interval is indeed 20 ms, but the beacon example provided in the SDK uses an advertising interval of between 100 and 200 ms, this is correct.
    The reason for this is that beacons do not usually transfer a whole lot of data, and as such it is more power efficient to not advertise with the lowest possible interval. You may of course change this in your own application, to use the 20 ms advertising interval.

    however, since i don't know how to change the data of the message while advertising i put a timer handler that stops the advertise, update the data and start it again.

    If you are using the advertising module you may instead use the _advdata_update function.
    However, the _advdata_update function has a known issue, which requires that you have two adv_data instances that you alternate between passing to update and updating.
    That is to say: you may not pass a single advdata instance to advertising start, update the same advdata instance and then provide it as the argument to _advdata_update.
    Instead, you will have to have two advdata instances, where you pass one as the argument to the _advdata_update function, and then you may update the other advdata instance, before calling _advdata_update again with the most recently updated(not currently in use) advdata instance.

    1. the time is not fixed, maybe some messages are lost or maybe not sent, i don't know 

    Advertisements are transfers without acknowledgments, and they may indeed be lost sometimes - there are no retransmissions because there is never any acknowledgements. This can happen more often if the devices are far apart, and if they are operating in a 2.4 GHz noisy environment. The time can not be completely fixed either, due to the random 10 ms delay that is added before every advertisement. This is part of the BLE specification and can not be removed.

    2. the intervals are larger then 50 ms .. more to the 100 ms  when they are sometimes fixed 

    Please elaborate on the exact timings you are seeing. How are you measuring this by the way, only the toggling pin, or do you also use the BLE Sniffer tool

    i need to be sending changeable data in a beacon at list 30 times in one second

    This is possible using beacon advertisements, since with a 20 ms advertising interval your worst case will be 30 ms between each advertisement, which again means that you will get more than 30 packets per second.
    However, you will need to keep in mind that advertisement packets may be lost. This is the biggest drawback of using advertisements to transfer data, and also what makes advertisement data transfer infeasible if you need to make sure every packet is received.
    In the latter case, you will need to establish a connection to ensure that every packet is received.

    Please let me know if anything still should be unclear,
     
    Best regards,
    Karl

  • hi Karl

    good to read from you 

    the thing i actually have to design is

    1. a software for 150 units that receive data (specific for each of them) from one broadcasting unit that is connected to a pc

    2. dLL for the beacon broadcasting gateway or dongle or whatever that is connected to the pc which is running some python app (will need some help with that later)

    regarding the first part:

    • I have no power saving issues since power will be given to all units all the time
    • I was going for broadcasting and beacon to avoid making connection with each unit which i think will take a lot of time on Acks and reconnecting with a different unit every time. I would have sent one big message to all units ,if it was possible, and maybe send it multiple times in a second just to make sure every unit got it.
    • So i was toggling myself between working on an extended beacon broadcast, to checking how much data i can transfer in the 31 byte message and how many times i can transfer it ,so that if one 31 byte message can contain data for 10 units, i have to see that i can send the message to all 150 units every one second
    You may of course change this in your own application, to use the 20 ms advertising interval.

    can i also control the scan interval, and if so how and is there some issues i need to take under consideration when doing that ?

    Instead, you will have to have two advdata instances, where you pass one as the argument to the _advdata_update function, and then you may update the other advdata instance, before calling _advdata_update again with the most recently updated(not currently in use) advdata instance.

    i could not find any _advdata_update function in the ble_advdata.h file. if or when i find the function you are referring to then do i need to stop the advertising and restart it like in the following code, and update in the middle or i can only update advdata in the timer handler and the broadcasting continue in its interval ?:

    sd_ble_gap_adv_stop(m_adv_handle);
        err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
        APP_ERROR_CHECK(err_code);
        err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
        APP_ERROR_CHECK(err_code);
        err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
        APP_ERROR_CHECK(err_code);

    This can happen more often if the devices are far apart, and if they are operating in a 2.4 GHz noisy environment

    if i am using the ADDRESS FILTER:

        err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_ADDR_FILTER, m_target_periph_addr); // Ziv
        APP_ERROR_CHECK(err_code);
    
        // Setting filters for scanning.
        err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_ADDR_FILTER, false); // Ziv
        APP_ERROR_CHECK(err_code);

    does it change something regarding noises or every message from any device is received and read all the way anyway  and only then is filtered ?

    How are you measuring this by the way

    i toggle a pin in the case of ' NRF_BLE_SCAN_EVT_FILTER_MATCH '  in the ' scan_event_handler(...) '

    i look at the pin in the scope in a 50ms for each square 

    Regarding the second thing:

    if you can recommend a gateway or a device, gateway or other that connects to a pc via usb and comes with a convenient API so i can wrap it and supply the customer with an easy API for the data needed to be transferred to all the 150 units via bl, that would be great. 

    i read some about the nrf52840 dongle but someone wrote that it has a lot of over had regarding softdevice and serialization if i recall and actually recommend using a designated device (for us price of such device is not an issue)

    hope to read from you soon 

    best regards

    Ziv

  • Hello Ziv,

    ziv123 said:
    which to my understanding copies all the defines and configs of one instance to the other  (not sure it is semantically correct to say it but you understood i belive)

    This might be the issue here, I do not think this is correct.
    What you are doing here is creating a shallow copy of the advdata struct. So, you are in fact just using the same single buffer.
    If you would like to check this, please try to print out the value of the advdata[advdata_idx] and advdata[advdata_next_idx] pointers following this line of code and see what they point to.
    If you are to do your updates like this, please ensure that you are making a deep copy of the advdata struct.
    Do this, and see if you then are able to update your advertising data using the _advdata_update function. I look forward to hearing if this resolves your issue!

    Please see this stackoverflow thread for a detailed explanation on shallow vs deep copying.

    Best regards,
    Karl 

  • hi Karl

    it did not solve (unless i did something wrong.. here is my code for init advertising with setting the 2 instances:

        // Build and set advertising data.
        memset(&advdata[0], 0, sizeof(advdata[0]));
        memset(&advdata[1], 0, sizeof(advdata[1]));
    
        advdata[0].name_type        = BLE_ADVDATA_NO_NAME;//BLE_ADVDATA_SHORT_NAME;//BLE_ADVDATA_NO_NAME;
        advdata[1].name_type        = BLE_ADVDATA_NO_NAME;
        advdata[0].flags            = flags;
        advdata[1].flags            = flags;
        advdata[0].p_manuf_specific_data = &manuf_specific_data;
        advdata[1].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.
    
        err_code = ble_advdata_encode(&advdata[0], m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
        APP_ERROR_CHECK(err_code);
        err_code = ble_advdata_encode(&advdata[1], m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
        APP_ERROR_CHECK(err_code);
    
        err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
        APP_ERROR_CHECK(err_code);
    }

    and here is the code for the timer handler:

    static void data_update_timer_handler(void* p_context)
    {
        ret_code_t err_code;
    //    err_code = sd_ble_gap_adv_stop(m_adv_handle);
    //    APP_ERROR_CHECK(err_code);
    
        static uint8_t i = 0;
        if( i >= SET_VALUES_ARRAY )
        {
            i = 0;
        }
        uint8_t idx = i++;
        m_beacon_info[12] = spin_angle[idx];
        m_beacon_info[13] = (spin_direction[idx] | speed[idx]);
        NRF_LOG_INFO("%d",m_beacon_info[12]);
    
        manuf_specific_data.data.p_data = (uint8_t *) m_beacon_info;
        manuf_specific_data.data.size   = APP_BEACON_INFO_LENGTH;
    
        if(0 == advdata_idx ) advdata_idx = 1;
        else if( 1 == advdata_idx) advdata_idx = 0;
    
        advdata[advdata_idx].p_manuf_specific_data = &manuf_specific_data;
        
        ble_advertising_advdata_update(&ble_adv ,&advdata[advdata_idx], NULL /*&advdata[advdata_next_idx]*/);
        //advdata_idx = advdata_next_idx;
    
    //    sd_ble_gap_adv_stop(m_adv_handle);
    //    err_code = ble_advdata_encode(&advdata[advdata_idx], m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
    //    APP_ERROR_CHECK(err_code);
    //    err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
    //    APP_ERROR_CHECK(err_code);
    //    err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
    //    APP_ERROR_CHECK(err_code);
    //    advdata_idx = advdata_next_idx;
    }

    it will be best if we would find out why this is not working but i wonder if it will not be easier to just download sdk 17, maybe there is such an example working in that sdk, of updating message data while advertising ?

    though i would prefer staying with sdk 16 for now and solv it but i am late on this project and pulled to other projects every few days so .. hopefuly you can help me with that

    best regards

    Ziv

  • Hello Ziv,

    Could you do an error check on the return value of the ble_advertising_advdata_update call?
    When you do not check the error code, you will not be made aware if the function fails - you should always check the returned error code.
    When checking the return value, are you getting any errors?
    Remember to define DEBUG in your preprocessor defines, to see the error output message in the log.

    Also, by calling ble_advdata_encode twice with the same destination you will override the data placed there by the first function upon return of the second function call.
    I would ask you to try this again, but this time - for simplicity - really differentiate the two buffers you are keeping. I.e separate them by name, to make things easier. Ideally, have zero overlap between the two namespaces and usage.

    ziv123 said:
    it will be best if we would find out why this is not working but i wonder if it will not be easier to just download sdk 17, maybe there is such an example working in that sdk, of updating message data while advertising ?

    There is unfortunately not such an example in the SDK 17, but using the _advdata_update function will be straight forward there - no need to keep two buffers.
    In essence, you can then just call the function with the updated advertising data, and have the new data begin advertising.
    You could modify the beacon example in SDK 17 to do this, to see for yourself how it is to use.

    ziv123 said:
    though i would prefer staying with sdk 16 for now and solv it but i am late on this project and pulled to other projects every few days so .. hopefuly you can help me with that

    I too hope that my above suggestions helps resolve this issue.

    Best regards,
    Karl

  • hi Karl 

    well it returns an error from here:

    if (p_advertising->initialized == false)
        {
            return NRF_ERROR_INVALID_STATE;
        }

    and indeed i did not initialize the :

    ble_advertising_t ble_adv;

    i asked about it before and then i didn't do it eventually.. but now after going over the links you sent, i am not sure what values should be in the second parameter of the init function:

    ble_advertising_init(&ble_adv, ???);

    may be you can help me understand the datasheet better ?

    best regards

    Ziv

  • Hello Ziv,

    ziv123 said:

    ble_advertising_init(&ble_adv, ???);

    may be you can help me understand the datasheet better ?

    The ble_advertising_init API Reference details what the second parameter must be, for the call to be successful. In essence, the second parameter is a ble_advertising_init_t struct, which contains all the information the module needs to start advertising, including config, event- and error-handler.

    Please let me know if there is anything in particular which is unclear in the documentation so I may explain.

    Best regards,
    Karl

Reply Children
  • hi Karl

    i guess i am doing something wrong cause the ble_advertising_init(..) falls in here:

     ret = sd_ble_gap_adv_set_configure(&p_advertising->adv_handle, NULL, &p_advertising->adv_params);
        VERIFY_SUCCESS(ret);
    

    the ret = 7, error, i can not find what goes in the sd_ble_gap_adv_set_configure(..) 

    also, it is not clear to me if i need to also use the 

    static void advertising_init(void) 

    and inside it call the ble_advertising_init(..) cause it seems there is and advdata initialization with in it

    anyway .. this is what i have tried

    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_adv_modes_config_t ble_adv_mode = true;
        //ble_advdata_manuf_data_t manuf_specific_data;
        ble_adv_init.config.ble_adv_fast_enabled = true;
    
        ble_adv_init.advdata.flags = flags;
        ble_adv_init.advdata.name_type = BLE_ADVDATA_NO_NAME;
        ble_adv_init.advdata.p_manuf_specific_data = &manuf_specific_data;
    
        //ble_adv_init.srdata = NULL;
        ble_adv_init.error_handler = NULL;
        ble_adv_init.evt_handler = NULL;
    
        ble_advertising_init(&ble_adv, &ble_adv_init);
        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
    
    ///ziv addition:
      //  static uint8_t i = 4;
      //  if( i >= SET_VALUES_ARRAY )
      //  {
      //      i = 0;
      //  }
       // uint8_t idx = i++;
       m_beacon_info[12] = spin_angle[3];
       m_beacon_info[13] = (spin_direction[3] | speed[3]);
    
    // ziv addition end  
    
        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_1, 0, sizeof(advdata_1));
        memset(&advdata_2, 0, sizeof(advdata_2));
    
        advdata_1.name_type        = BLE_ADVDATA_NO_NAME;//BLE_ADVDATA_SHORT_NAME;//BLE_ADVDATA_NO_NAME;
        advdata_2.name_type        = BLE_ADVDATA_NO_NAME;
        advdata_1.flags            = flags;
        advdata_2.flags            = flags;
        advdata_1.p_manuf_specific_data = &manuf_specific_data;
        advdata_2.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.
    
        err_code = ble_advdata_encode(&advdata_1, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
        APP_ERROR_CHECK(err_code);
        err_code = ble_advdata_encode(&advdata_2, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
        APP_ERROR_CHECK(err_code);
    
        err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
        APP_ERROR_CHECK(err_code);
    }
    

    hope you can tell me what i am doing wrong ???

    best regards

    Ziv

  • hi Karl

    another question on this matter, once we solve this, is there a way to update/change the advdata every time there is an advertise? meaning ,if advertising interval is 30ms, then after every transmit the advdata will change according to some handler without using a timer to call the advdata_update function ?

     best regards

    Ziv

  • it strange but when using the regular advertising_init() without using the ble_advertising_init(), and setting the timer to change only the ble_advdata_encode() like this:

    advdata_1.p_manuf_specific_data = &manuf_specific_data;
    //    err_code = sd_ble_gap_adv_stop(m_adv_handle);
    //    APP_ERROR_CHECK(err_code);
        err_code = ble_advdata_encode(&advdata_1, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
        app_uart_put(err_code);
        APP_ERROR_CHECK(err_code);

    without stopping and starting the advertising, then it seems to be working and changing.. i am not sure if it is a risky and unstable way and ii cant realy use the nrf app for testing it with advertising interval less then 500ms

    what do you think of that ?

    and also would appreciate answering the other 2 reply's :)  

    best regards

    Ziv

  • Hello Ziv,

    ziv123 said:
    i guess i am doing something wrong cause the ble_advertising_init(..) falls in here:

    Just in case I will mention that you are calling the init with no advertising data, please see the sd_ble_gap_adv_set_configure API Reference.

    ziv123 said:
    the ret = 7, error, i can not find what goes in the sd_ble_gap_adv_set_configure(..) 

    Have you defined DEBUG in your preprocessor defines?
    In that case, please use APP_ERROR_CHECK instead of VERIFY_SUCCESS - verify_success only check whether the passed argument is NRF_SUCCESS and if not it only returns the error code. APP_ERROR_CHECK calls the error event handler in the case that the error code is not NRF_SUCCESS. In the default case, we will then know what caused the error by looking in the log.
    You can define DEBUG in Segger Embedded Studios as shown in the included picture:

    ziv123 said:

    static void advertising_init(void) 

    and inside it call the ble_advertising_init(..) cause it seems there is and advdata initialization with in it

    I do not understand what you mean by this. ble_advertising_init is the init function for the Advertising Module. The general advertising_init function is doing more than just calling ble_advertising_init.

    ziv123 said:
    hope you can tell me what i am doing wrong ???

    Please call APP_ERROR_CHECK, with DEBUG defined, on the error code, so we may know which error is generated, and where it is generated.
    This will make debugging much easier. I suspect that you are providing an invalid argument to a function, or similar - this will easily be seen with the DEBUG define and APP_ERROR_CHECK.
    Furthermore, you should really always call APP_ERROR_CHECK on your functions that may return non-successfully, to ensure that you are made aware of their failing - and that you may handle it accordingly.

    ziv123 said:
    another question on this matter, once we solve this, is there a way to update/change the advdata every time there is an advertise? meaning ,if advertising interval is 30ms, then after every transmit the advdata will change according to some handler without using a timer to call the advdata_update function ?

    Yes, this is possible, as we have spoken about earlier. You will have to do this using Radio Notifications. Please see this ticket for further information.
    However, I must again mention that it is not guaranteed that advertisements are received by the other devices, so by transmitting data using advertisements you must anticipate some packet loss. There is also no retransmission on advertisements, so this might mean that your advertisement never is hear, and the data is lost - without any feedback.
    Based on your previous comments I am inclined to think that packet loss might be an issue in your project, and you might therefore want to send the advertisements multiple times, to increase the chance that they are received successfully.

    ziv123 said:

    without stopping and starting the advertising, then it seems to be working and changing.. i am not sure if it is a risky and unstable way and ii cant realy use the nrf app for testing it with advertising interval less then 500ms

    what do you think of that ?



    If you would like to monitor the BLE Traffic ( advertisements ) more closely, I highly recommend using the nRF Sniffer tool. It lets you see all advertisements and BLE communication that the Sniffer is able to intercept. It is a very powerful tool when developing with BLE.

    I am not sure that I understand you setup here. Do you mean that you have a timer that calls the _encode periodically?
    Have you abandoned the idea of using the _advdata_update function?

    Best regards,
    Karl

  • hi Karl 

    the radio library you are talking about, i could not find it in the sdk 16.0, the branch is empty 

    am i missing something ?

    best regards

    Ziv

Related