1- // Based on https://mynewt.apache.org/latest/tutorials/ble/ibeacon.html
1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one
3+ * or more contributor license agreements. See the NOTICE file
4+ * distributed with this work for additional information
5+ * regarding copyright ownership. The ASF licenses this file
6+ * to you under the Apache License, Version 2.0 (the
7+ * "License"); you may not use this file except in compliance
8+ * with the License. You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing,
13+ * software distributed under the License is distributed on an
14+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ * KIND, either express or implied. See the License for the
16+ * specific language governing permissions and limitations
17+ * under the License.
18+ */
19+ // Bluetooth LE Functions. Based on https://mynewt.apache.org/latest/tutorials/ble/ibeacon.html
220#include "sysinit/sysinit.h"
321#include "os/os.h"
422#include "console/console.h"
523#include "host/ble_hs.h"
624
7- static void
8- ble_app_set_addr (void )
9- {
25+ static void ble_app_on_sync (void );
26+ static void ble_app_set_addr (void );
27+ static void ble_app_advertise (void );
28+
29+ int start_ble (void ) {
30+ // Set the callback for starting Bluetooth LE.
31+ ble_hs_cfg .sync_cb = ble_app_on_sync ;
32+ return 0 ;
33+ }
34+
35+ static void ble_app_on_sync (void ) {
36+ // Called upon starting Bluetooth LE.
37+ // Generate a non-resolvable private address.
38+ ble_app_set_addr ();
39+
40+ // Advertise indefinitely as an iBeacon.
41+ ble_app_advertise ();
42+ }
43+
44+ static void ble_app_set_addr (void ) {
45+ // Generate a non-resolvable private address.
1046 ble_addr_t addr ;
1147 int rc ;
1248
@@ -17,41 +53,23 @@ ble_app_set_addr(void)
1753 assert (rc == 0 );
1854}
1955
20- static void
21- ble_app_advertise (void )
22- {
56+ static void ble_app_advertise (void ) {
57+ // Advertise indefinitely as an iBeacon.
2358 struct ble_gap_adv_params adv_params ;
2459 uint8_t uuid128 [16 ];
2560 int rc ;
2661
27- /* Arbitrarily set the UUID to a string of 0x11 bytes. */
62+ // Arbitrarily set the UUID to a string of 0x11 bytes.
2863 memset (uuid128 , 0x11 , sizeof uuid128 );
2964
30- /* Major version=2; minor version=10. */
31- // Measured Power ranging data (Calibrated tx power at 1 meters). Must be > -126 and < 20
32- rc = ble_ibeacon_set_adv_data (uuid128 , 2 , 10 , -60 ); // TODO: Confirm RSSI
65+ // Set iBeacon parameters: Major version=2; minor version=10; RSSI=-60.
66+ // RSSI is the Measured Power ranging data (Calibrated tx power at 1 meters). Must be > -126 and < 20.
67+ rc = ble_ibeacon_set_adv_data (uuid128 , 2 , 10 , -60 ); // TODO: Verify RSSI for your device.
3368 assert (rc == 0 );
3469
35- /* Begin advertising. */
70+ // Begin advertising as an iBeacon.
3671 adv_params = (struct ble_gap_adv_params ){ 0 };
3772 rc = ble_gap_adv_start (BLE_OWN_ADDR_RANDOM , NULL , BLE_HS_FOREVER ,
3873 & adv_params , NULL , NULL );
3974 assert (rc == 0 );
4075}
41-
42- static void
43- ble_app_on_sync (void )
44- {
45- /* Generate a non-resolvable private address. */
46- ble_app_set_addr ();
47-
48- /* Advertise indefinitely. */
49- ble_app_advertise ();
50- }
51-
52- int
53- start_ble (void )
54- {
55- ble_hs_cfg .sync_cb = ble_app_on_sync ;
56- return 0 ;
57- }
0 commit comments