advertising duration (zephyr)

Hi,

I would like to ask on where can I find or set the advertising duration or on how long the advertisement would last?

I am using zephyr, nrf52840DK. I want to be able to advertise for 30 seconds and then turn off the advertisement after 30 seconds.

Thanks,

oli

Parents
  • Hi Oli,

    Thank you for contacting DevZone at NordicSemi.

    As per your query, you want to advertise for 30 seconds and then you want to turn advertisement off.

    I would like to refer you to this excellent application example that has very good match with your requirements. It is called Periodic Advertising, and is located in /samples/bluetooth/periodic_adv in the Zephyr tree. (Link: Bluetooth: Periodic Advertising — Zephyr Project Documentation (nordicsemi.com) )

    Please build and flash it on your board, and see the output on the NRF terminal. As you explore this simple yet powerful example, you will see that:

    In the main() program:

    We have a while(true) loop / which means always do the following:

    • we are enabling extended advertisement
    • then we are doing periodic advertisement (3 times with 10 seconds delay)
    • we are disabling extended advertisement
    • delay for 10 seconds

    As this loop repeats forever, you have control over both of the loops and the delay timings. I hope you will like this and extend your project.

    Kind Regards,
    Naeem

  • Hi Naeem,

    Thank you for this very informative answer. So, just to clarify, if I will modify the sample, I just have to change the delays to 30 seconds instead of 10 seconds? 

    Thanks,

    oli

  • Hi Oli,

    You requirement is not very clear to me. If you want to advertise for 30 seconds then you also need to know how often you want to advertise. For example, we can advertise once every second within 30 second duration. Or we can also advertise 10 times in every second within 30 second duration. Therefore, first you need to know how often you need to advertise.

    In the example code, the outer while loop is running forever, which means we are doing advertising forever (called periodic advertising). If you want to advertise for only 30 seconds or so, then you don' need while loop. Consider the following code (this should replace the complete while loop). This code uses a for loop to advertise for 30 seconds where we advertise once a second (as you can see there is delay of 1 second, and the loop control variable limit is 30). 

    printk("Starting advertising for 30 seconds...");
    err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT);
    if (err) {
        printk("Failed to start extended advertising (err %d)\n", err);
        return;
    }
    printk("Done.\n");
    
    for (int i = 0; i < 30; i++) {
        k_sleep(K_SECONDS(1));
        mfg_data[2]++;
        printk("Set Periodic Advertising Data...");
        err = bt_le_per_adv_set_data(adv, ad, ARRAY_SIZE(ad));
        if (err) {
            printk("Failed (err %d)\n", err);
            return;
        }
        printk("done.\n");
    }
    
    printk("Done with 30 seconds advertising...\n");
    printk("Now stop extended advertising...");
    err = bt_le_ext_adv_stop(adv);
    if (err) {
        printk("Failed to stop extended advertising (err %d)\n", err);
        return;
    }
    printk("done.\n");
    

    I hope you can extend / update as per your needs.

    Regards,

    Naeem

  • Hi Naeem,

    As I understand, the above example code that you gave me is advertising once every second within the 30 seconds duration, is that right?

    Can you show me how to:

    advertise 10 times in every second within 30 second duration

    Thank you very much!

    Kind regards,

    oli

  • Hi Oli,

    Oli said:
    As I understand, the above example code that you gave me is advertising once every second within the 30 seconds duration, is that right?

    Yes, this is correct.

    Regards,

    Naeem

Reply Children
No Data
Related