Skip to content

Commit 1b3dbf0

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

File tree

1 file changed

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

1 file changed

+28
-7
lines changed

samples/bluetooth/peripheral_ams_client/src/main.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ static const struct bt_data sd[] = {
4545
BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
4646
};
4747

48+
static struct k_work adv_work;
49+
4850
static struct bt_ams_client ams_c;
4951

5052
static const enum bt_ams_player_attribute_id entity_update_player[] = {
@@ -256,6 +258,23 @@ static const struct bt_gatt_dm_cb discover_cb = {
256258
.error_found = discover_error_found_cb,
257259
};
258260

261+
static void adv_work_handler(struct k_work *work)
262+
{
263+
int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
264+
265+
if (err) {
266+
printk("Advertising failed to start (err %d)\n", err);
267+
return;
268+
}
269+
270+
printk("Advertising successfully started\n");
271+
}
272+
273+
static void advertising_start(void)
274+
{
275+
k_work_submit(&adv_work);
276+
}
277+
259278
static void connected(struct bt_conn *conn, uint8_t err)
260279
{
261280
int sec_err;
@@ -311,10 +330,17 @@ static void security_changed(struct bt_conn *conn, bt_security_t level,
311330
}
312331
}
313332

333+
static void recycled_cb(void)
334+
{
335+
printk("Connection object available from previous conn. Disconnect is complete!\n");
336+
advertising_start();
337+
}
338+
314339
BT_CONN_CB_DEFINE(conn_callbacks) = {
315340
.connected = connected,
316341
.disconnected = disconnected,
317342
.security_changed = security_changed,
343+
.recycled = recycled_cb,
318344
};
319345

320346
static void auth_cancel(struct bt_conn *conn)
@@ -479,13 +505,8 @@ int main(void)
479505
return 0;
480506
}
481507

482-
err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
483-
if (err) {
484-
printk("Advertising failed to start (err %d)\n", err);
485-
return 0;
486-
}
487-
488-
printk("Advertising successfully started\n");
508+
k_work_init(&adv_work, adv_work_handler);
509+
advertising_start();
489510

490511
for (;;) {
491512
dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);

0 commit comments

Comments
 (0)