1 /* main.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2015-2016 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <zephyr/types.h>
10 #include <stddef.h>
11 #include <zephyr/sys/printk.h>
12 #include <zephyr/sys/util.h>
13 
14 #include <zephyr/bluetooth/bluetooth.h>
15 
16 static uint8_t mfg_data[] = { 0xff, 0xff, 0x00 };
17 
18 static const struct bt_data ad[] = {
19 	BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, 3),
20 };
21 
scan_cb(const bt_addr_le_t * addr,int8_t rssi,uint8_t adv_type,struct net_buf_simple * buf)22 static void scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type,
23 		    struct net_buf_simple *buf)
24 {
25 	mfg_data[2]++;
26 }
27 
main(void)28 int main(void)
29 {
30 	struct bt_le_scan_param scan_param = {
31 		.type       = BT_LE_SCAN_TYPE_PASSIVE,
32 		.options    = BT_LE_SCAN_OPT_NONE,
33 		.interval   = 0x0010,
34 		.window     = 0x0010,
35 	};
36 	int err;
37 
38 	printk("Starting Scanner/Advertiser Demo\n");
39 
40 	/* Initialize the Bluetooth Subsystem */
41 	err = bt_enable(NULL);
42 	if (err) {
43 		printk("Bluetooth init failed (err %d)\n", err);
44 		return 0;
45 	}
46 
47 	printk("Bluetooth initialized\n");
48 
49 	err = bt_le_scan_start(&scan_param, scan_cb);
50 	if (err) {
51 		printk("Starting scanning failed (err %d)\n", err);
52 		return 0;
53 	}
54 
55 	do {
56 		k_sleep(K_MSEC(400));
57 
58 		/* Start advertising */
59 		err = bt_le_adv_start(BT_LE_ADV_NCONN, ad, ARRAY_SIZE(ad),
60 				      NULL, 0);
61 		if (err) {
62 			printk("Advertising failed to start (err %d)\n", err);
63 			return 0;
64 		}
65 
66 		k_sleep(K_MSEC(400));
67 
68 		err = bt_le_adv_stop();
69 		if (err) {
70 			printk("Advertising failed to stop (err %d)\n", err);
71 			return 0;
72 		}
73 	} while (1);
74 	return 0;
75 }
76