Hugo Pristauz / Mbed 2 deprecated S05_Advertising

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Revision:
22:e82c7b8a6072
Parent:
21:0e7c08f5386f
Child:
23:f06c9851c72c
--- a/main.cpp	Wed Oct 05 09:16:58 2016 +0000
+++ b/main.cpp	Sat Dec 10 20:32:35 2016 +0000
@@ -13,96 +13,99 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
+ 
 #include "mbed.h"
 #include "ble/BLE.h"
-#include "ble/services/HeartRateService.h"
+#include "bricks/blob.h"
+#include "bricks/blink.h"
 
-DigitalOut led1(LED1, 1);
+   const char *idle = "x       ";               // 'idle' blinking sequence
+   const char *action = "x x x x x       ";     // 'action' blinking sequence
 
-const static char     DEVICE_NAME[]        = "HRM1";
-static const uint16_t uuid16_list[]        = {GattService::UUID_HEART_RATE_SERVICE};
+/* Optional: Device Name, add for human read-ability */
+const static char     DEVICE_NAME[] = "T05B_Advertising";
 
-static volatile bool  triggerSensorPolling = false;
+/* You have up to 26 bytes of advertising data to use. */
+const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05};   /* Example of hex data */
+//const static uint8_t AdvData[] = {"ChangeThisData"};         /* Example of character data */
 
+/* Optional: Restart advertising when peer disconnects */
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
-    (void)params;
-    BLE::Instance().gap().startAdvertising(); // restart advertising
+    BLE::Instance().gap().startAdvertising();
 }
-
-void periodicCallback(void)
-{
-    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
-    /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
-     * heavy-weight sensor polling from the main thread. */
-    triggerSensorPolling = true;
-}
-
+/**
+ * This function is called when the ble initialization process has failed
+ */
 void onBleInitError(BLE &ble, ble_error_t error)
 {
-    (void)ble;
-    (void)error;
-   /* Initialization error handling should go here */
-}
+    /* Avoid compiler warnings */
+    (void) ble;
+    (void) error;
+    
+    /* Initialization error handling should go here */
+}    
 
+/**
+ * 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;
     }
 
-    if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+    /* Ensure that it is the default instance of BLE */
+    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
         return;
     }
+    
+    /* Set device name characteristic data */
+    ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);
 
+    /* Optional: add callback for disconnection */
     ble.gap().onDisconnection(disconnectionCallback);
 
-    /* Setup primary service. */
-    uint8_t hrmCounter = 60; // init HRM to 60bps
-    HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
+    /* Sacrifice 3B of 31B to Advertising Flags */
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
 
-    /* Setup advertising. */
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
-    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); /* 1000ms */
-    ble.gap().startAdvertising();
+    /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
 
-    // infinite loop
-    while (true) {
-        // check for trigger from periodicCallback()
-        if (triggerSensorPolling && ble.getGapState().connected) {
-            triggerSensorPolling = false;
-
-            // Do blocking calls or whatever is necessary for sensor polling.
-            // In our case, we simply update the HRM measurement.
-            hrmCounter++;
+    /* Optional: Add name to device */
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
 
-            //  60 <= HRM bps <= 100
-            if (hrmCounter == 100) {
-                hrmCounter = 60;
-            }
+    /* Set advertising interval. Longer interval == longer battery life */
+    ble.gap().setAdvertisingInterval(100); /* 100ms */
 
-            // update bps
-            hrService.updateHeartRate(hrmCounter);
-        } else {
-            ble.waitForEvent(); // low power wait for event
-        }
-    }
+    /* Start advertising */
+    ble.gap().startAdvertising();
 }
 
-int main(void)
-{
-    Ticker ticker;
-    ticker.attach(periodicCallback, 1); // blink LED every second
+   int main(void)
+   {
+      blinking(idle);
+      wait(3.0);
+      blinking(action,idle);
+    
+      Blob blob;
 
-    BLE::Instance().init(bleInitComplete);
-}
+      //BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
+ 
+      /* Initialize BLE baselayer, always do this first! */
+      blob.pble->init(bleInitComplete);
 
+      /* Infinite loop waiting for BLE events */
+      while (true) 
+      {
+         /* Save power while waiting for callback events */
+         //blob.pble->waitForEvent();
+         blob.sleep();                   // low power waiting 
+      }
+   }