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

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

This example creates and updates a standard Battery Level service, and a single GATT characteristic that contains the battery level.

Files at this revision

API Documentation at this revision

Comitter:
vcoubard
Date:
Tue Nov 03 16:03:18 2015 +0000
Parent:
16:5cdd04cf1ed4
Child:
18:8546914968b6
Commit message:
Use proper API instead of deprecated ones; Update disconnection callback signature to the correct one; Add the LE_GENERAL_DISCOVERABLE into advertisement FLAGS; Use new BLE::init API of BLE API 2.xx

Changed in this revision

BLE_API.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
nRF51822.lib Show annotated file Show diff for this revision Revisions of this file
--- a/BLE_API.lib	Sat Jun 20 23:22:18 2015 +0000
+++ b/BLE_API.lib	Tue Nov 03 16:03:18 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#9f4251b3355c
+http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#a097e1be76f4
--- a/main.cpp	Sat Jun 20 23:22:18 2015 +0000
+++ b/main.cpp	Tue Nov 03 16:03:18 2015 +0000
@@ -18,16 +18,16 @@
 #include "BLE.h"
 #include "BatteryService.h"
 
-BLE  ble;
-
 DigitalOut led1(LED1, 1);
 Ticker t;
+BatteryService *batteryService = NULL;
+uint8_t batteryLevel = 50;
 
-void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *disconnectionParams)
 {
-    printf("Disconnected handle %u!\n\r", handle);
+    printf("Disconnected handle %u!\n\r", disconnectionParams->handle);
     printf("Restarting the advertising process\n\r");
-    ble.startAdvertising();
+    BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
 }
 
 void blink(void)
@@ -35,23 +35,39 @@
     led1 = !led1;
 }
 
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+{
+    BLE &ble          = params->ble;
+    ble_error_t error = params->error;
+    Gap& gap = ble.gap();
+
+    if (error != BLE_ERROR_NONE) {
+        return;
+    }
+
+    gap.onDisconnection(disconnectionCallback);
+
+    batteryService = new BatteryService(ble, batteryLevel);
+
+    /* setup advertising */
+    gap.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+    gap.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    gap.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
+    gap.startAdvertising();
+}
+
 int main(void)
 {
-    uint8_t batteryLevel = 50;
     t.attach(blink, 1.0f);
 
     printf("Initialising the nRF51822\n\r");
 
-    ble.init();
-    ble.onDisconnection(disconnectionCallback);
-
-    BatteryService batteryService(ble, batteryLevel);
+    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
+    ble.init(bleInitComplete);
 
-    /* setup advertising */
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
-    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
-    ble.startAdvertising();
+    /* 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 (true) {
         ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup)
@@ -62,6 +78,6 @@
             batteryLevel = 20;
         }
 
-        batteryService.updateBatteryLevel(batteryLevel);
+        batteryService->updateBatteryLevel(batteryLevel);
     }
 }
--- a/mbed.bld	Sat Jun 20 23:22:18 2015 +0000
+++ b/mbed.bld	Tue Nov 03 16:03:18 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/7cff1c4259d7
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/9296ab0bfc11
\ No newline at end of file
--- a/nRF51822.lib	Sat Jun 20 23:22:18 2015 +0000
+++ b/nRF51822.lib	Tue Nov 03 16:03:18 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#7c68c8d67e1f
+http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#bf85bf7e73d5