Hi,
I just started developing a BLE Central device on a nRF52840 DK.
After successfully connect my DevKit with a phone acting as a peripheral (using nRF Connect Mobile), The DK was able to subscribe to some services I configured on the phone and receive the corresponding notifications.
Right now I'm trying to disconnect every BLE connections the DK has when a specific button is pressed.
I managed to init the button and to add a callback function on it. The function works great but when I'm trying to disconnect the peripheral, I got an error:
ASSERTION FAIL [err == 0] @ ZEPHYR_BASE/subsys/bluetooth/host/hci_core.c:306
k_sem_take failed with err -11
This is the part of my code that should disconnect every BLE connection that the central has:
static void disconnect(struct bt_conn *conn, void *data) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); printk("Disconnecting %s...\n", addr); int err = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); if (err) { printk("Failed disconnection %s\n", addr); } else { printk("Success\n"); } } void ble_disconnect_all(void) { bt_conn_foreach(BT_CONN_TYPE_LE, disconnect, NULL); }
The function ble_disconnect_all(void)
is called when the button1 is pressed, the printk("Disconnecting %s...\n", addr)
is actually printed but when the program called bt_conn_disconnect(conn, BT_HCI_REMOTE_USER_TERM_CONN)
, then I get the error above.
I look up to the hci_core.c
at the line 306. It's in the function called bt_hci_cmd_send_sync()
. In this function, k_sem_take(&sync_sem, HCI_CMD_TIMEOUT)
tried to take a semaphore, failed and then the BT_ASSERT_MSG()
triggered. The message appears immediately when I pressed the button, it doesn't wait the 10 seconds that is configure by default in HCI_CMD_TIMEOUT
.
Since this error appears, the program seems to be frozen. Nothing happen when I manually disconnect my phone (either by disabling the bluetooth or just by pressing the Disconnect option in nRF Connect Mobile). The only way I found to unfroze the device is by turning it off/on.
My setup
- Ubuntu 22.04 in WSL2 (Host OS is Windows 10)
- PlatformIO CLI, version 6.1.15
- Zephyr framework, version 2.20701.220422
Prj.conf:
CONFIG_BT=y
CONFIG_BT_DEVICE_NAME="BLE_test"
CONFIG_BT_CENTRAL=y
CONFIG_BT_GATT_CLIENT=y
CONFIG_CONSOLE=y
CONFIG_GPIO=y