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

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Revision:
1:c81543a36d05
Parent:
0:dbff48b4070b
Child:
7:15e13a8f1193
--- a/main.cpp	Tue Sep 22 19:01:03 2015 +0000
+++ b/main.cpp	Thu Dec 03 11:50:36 2015 +0000
@@ -21,48 +21,61 @@
 BLE  ble;
  
 DigitalOut led1(LED1, 1);
-Ticker t;
- 
-void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+
+const static char     DEVICE_NAME[] = "BATTERY";
+static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE};
+
+static volatile bool  triggerSensorPolling = false;
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
-    printf("Disconnected handle %u!\n\r", handle);
+    printf("Disconnected handle %u!\n\r", params->handle);
     printf("Restarting the advertising process\n\r");
-    ble.startAdvertising();
+    ble.gap().startAdvertising();
 }
  
 void blink(void)
 {
     led1 = !led1;
+    triggerSensorPolling = true;
 }
  
 int main(void)
 {
+    Ticker t;
     uint8_t batteryLevel = 50;
     t.attach(blink, 1.0f);
  
     printf("Initialising\n\r");
  
     ble.init();
-    ble.onDisconnection(disconnectionCallback);
+    ble.gap().onDisconnection(disconnectionCallback);
  
     BatteryService batteryService(ble, batteryLevel);
  
     /* setup advertising */
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
     ble.startAdvertising();
  
     while (true) {
-        ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup)
+        // check for trigger from periodicCallback()
+        if (triggerSensorPolling && ble.getGapState().connected) {
+            triggerSensorPolling = false;
  
-        // the magic battery processing
-        batteryLevel++;
-        if (batteryLevel > 100) {
-            batteryLevel = 20;
+            // the magic battery processing
+            batteryLevel++;
+            if (batteryLevel > 100) {
+                batteryLevel = 20;
+            }
+ 
+            batteryService.updateBatteryLevel(batteryLevel);
+        } else {
+            ble.waitForEvent(); // low power wait for event
         }
- 
-        batteryService.updateBatteryLevel(batteryLevel);
     }
 }
  
\ No newline at end of file