An example of creating and updating a simple GATT Service using the BLE_API

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Revision:
7:15e13a8f1193
Parent:
1:c81543a36d05
--- a/main.cpp	Fri Feb 19 14:58:43 2016 +0000
+++ b/main.cpp	Thu Feb 25 10:04:07 2016 +0000
@@ -18,8 +18,6 @@
 #include "BLE.h"
 #include "BatteryService.h"
  
-BLE  ble;
- 
 DigitalOut led1(LED1, 1);
 
 const static char     DEVICE_NAME[] = "BATTERY";
@@ -29,9 +27,8 @@
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
-    printf("Disconnected handle %u!\n\r", params->handle);
-    printf("Restarting the advertising process\n\r");
-    ble.gap().startAdvertising();
+    (void)params;
+    BLE::Instance().gap().startAdvertising(); // restart advertising
 }
  
 void blink(void)
@@ -39,18 +36,31 @@
     led1 = !led1;
     triggerSensorPolling = true;
 }
- 
-int main(void)
+
+void onBleInitError(BLE &ble, ble_error_t error)
+{
+    (void)ble;
+    (void)error;
+   /* Initialization error handling should go here */
+}
+
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
 {
-    Ticker t;
+    BLE&        ble   = params->ble;
+    ble_error_t error = params->error;
+
+    if (error != BLE_ERROR_NONE) {
+        onBleInitError(ble, error);
+        return;
+    }
+
+    if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+        return;
+    }
+
+    ble.gap().onDisconnection(disconnectionCallback);
+
     uint8_t batteryLevel = 50;
-    t.attach(blink, 1.0f);
- 
-    printf("Initialising\n\r");
- 
-    ble.init();
-    ble.gap().onDisconnection(disconnectionCallback);
- 
     BatteryService batteryService(ble, batteryLevel);
  
     /* setup advertising */
@@ -78,4 +88,13 @@
         }
     }
 }
+
+int main(void)
+{
+    Ticker t;
+    t.attach(blink, 1.0f);
+ 
+    BLE::Instance().init(bleInitComplete);
+}
+
  
\ No newline at end of file