PUBLISH HelloBlue

Dependencies:   BLE_API mbed nRF51822

Fork of HelloBlue by Wayne Keenan

Revision:
2:4f0d8bf09690
Parent:
1:b84d6e0b404e
diff -r b84d6e0b404e -r 4f0d8bf09690 main.cpp
--- a/main.cpp	Wed Jan 28 17:44:09 2015 +0000
+++ b/main.cpp	Mon Jan 18 11:13:31 2016 +0000
@@ -1,31 +1,50 @@
 #include "mbed.h"
-#include "BLEDevice.h"
+#include "ble/BLE.h"
 
-// DFUService is already included & automatically advertised by the mbed lib dependancies (currently)
+// DFUService is already included & automatically advertised by the mbed lib dependancies
 
 const static char     DEVICE_NAME[]        = "HelloBlue";
 
 BLEDevice ble;
 
-void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
+}
+
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
 {
-    ble.startAdvertising(); // restart advertising
+    BLE &ble          = params->ble;
+    ble_error_t error = params->error;
+
+    if (error != BLE_ERROR_NONE) {
+        return;
+    }
+
+    ble.gap().onDisconnection(disconnectionCallback);
+    
+    /* Setup advertising. */
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.gap().setAdvertisingInterval(1000); /* ms */
+    ble.gap().startAdvertising();
 }
 
+
 int main(void)
 {
-    ble.init();
-    ble.onDisconnection(disconnectionCallback);
-
-    /* Setup advertising. */
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
-    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000));
-    ble.startAdvertising();
+    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
+    ble.init(bleInitComplete);
     
-   while (true) {
-        ble.waitForEvent();
+    /* SpinWait for initialization to complete. This is necessary because the
+     * BLE object is used in the main loop below. */
+    while (ble.hasInitialized()  == false) { /* spin loop */ }
+    
+    while (1) 
+    {
+        // to see if a Central device is currently connected you can call: ble.getGapState().connected
+        ble.waitForEvent(); // low power wait for event
     }
 }