Skip to content

Commit a9f23cc

Browse files
jhedbergkartben
authored andcommitted
samples: bluetooth: peripheral_sc_only: Fix restarting advertising
We need to call `bt_le_adv_start()` again to make sure the device remains connectable after a disconnection event. The appropriate place to do it is in the `recycled()` callback. Signed-off-by: Johan Hedberg <[email protected]>
1 parent 3ae8a9c commit a9f23cc

File tree

1 file changed

+14
-6
lines changed
  • samples/bluetooth/peripheral_sc_only/src

1 file changed

+14
-6
lines changed

samples/bluetooth/peripheral_sc_only/src/main.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ static const struct bt_data sd[] = {
2828
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
2929
};
3030

31+
static void start_adv(void)
32+
{
33+
int err;
34+
35+
err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
36+
if (err) {
37+
printk("Advertising failed to start (err %d)\n", err);
38+
} else {
39+
printk("Advertising successfully started\n");
40+
}
41+
}
42+
3143
static void connected(struct bt_conn *conn, uint8_t err)
3244
{
3345
char addr[BT_ADDR_LE_STR_LEN];
@@ -86,6 +98,7 @@ static void security_changed(struct bt_conn *conn, bt_security_t level,
8698
BT_CONN_CB_DEFINE(conn_callbacks) = {
8799
.connected = connected,
88100
.disconnected = disconnected,
101+
.recycled = start_adv,
89102
.identity_resolved = identity_resolved,
90103
.security_changed = security_changed,
91104
};
@@ -145,12 +158,7 @@ int main(void)
145158
bt_conn_auth_cb_register(&auth_cb_display);
146159
bt_conn_auth_info_cb_register(&auth_cb_info);
147160

148-
err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
149-
if (err) {
150-
printk("Advertising failed to start (err %d)\n", err);
151-
return 0;
152-
}
161+
start_adv();
153162

154-
printk("Advertising successfully started\n");
155163
return 0;
156164
}

0 commit comments

Comments
 (0)