An initial demo showcasing the GattClient APIs. Drives an LED service exported by a BLE_LED peripheral. Shows scanning, connections, service-discovery, and reads/writes.

Dependencies:   BLE_API mbed nRF51822

Files at this revision

API Documentation at this revision

Comitter:
andresag
Date:
Tue Jan 12 10:49:03 2016 +0000
Parent:
10:507318f2afda
Commit message:
Update example to latest BLE API.

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
diff -r 507318f2afda -r 023d96b0e427 BLE_API.lib
--- a/BLE_API.lib	Tue Sep 29 11:52:34 2015 +0000
+++ b/BLE_API.lib	Tue Jan 12 10:49:03 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#d494ad3e87bd
+http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#bfc5b9b6ecf5
diff -r 507318f2afda -r 023d96b0e427 main.cpp
--- a/main.cpp	Tue Sep 29 11:52:34 2015 +0000
+++ b/main.cpp	Tue Jan 12 10:49:03 2016 +0000
@@ -19,25 +19,26 @@
 #include "ble/DiscoveredCharacteristic.h"
 #include "ble/DiscoveredService.h"
 
-BLE ble;
+DigitalOut alivenessLED(LED1, 1);
 
-DigitalOut alivenessLED(LED1, 1);
-bool triggerLedCharacteristic = false;
+bool                     triggerLedCharacteristic = false;
 DiscoveredCharacteristic ledCharacteristic;
 
+Ticker ticker;
+
 void periodicCallback(void) {
     alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */
 }
 
 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
-    if (params->peerAddr[0] != 0x29) { /* !ALERT! Alter this filter to suit your device. */
+    if (params->peerAddr[0] != 0x37) { /* !ALERT! Alter this filter to suit your device. */
         return;
     }
     printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
            params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
            params->rssi, params->isScanResponse, params->type);
 
-    ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
+    BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
 }
 
 void serviceDiscoveryCallback(const DiscoveredService *service) {
@@ -67,8 +68,8 @@
 
 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
     if (params->role == Gap::CENTRAL) {
-        ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
-        ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001);
+        BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
+        BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001);
     }
 }
 
@@ -97,11 +98,33 @@
     printf("disconnected\r\n");
 }
 
-int main(void) {
-    Ticker ticker;
-    ticker.attach(periodicCallback, 1);
+/**
+ * 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 */
+}
 
-    ble.init();
+/**
+ * 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;
+    }
+ 
     ble.gap().onConnection(connectionCallback);
     ble.gap().onDisconnection(disconnectionCallback);
 
@@ -110,6 +133,17 @@
 
     ble.gap().setScanParams(500, 400);
     ble.gap().startScan(advertisementCallback);
+}
+
+int main(void) {
+    ticker.attach(periodicCallback, 1);
+
+    BLE &ble = BLE::Instance();
+    ble.init(bleInitComplete);
+
+    /* 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) {
         if (triggerLedCharacteristic && !ble.gattClient().isServiceDiscoveryActive()) {
diff -r 507318f2afda -r 023d96b0e427 mbed.bld
--- a/mbed.bld	Tue Sep 29 11:52:34 2015 +0000
+++ b/mbed.bld	Tue Jan 12 10:49:03 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/4f6c30876dfa
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/4336505e4b1c
\ No newline at end of file
diff -r 507318f2afda -r 023d96b0e427 nRF51822.lib
--- a/nRF51822.lib	Tue Sep 29 11:52:34 2015 +0000
+++ b/nRF51822.lib	Tue Jan 12 10:49:03 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#088f5738bf18
+http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#3cc0718d98d0