1
2 #include "os/os.h"
3 #include "nimble/hci_common.h"
4 #include "host/ble_hs_hci.h"
5 #include "ble_hs_priv.h"
6
7 #include <rtthread.h>
8
ble_app_set_addr(void)9 static void ble_app_set_addr(void)
10 {
11 ble_addr_t addr;
12 int rc;
13
14 rc = ble_hs_id_gen_rnd(1, &addr);
15 RT_ASSERT(rc == 0);
16
17 rc = ble_hs_id_set_rnd(addr.val);
18 RT_ASSERT(rc == 0);
19 }
20
ble_app_advertise(void)21 static void ble_app_advertise(void)
22 {
23 struct ble_gap_adv_params adv_params;
24 uint8_t uuid128[16];
25 int rc;
26 int8_t tx_pwr;
27
28 /* Arbitrarily set the UUID to a string of 0x11 bytes. */
29 memset(uuid128, 0x11, sizeof uuid128);
30
31 rc = ble_hs_hci_util_read_adv_tx_pwr(&tx_pwr);
32 RT_ASSERT(rc == 0);
33
34 /* Major version=2; minor version=10. */
35 rc = ble_ibeacon_set_adv_data(uuid128, 2, 10, tx_pwr);
36 RT_ASSERT(rc == 0);
37
38 /* Begin advertising. */
39 adv_params = (struct ble_gap_adv_params){ 0 };
40 rc = ble_gap_adv_start(BLE_OWN_ADDR_RANDOM, NULL, BLE_HS_FOREVER,
41 &adv_params, NULL, NULL);
42 RT_ASSERT(rc == 0);
43 }
44
ble_app_on_sync(void)45 static void ble_app_on_sync(void)
46 {
47 /* Generate a non-resolvable private address. */
48 ble_app_set_addr();
49
50 /* Advertise indefinitely. */
51 ble_app_advertise();
52 }
53
ble_ibeacon(void)54 static int ble_ibeacon(void)
55 {
56 static int init_flag = 0;
57
58 if (init_flag)
59 return 0;
60 init_flag = 1;
61
62 ble_hs_cfg.sync_cb = ble_app_on_sync;
63
64 /* startup bluetooth host stack*/
65 ble_hs_thread_startup();
66
67 return 0;
68 }
69 MSH_CMD_EXPORT_ALIAS(ble_ibeacon, ble_ibeacon, "bluetoooth ibeacon sample");
70