1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/bluetooth/bluetooth.h>
8 
9 static uint8_t mfg_data[] = { 0xff, 0xff, 0x00 };
10 
11 static const struct bt_data per_adv_ad[] = {
12 	BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, 3),
13 };
14 
15 static const struct bt_data ad[] = {
16 	BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
17 };
18 
main(void)19 int main(void)
20 {
21 	struct bt_le_ext_adv *adv;
22 	int err;
23 
24 	printk("Starting Periodic Advertising Demo\n");
25 
26 	/* Initialize the Bluetooth Subsystem */
27 	err = bt_enable(NULL);
28 	if (err) {
29 		printk("Bluetooth init failed (err %d)\n", err);
30 		return 0;
31 	}
32 
33 	/* Create a non-connectable advertising set */
34 	err = bt_le_ext_adv_create(BT_LE_EXT_ADV_NCONN, NULL, &adv);
35 	if (err) {
36 		printk("Failed to create advertising set (err %d)\n", err);
37 		return 0;
38 	}
39 
40 	/* Set advertising data to have complete local name set */
41 	err = bt_le_ext_adv_set_data(adv, ad, ARRAY_SIZE(ad), NULL, 0);
42 	if (err) {
43 		printk("Failed to set advertising data (err %d)\n", err);
44 		return 0;
45 	}
46 
47 	/* Set periodic advertising parameters */
48 	err = bt_le_per_adv_set_param(adv, BT_LE_PER_ADV_DEFAULT);
49 	if (err) {
50 		printk("Failed to set periodic advertising parameters"
51 		       " (err %d)\n", err);
52 		return 0;
53 	}
54 
55 	/* Enable Periodic Advertising */
56 	err = bt_le_per_adv_start(adv);
57 	if (err) {
58 		printk("Failed to enable periodic advertising (err %d)\n", err);
59 		return 0;
60 	}
61 
62 	while (true) {
63 		printk("Start Extended Advertising...");
64 		err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT);
65 		if (err) {
66 			printk("Failed to start extended advertising "
67 			       "(err %d)\n", err);
68 			return 0;
69 		}
70 		printk("done.\n");
71 
72 		for (int i = 0; i < 3; i++) {
73 			k_sleep(K_SECONDS(10));
74 
75 			mfg_data[2]++;
76 
77 			printk("Set Periodic Advertising Data...");
78 			err = bt_le_per_adv_set_data(adv, per_adv_ad, ARRAY_SIZE(per_adv_ad));
79 			if (err) {
80 				printk("Failed (err %d)\n", err);
81 				return 0;
82 			}
83 			printk("done.\n");
84 		}
85 
86 		k_sleep(K_SECONDS(10));
87 
88 		printk("Stop Extended Advertising...");
89 		err = bt_le_ext_adv_stop(adv);
90 		if (err) {
91 			printk("Failed to stop extended advertising "
92 			       "(err %d)\n", err);
93 			return 0;
94 		}
95 		printk("done.\n");
96 
97 		k_sleep(K_SECONDS(10));
98 	}
99 	return 0;
100 }
101