ble transmitter

Dependencies:   mbed BLE_API nRF51822

Revision:
3:76b8337374e7
Parent:
2:f8dfdded83bf
Child:
4:c3123e9e7d39
--- a/transmitter_main.cpp	Wed Apr 17 20:32:42 2019 +0000
+++ b/transmitter_main.cpp	Wed Apr 17 21:05:11 2019 +0000
@@ -8,51 +8,63 @@
 #define BATTERY_PIN p1
 
 DigitalOut led1(p21);
-const static char DEVICE_NAME[] = "Joe-Aidan";
 
 struct Packet {
-    uint32_t increasing_sequence;
+    uint32_t seqNum;
 };
 
+BLE ble;
+const static char DEVICE_NAME[] = "JOEY";
 static volatile bool  triggerSensorPolling = false;
+
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
     BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
 }
+
+void updatePayload(void)
+{
+    static Packet p;
+    ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&p, sizeof(p));
+    (p.seqNum)++;
+}
+
 void periodicCallback(void)
 {
     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
+    updatePayload();
+    
     /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
      * heavy-weight sensor polling from the main thread. */
     triggerSensorPolling = true;
 }
+
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
 {
-    BLE &ble          = params->ble;
+    BLE &localble          = params->ble;
     ble_error_t error = params->error;
+    
     if (error != BLE_ERROR_NONE) {
         return;
     }
-    ble.gap().onDisconnection(disconnectionCallback);
-    /* Setup primary service. */
-    // hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
-    /* Setup auxiliary service. */
-    // deviceInfo = new DeviceInformationService(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
-    /* 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::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
-    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.gap().setAdvertisingInterval(1000); /* 1000ms */
-    ble.gap().startAdvertising();
+    
+    localble.gap().onDisconnection(disconnectionCallback);
+    
+    localble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    updatePayload();
+    
+    localble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    localble.gap().setAdvertisingInterval(1000); /* 1000ms */
+    localble.gap().startAdvertising();
 }
+
 int main(void)
 {
     led1 = 1;
+    
     Ticker ticker;
-    ticker.attach(periodicCallback, 1); // blink LED every second
-    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
+    ticker.attach(periodicCallback, 1);
+    
     ble.init(bleInitComplete);
     
     /* SpinWait for initialization to complete. This is necessary because the