Skip to content

samples: bluetooth: fix no ble advertising after disconnecting #90441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions samples/bluetooth/peripheral/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
printk("Disconnected, reason 0x%02x %s\n", reason, bt_hci_err_to_str(reason));
}

static void recycled(void) {
printk("connection recycled. Restart advertising a connection");
const int 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);
}
}

static void alert_stop(void)
{
printk("Alert stopped\n");
Expand All @@ -272,6 +280,7 @@ static void alert_high_start(void)
BT_CONN_CB_DEFINE(conn_callbacks) = {
.connected = connected,
.disconnected = disconnected,
.recycled = recycled,
};

BT_IAS_CB_DEFINE(ias_callbacks) = {
Expand Down
61 changes: 40 additions & 21 deletions samples/bluetooth/peripheral_hr/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ static ATOMIC_DEFINE(state, 2U);
#define STATE_CONNECTED 1U
#define STATE_DISCONNECTED 2U

static int start_adv(void);

static void connected(struct bt_conn *conn, uint8_t err)
{
if (err) {
Expand All @@ -61,9 +63,18 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
(void)atomic_set_bit(state, STATE_DISCONNECTED);
}

static void recycled(void) {
printk("connection recycled. Restart advertising a connection");
const int err = start_adv();
if (err) {
printk("Advertising failed to start (err %d)\n", err);
}
}

BT_CONN_CB_DEFINE(conn_callbacks) = {
.connected = connected,
.disconnected = disconnected,
.recycled = recycled,
};

static void hrs_ntf_changed(bool enabled)
Expand Down Expand Up @@ -186,28 +197,13 @@ static void blink_stop(void)
#endif /* LED0_NODE */
#endif /* CONFIG_GPIO */

int main(void)
{
int err;

err = bt_enable(NULL);
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return 0;
}

printk("Bluetooth initialized\n");

bt_conn_auth_cb_register(&auth_cb_display);

bt_hrs_cb_register(&hrs_cb);

static int start_adv(void) {
#if !defined(CONFIG_BT_EXT_ADV)
printk("Starting Legacy Advertising (connectable and scannable)\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 0;
return err;
}

#else /* CONFIG_BT_EXT_ADV */
Expand All @@ -232,26 +228,49 @@ int main(void)
err = bt_le_ext_adv_create(&adv_param, NULL, &adv);
if (err) {
printk("Failed to create extended advertising set (err %d)\n", err);
return 0;
return err;
}
}

printk("Setting extended advertising data\n");
err = bt_le_ext_adv_set_data(adv, ad, ARRAY_SIZE(ad), NULL, 0);
if (err) {
printk("Failed to set extended advertising data (err %d)\n", err);
return 0;
return err;
}

printk("Starting Extended Advertising (connectable non-scannable)\n");
err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT);
if (err) {
printk("Failed to start extended advertising set (err %d)\n", err);
return 0;
return err;
}
#endif /* CONFIG_BT_EXT_ADV */
return 0;
}

int main(void)
{
int err;

err = bt_enable(NULL);
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return 0;
}

printk("Advertising successfully started\n");
printk("Bluetooth initialized\n");

bt_conn_auth_cb_register(&auth_cb_display);

bt_hrs_cb_register(&hrs_cb);

err == start_adv();
if (0 == err) {
printk("Advertising successfully started\n");
} else {
printk("Advertising failed to start\n");
}

#if defined(HAS_LED)
err = blink_setup();
Expand Down