-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: bluetooth: Added sample for Extended Advertisements
Both advertiser and scanner demonstrate the use of extended advertising and scanning, and how to gracefully restart the extended advertisements through the use of recycle() callback. In the sample, the advertiser initiates a connectable advertisement set, which prompts the scanner to connect. After the connection is established, the advertiser waits for 5 seconds to disconnect. After the connection is dropped, the advertiser immediately restarts broadcasting, while the scanner cools-down for 5 seconds to restart its process. Signed-off-by: Luis Ubieda <[email protected]>
- Loading branch information
1 parent
7e5b2a7
commit 2745f6a
Showing
9 changed files
with
476 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
.. _bluetooth_extended_advertising_sample: | ||
|
||
Bluetooth: Extended Advertising | ||
################################ | ||
|
||
Overview | ||
******** | ||
|
||
This sample demonstrates the use of the extended advertising feature, by: | ||
|
||
- Outlining the steps required to initialize an extended advertising application. | ||
- Demo how to gracefully restart the functionality, after a disconnect. | ||
|
||
The sample consists of the advertiser initiating a connectable advertisement set, | ||
which prompts the scanner to connect after scanning for extended advertisements. | ||
Once the connection is established, the advertiser waits for 5 seconds to disconnect. | ||
After the connection is dropped, the advertiser immediately restarts broadcasting, | ||
while the scanner cools-down for 5 seconds to restart its process. | ||
|
||
This sample handles all actions in a separate thread, to promote good design | ||
practices. Even though it is not strictly required, scheduling from another context is | ||
strongly recommended (e.g. using a work item), as re-starting an advertiser or | ||
scanner from within the `recycled` callback exposes the application to deadlocking. | ||
|
||
Requirements | ||
************ | ||
|
||
* Two boards with Bluetooth Low Energy support | ||
|
||
Building and Running | ||
******************** | ||
|
||
This sample can be found under | ||
:zephyr_file:`samples/bluetooth/extended_adv` in the Zephyr tree. | ||
|
||
See :ref:`bluetooth samples section <bluetooth-samples>` for details. | ||
|
||
This sample uses two applications, so two devices need to be setup. | ||
Flash one device with the scanner application, and another device with the | ||
advertiser application. | ||
|
||
The two devices should automatically connect if they are close enough. | ||
|
||
Here are the outputs you should get by default: | ||
|
||
Advertiser: | ||
|
||
.. code-block:: console | ||
*** Booting Zephyr OS build zephyr-v3.5.0-4935-gfc7972183da5 *** | ||
Starting Extended Advertising Demo | ||
Starting Extended Advertising | ||
Connected (err 0x00) | ||
Connected state! | ||
Initiating disconnect within 5 seconds... | ||
Disconnected (reason 0x16) | ||
Connection object available from previous conn. Disconnect is complete! | ||
Disconnected state! Restarting advertising | ||
Starting Extended Advertising | ||
Connected (err 0x00) | ||
Connected state! | ||
Initiating disconnect within 5 seconds... | ||
Disconnected (reason 0x16) | ||
Connection object available from previous conn. Disconnect is complete! | ||
Disconnected state! Restarting advertising | ||
Starting Extended Advertising | ||
Scanner: | ||
|
||
.. code-block:: console | ||
*** Booting Zephyr OS build zephyr-v3.5.0-4935-ge3308caf97bc *** | ||
Starting Extended Advertising Demo [Scanner] | ||
Found extended advertisement packet! | ||
Stopping scan | ||
Connected (err 0x00) | ||
Connected state! | ||
Disconnected (reason 0x13) | ||
Recycled cb called! | ||
Disconnected, cooldown for 5 seconds! | ||
Starting to scan for extended adv | ||
Found extended advertisement packet! | ||
Stopping scan | ||
Connected (err 0x00) | ||
Connected state! | ||
Disconnected (reason 0x13) | ||
Recycled cb called! | ||
Disconnected, cooldown for 5 seconds! | ||
Starting to scan for extended adv | ||
Found extended advertisement packet! | ||
Stopping scan | ||
Connected (err 0x00) | ||
Connected state! | ||
Disconnected (reason 0x13) | ||
Recycled cb called! | ||
Disconnected, cooldown for 5 seconds! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Copyright 2024 Croxel, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(extended_adv) | ||
|
||
target_sources(app PRIVATE src/main.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CONFIG_BT=y | ||
CONFIG_BT_DEVICE_NAME="test_ext_adv" | ||
CONFIG_BT_PERIPHERAL=y | ||
CONFIG_BT_EXT_ADV=y | ||
|
||
CONFIG_ASSERT=y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
sample: | ||
name: Bluetooth Extended Advertising Advertiser | ||
tests: | ||
sample.bluetooth.extended_advertising.advertiser: | ||
harness: bluetooth | ||
platform_allow: nrf52840dk_nrf52840 | ||
tags: bluetooth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/** | ||
* Copyright (c) 2024 Croxel, Inc. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/bluetooth/bluetooth.h> | ||
#include <zephyr/bluetooth/conn.h> | ||
#include <zephyr/bluetooth/gap.h> | ||
|
||
static struct bt_conn *default_conn; | ||
|
||
enum bt_sample_adv_evt { | ||
BT_SAMPLE_EVT_CONNECTED, | ||
BT_SAMPLE_EVT_DISCONNECTED, | ||
BT_SAMPLE_EVT_MAX, | ||
}; | ||
|
||
enum bt_sample_adv_st { | ||
BT_SAMPLE_ST_ADV, | ||
BT_SAMPLE_ST_CONNECTED, | ||
}; | ||
|
||
static ATOMIC_DEFINE(evt_bitmask, BT_SAMPLE_EVT_MAX); | ||
|
||
static volatile enum bt_sample_adv_st app_st = BT_SAMPLE_ST_ADV; | ||
|
||
static struct k_poll_signal poll_sig = K_POLL_SIGNAL_INITIALIZER(poll_sig); | ||
static struct k_poll_event poll_evt = K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL, | ||
K_POLL_MODE_NOTIFY_ONLY, &poll_sig); | ||
|
||
static void raise_evt(enum bt_sample_adv_evt evt) | ||
{ | ||
(void)atomic_set_bit(evt_bitmask, evt); | ||
k_poll_signal_raise(poll_evt.signal, 1); | ||
} | ||
|
||
static void connected_cb(struct bt_conn *conn, uint8_t err) | ||
{ | ||
printk("Connected (err 0x%02X)\n", err); | ||
|
||
if (err) { | ||
return; | ||
} | ||
|
||
__ASSERT(!default_conn, "Attempting to override existing connection object!"); | ||
default_conn = bt_conn_ref(conn); | ||
|
||
raise_evt(BT_SAMPLE_EVT_CONNECTED); | ||
} | ||
|
||
static void disconnected_cb(struct bt_conn *conn, uint8_t reason) | ||
{ | ||
printk("Disconnected (reason 0x%02X)\n", reason); | ||
|
||
__ASSERT(conn == default_conn, "Unexpected disconnected callback"); | ||
|
||
bt_conn_unref(default_conn); | ||
default_conn = NULL; | ||
} | ||
|
||
static void recycled_cb(void) | ||
{ | ||
printk("Connection object available from previous conn. Disconnect is complete!\n"); | ||
raise_evt(BT_SAMPLE_EVT_DISCONNECTED); | ||
} | ||
|
||
BT_CONN_CB_DEFINE(conn_cb) = { | ||
.connected = connected_cb, | ||
.disconnected = disconnected_cb, | ||
.recycled = recycled_cb, | ||
}; | ||
|
||
static int start_advertising(struct bt_le_ext_adv *adv) | ||
{ | ||
int err; | ||
|
||
printk("Starting Extended Advertising\n"); | ||
err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT); | ||
if (err) { | ||
printk("Failed to start extended advertising (err %d)\n", err); | ||
} | ||
|
||
return err; | ||
} | ||
|
||
int main(void) | ||
{ | ||
int err; | ||
struct bt_le_ext_adv *adv; | ||
|
||
printk("Starting Extended Advertising Demo\n"); | ||
|
||
/* Initialize the Bluetooth Subsystem */ | ||
err = bt_enable(NULL); | ||
if (err) { | ||
printk("Bluetooth init failed (err %d)\n", err); | ||
return err; | ||
} | ||
|
||
/* Create a non-connectable non-scannable advertising set */ | ||
err = bt_le_ext_adv_create(BT_LE_EXT_ADV_CONN_NAME, NULL, &adv); | ||
if (err) { | ||
printk("Failed to create advertising set (err %d)\n", err); | ||
return err; | ||
} | ||
|
||
err = start_advertising(adv); | ||
if (err) { | ||
return err; | ||
} | ||
|
||
while (true) { | ||
k_poll(&poll_evt, 1, K_FOREVER); | ||
|
||
k_poll_signal_reset(poll_evt.signal); | ||
poll_evt.state = K_POLL_STATE_NOT_READY; | ||
|
||
/* Identify event and act upon if applicable */ | ||
if (atomic_test_and_clear_bit(evt_bitmask, BT_SAMPLE_EVT_CONNECTED) && | ||
app_st == BT_SAMPLE_ST_ADV) { | ||
|
||
printk("Connected state!\n"); | ||
app_st = BT_SAMPLE_ST_CONNECTED; | ||
|
||
printk("Initiating disconnect within 5 seconds...\n"); | ||
k_sleep(K_SECONDS(5)); | ||
|
||
bt_conn_disconnect(default_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); | ||
|
||
} else if (atomic_test_and_clear_bit(evt_bitmask, BT_SAMPLE_EVT_DISCONNECTED) && | ||
app_st == BT_SAMPLE_ST_CONNECTED) { | ||
|
||
printk("Disconnected state! Restarting advertising\n"); | ||
app_st = BT_SAMPLE_ST_ADV; | ||
err = start_advertising(adv); | ||
if (err) { | ||
return err; | ||
} | ||
} | ||
} | ||
|
||
return err; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Copyright 2024 Croxel, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(extended_adv_scanner) | ||
|
||
target_sources(app PRIVATE src/main.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CONFIG_BT=y | ||
CONFIG_BT_CENTRAL=y | ||
CONFIG_BT_EXT_ADV=y | ||
|
||
CONFIG_ASSERT=y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
sample: | ||
name: Bluetooth Extended Advertising Scanner | ||
tests: | ||
sample.bluetooth.extended_advertising.scanner: | ||
harness: bluetooth | ||
platform_allow: nrf52840dk_nrf52840 | ||
tags: bluetooth |
Oops, something went wrong.