How to correctly disable BLE advertising?

Hi,

I've come across several posts on this topic, but they all seem to be older than two years. Unfortunately, most of them don't provide any useful information.

Currently, I've implemented a small code example, where I start BLE advertisement upon a button press using interrupts. I then start a timer, and upon its expiration, I stop the BLE advertisement.

I found out that you can not disable BLE, as it is not implemented on the low level, link. If run my code, I get this error: 

Upon examining the hci_core.c code, it becomes apparent that the k_sem_take() function yields a result of -11, corresponding to the error code EAGAIN.:

I know how to interpret it correctly, but to me it seems like the resource is not available, possibly due to timing restrictions. However, this shouldn't be the case, considering the timeout is set to 10 seconds (HCI_CMD_TIMEOUT = 10 sec). Any clarification or assistance on this matter would be greatly appreciated.

This is my code:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/gap.h>
#include <dk_buttons_and_leds.h>
bool is_running = false;
LOG_MODULE_REGISTER(test_ble, LOG_LEVEL_INF);
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
#define RUN_STATUS_LED DK_LED1
#define RUN_LED_BLINK_INTERVAL 1000
void my_timer_handler(struct k_timer *dummy);
void my_work_handler(struct k_work *work);
K_TIMER_DEFINE(my_timer, my_timer_handler, NULL);
K_WORK_DEFINE(my_work, my_work_handler);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

One thing that also bothers me is that the interrupt routine is triggered twice, even though I have only pressed the button once. Why is this the case?

Thanks for your help.