Skip to content

Commit d54220f

Browse files
leewkb4567rlubos
authored andcommitted
samples: bluetooth: ancs_client: Fix resume advertising
ANCS sample resumes advertising after disconnection. Ref: NCSDK-32197 Signed-off-by: Bernard Lee <[email protected]>
1 parent 73f05c8 commit d54220f

File tree

1 file changed

+28
-7
lines changed
  • samples/bluetooth/peripheral_ancs_client/src

1 file changed

+28
-7
lines changed

samples/bluetooth/peripheral_ancs_client/src/main.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ static const struct bt_data ad[] = {
5050

5151
static struct bt_ancs_client ancs_c;
5252

53+
static struct k_work adv_work;
54+
5355
static struct bt_gattp gattp;
5456

5557
static atomic_t discovery_flags;
@@ -317,6 +319,23 @@ static void discover_ancs_again(struct bt_conn *conn)
317319
discover_ancs(conn, true);
318320
}
319321

322+
static void adv_work_handler(struct k_work *work)
323+
{
324+
int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2, ad, ARRAY_SIZE(ad), NULL, 0);
325+
326+
if (err) {
327+
printk("Advertising failed to start (err %d)\n", err);
328+
return;
329+
}
330+
331+
printk("Advertising successfully started\n");
332+
}
333+
334+
static void advertising_start(void)
335+
{
336+
k_work_submit(&adv_work);
337+
}
338+
320339
static void connected(struct bt_conn *conn, uint8_t err)
321340
{
322341
int sec_err;
@@ -369,10 +388,17 @@ static void security_changed(struct bt_conn *conn, bt_security_t level,
369388
}
370389
}
371390

391+
static void recycled_cb(void)
392+
{
393+
printk("Connection object available from previous conn. Disconnect is complete!\n");
394+
advertising_start();
395+
}
396+
372397
BT_CONN_CB_DEFINE(conn_callbacks) = {
373398
.connected = connected,
374399
.disconnected = disconnected,
375400
.security_changed = security_changed,
401+
.recycled = recycled_cb,
376402
};
377403

378404
static void auth_cancel(struct bt_conn *conn)
@@ -730,13 +756,8 @@ int main(void)
730756
return 0;
731757
}
732758

733-
err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2, ad, ARRAY_SIZE(ad), NULL, 0);
734-
if (err) {
735-
printk("Advertising failed to start (err %d)\n", err);
736-
return 0;
737-
}
738-
739-
printk("Advertising successfully started\n");
759+
k_work_init(&adv_work, adv_work_handler);
760+
advertising_start();
740761

741762
for (;;) {
742763
dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);

0 commit comments

Comments
 (0)