AltBeacon program for embedded BLE. This program demonstrates how to set up a BLE device to broadcast AltBLE compatible data. Please see the official website for more details. https://github.com/AltBeacon/spec and http://altbeacon.org/

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_AltBeacon by Austin Blackstone

Description

AltBeacon is an open beacon standard developed by Roving Networks. AltBeacons an alternative to the closed sourced and heavily licensed iBeacon standard.

For full details please see the AltBeacon repository

Revision:
2:6ec277483638
Parent:
0:f519dff5c6a7
--- a/main.cpp	Fri Mar 20 21:10:12 2015 +0000
+++ b/main.cpp	Tue Jan 12 11:34:09 2016 +0000
@@ -16,6 +16,7 @@
 
 #include "mbed.h"
 #include "AltBeaconService.h"
+#include "ble/BLE.h"
 
 /**
  * For this demo application, populate the beacon advertisement payload
@@ -25,8 +26,6 @@
  *  Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 11, 18
  */
 
-BLEDevice ble;
-
 /**
  * The AltBeacon requires a manufacturer ID, and a Beacon ID
  * the first 16 bytes of the BeaconID should be a UUID and the remaining
@@ -35,25 +34,58 @@
  * Note: please remember to calibrate your beacon
  * RSSI for more accurate results.
  */
-uint8_t beaconID[] = {  0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
-                        0x10,0x11,0x12,0x13,0x14,0x15,0x00,0x01,0x00,0x02 };
-uint16_t manufacturerID = 0x5900; //Nordic SIG ID
-int8_t rssi = -122;
+uint8_t beaconID[]      = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
+                           0x10,0x11,0x12,0x13,0x14,0x15,0x00,0x01,0x00,0x02};
+uint16_t manufacturerID = 0x5900; /* Nordic SIG ID */
+int8_t   rssi           = -122;
+
+AltBeaconService *altBeaconServicePtr;
+
+/**
+ * This function is called when the ble initialization process has failed
+ */
+void onBleInitError(BLE &ble, ble_error_t error)
+{
+    /* Initialization error handling should go here */
+}
 
+/**
+ * Callback triggered when the ble initialization process has finished
+ */
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+{
+    BLE&        ble   = params->ble;
+    ble_error_t error = params->error;
+
+    if (error != BLE_ERROR_NONE) {
+        /* In case of error, forward the error handling to onBleInitError */
+        onBleInitError(ble, error);
+        return;
+    }
+
+    /* Ensure that it is the default instance of BLE */
+    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+        return;
+    }
+
+    /* Initialize AltBeacon */
+    altBeaconServicePtr =new AltBeaconService(ble, manufacturerID, beaconID, rssi);
+
+    /* Set advertising time */
+    ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
+    
+    /* Start advertising */
+    ble.startAdvertising();
+}
 
 int main(void)
 {
-    // Initialize BLE baselayer
-    ble.init();
-
-    // Initialize AltBeacon
-    AltBeaconService altbeacon(ble, manufacturerID, beaconID, rssi);
+    BLE& ble = BLE::Instance();
 
-    // Set advertising time
-    ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
-    ble.startAdvertising();
+    /* Initialize BLE baselayer */ 
+    ble.init(bleInitComplete);
 
-    while(1) {
-        ble.waitForEvent(); // allows or low power operation
+    while(true) {
+        ble.waitForEvent(); /* Allow low power operation */
     }
 }