wattx / Mbed 2 deprecated BLE_GAP_Example

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_GAP_Example by Bluetooth Low Energy

Revision:
16:f3a913008c73
Parent:
15:7e06fce6e4f8
Child:
17:76aa0560ca15
diff -r 7e06fce6e4f8 -r f3a913008c73 main.cpp
--- a/main.cpp	Tue Jan 12 12:00:16 2016 +0000
+++ b/main.cpp	Tue Sep 06 13:52:26 2016 +0000
@@ -17,13 +17,40 @@
 #include "mbed.h"
 #include "ble/BLE.h"
 
+Ticker timer;
+
+DigitalIn myInputPin(P0_10);
+
+BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
+
 /* Optional: Device Name, add for human read-ability */
 const static char     DEVICE_NAME[] = "ChangeMe!!";
 
 /* 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 */
+uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x17};   /* Example of hex data */
 //const static uint8_t AdvData[] = {"ChangeThisData"};         /* Example of character data */
 
+
+class AdvertisementPacket {
+public:
+    AdvertisementPacket(BLE&);
+    void updateAdvertisementPacket(){
+        
+        AdvData[1] = myInputPin;
+        
+        ble.gap().clearAdvertisingPayload();
+        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
+        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
+        };
+private:
+    BLE &ble;
+};
+
+AdvertisementPacket::AdvertisementPacket(BLE &bleIn): ble(bleIn){
+        
+};
+/*
+
 /* Optional: Restart advertising when peer disconnects */
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
@@ -68,7 +95,16 @@
 
     /* 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);
+    //ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    
+    
+     /* Configure advertisements */
+    //ble.gap().setTxPower(radioPowerLevels[txPowerMode]);
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
+    //ble.gap().setAdvertisingInterval(beaconPeriod);
+    //ble.gap().onRadioNotification(this, &EddystoneService::radioNotificationCallback);
+    //ble.gap().initRadioNotification();
+    
 
     /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
@@ -77,7 +113,13 @@
     //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
 
     /* Set advertising interval. Longer interval == longer battery life */
-    ble.gap().setAdvertisingInterval(100); /* 100ms */
+    ble.gap().setAdvertisingInterval(1000); /* 100ms */
+    
+    ble.gap().clearAdvertisingPayload();
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
+    
+    
 
     /* Start advertising */
     ble.gap().startAdvertising();
@@ -85,14 +127,22 @@
 
 int main(void)
 {
-    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
+    
+    
+    
  
     /* Initialize BLE baselayer, always do this first! */
     ble.init(bleInitComplete);
 
-    /* Infinite loop waiting for BLE events */
+    /* SpinWait for initialization to complete. This is necessary because the
+     * BLE object is used in the main loop below. */
+    while (!ble.hasInitialized()) { /* spin loop */ }
+    
+    AdvertisementPacket f(ble);
+
+    timer.attach( &f, &AdvertisementPacket::updateAdvertisementPacket, 1.0); 
+
     while (true) {
-        /* Save power while waiting for callback events */
         ble.waitForEvent();
     }
 }