I am using NCS V3.0.2 with a custom nRF52840 board, which functions as a BLE peripheral. The application that I am working on fully depends on the BLE connection, so the crucial point is the BLE advertisement. If the BLE advertisement fails, I need to retry it. If the issue persists, I need to restart the BLE stack to reset all BLE states and have a fresh start. The device restart is not an option here for me.
I saw that Zephyr has bt_disable() and bt_enable() APIs to perform the BLE stack restart operation. I have tried to implement the approach by taking reference from one of the samples in the Zephyr as mentioned below.
K_WORK_DELAYABLE_DEFINE(adv_work, ble_adv_handler);
static void ble_adv_handler(struct k_work *work)
{
int err = 0;
printk("Restarting BLE Module !!!\n");
err = bt_le_adv_stop();
if (err && err != -EALREADY) {
printk("bt_le_adv_stop failed (err %d)\n", err);
} else {
printk("Advertising stopped successfully\n");
}
k_msleep(500);
/// Disable the Bluetooth Subsystem
err = bt_disable();
if (err) {
printk("Bluetooth disable failed (err %d)\n", err);
return;
}
k_msleep(500);
/// Initialize the Bluetooth Subsystem
err = bt_enable(NULL);
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return;
}
k_msleep(500);
printk("Restarting BLE Module done!!!\n");
err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
if (err) {
printk("Advertising failed to start (err %d)\n", err);
return;
}
printk("Regular advertising started\n");
}
static void restart_ble_module(void)
{
k_work_schedule(&adv_work, K_MSEC(200));
}
But when I tried this approach, from the logs it shows the BLE restart got successful, but when it starts the advertisement, it returns with the error "Advertising failed to start (err -11)".
I am not sure about the exact cause of this error because, as you see in the above code, I first stopped the ongoing advertisement for safe side, and even after disabling the BLE, it still says "-EAGAIN", which means the process is already running.
Is it something related to the Softdevice in nRF where it did not support the BLE restart feature that Zephyr provides, or is there any configuration issue/mismatch that I missed?
Your support is really appreciated here.
Thank you,
Ankit.