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 #include <zephyr/bluetooth/hci.h>
16
17 #define DEVICE_NAME CONFIG_BT_DEVICE_NAME
18 #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
19
20 /*
21 * Set Advertisement data. Based on the Eddystone specification:
22 * https://github.com/google/eddystone/blob/master/protocol-specification.md
23 * https://github.com/google/eddystone/tree/master/eddystone-url
24 */
25 static const struct bt_data ad[] = {
26 BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
27 BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0xaa, 0xfe),
28 BT_DATA_BYTES(BT_DATA_SVC_DATA16,
29 0xaa, 0xfe, /* Eddystone UUID */
30 0x10, /* Eddystone-URL frame type */
31 0x00, /* Calibrated Tx power at 0m */
32 0x00, /* URL Scheme Prefix http://www. */
33 'z', 'e', 'p', 'h', 'y', 'r',
34 'p', 'r', 'o', 'j', 'e', 'c', 't',
35 0x08) /* .org */
36 };
37
38 /* Set Scan Response data */
39 static const struct bt_data sd[] = {
40 BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
41 };
42
bt_ready(int err)43 static void bt_ready(int err)
44 {
45 char addr_s[BT_ADDR_LE_STR_LEN];
46 bt_addr_le_t addr = {0};
47 size_t count = 1;
48
49 if (err) {
50 printk("Bluetooth init failed (err %d)\n", err);
51 return;
52 }
53
54 printk("Bluetooth initialized\n");
55
56 /* Start advertising */
57 err = bt_le_adv_start(BT_LE_ADV_NCONN_IDENTITY, ad, ARRAY_SIZE(ad),
58 sd, ARRAY_SIZE(sd));
59 if (err) {
60 printk("Advertising failed to start (err %d)\n", err);
61 return;
62 }
63
64
65 /* For connectable advertising you would use
66 * bt_le_oob_get_local(). For non-connectable non-identity
67 * advertising an non-resolvable private address is used;
68 * there is no API to retrieve that.
69 */
70
71 bt_id_get(&addr, &count);
72 bt_addr_le_to_str(&addr, addr_s, sizeof(addr_s));
73
74 printk("Beacon started, advertising as %s\n", addr_s);
75 }
76
main(void)77 int main(void)
78 {
79 int err;
80
81 printk("Starting Beacon Demo\n");
82
83 /* Initialize the Bluetooth Subsystem */
84 err = bt_enable(bt_ready);
85 if (err) {
86 printk("Bluetooth init failed (err %d)\n", err);
87 }
88 return 0;
89 }
90