BLE Observer example for nucleo platform

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Revision:
5:346bcd62f251
Parent:
4:2aefa62bdce5
Child:
6:1c20e1891e4b
--- a/main.cpp	Mon Nov 14 10:20:45 2016 +0000
+++ b/main.cpp	Mon Nov 14 15:29:43 2016 +0000
@@ -23,9 +23,13 @@
 
 // Change your device name below
 const char DEVICE_NAME[] = "Nucleo_test";
+
+#define UUID 0x00FF        //Beacon UUID
+const uint16_t uuid16_list[] = {UUID};
  
 // Control Beacon read
 static int beacon = 0; 
+
 void periodicCallback(void)
 {
     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
@@ -47,7 +51,7 @@
     
 if (params->peerAddr[0] ==  0xA8) {
     
-    if (abs(params->rssi) <= 60) {          // Beacon 2 necessita di una soglia di vicinanza più alta  
+    if (abs(params->rssi) <= 60) {           
     printf("Beacon_2: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n",
          params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
          params->rssi);
@@ -84,39 +88,31 @@
 
     // Put the device name in the advertising payload
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
-      
+    
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+  
     /* Optional: add callback for disconnection */
     ble.gap().onDisconnection(disconnectionCallback);
     
     // The Service Data data type consists of a service UUID with the data associated with that service.
     // We will encode the number of beacon received
-    //uint8_t service_data[3];
-    //service_data[0] = MajorID & 0xff;
-    //service_data[1] = MinorID & 0xff;
-    //service_data[2] = TXPOWER & 0xff;
-    //err = ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)service_data, sizeof(service_data));
-    //if (err != BLE_ERROR_NONE) {
-    //    print_error(err, "Setting service data failed");
-    //    return;
-    //}    
+    uint8_t service_data[2];
+    service_data[0] = UUID & 0xff;
+    service_data[1] = beacon & 0xff;
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)service_data, sizeof(service_data));  
   
     // It is not connectable as we are just boardcasting
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
 
     //Send out the advertising payload every 1000ms
     ble.gap().setAdvertisingInterval(1000);
-
-    //ble.gap().startAdvertising();
-
 }
 
 void bleScanInitComplete(BLE::InitializationCompleteCallbackContext *context) {
             
-            ble_Scan.gap().setScanParams(100 /* scan interval */, 50 /* scan window */, 1);
+    ble_Scan.gap().setScanParams(100 /* scan interval */, 50 /* scan window */, 1);
             
 }
-
-//extern "C" void mbed_reset();
  
  
 int main(void)
@@ -127,47 +123,32 @@
     
      BLE &ble = BLE::Instance();
      BLE &ble_Scan = BLE::Instance();
- 
-    //ble.init(bleInitComplete);
-    //ble_Scan.init();
 
-    //ble_Scan.gap().setScanParams(100 /* scan interval */, 50 /* scan window */, 1);
-    //ble_Scan.gap().startScan(advertisementCallback);
-    //ble.gap().startAdvertising();
-
-int stato = 1; 
+int status = 1; 
 
      /* Infinite loop waiting for BLE events */
      while (true) {   
-     switch (stato) {
+     switch (status) {
       case(0):
-             //BLE &ble = BLE::Instance();
            printf("Advertising stato 0\r\n");
-           //ble.gap().stopScan();
            ble.init(bleInitComplete);
            ble.gap().startAdvertising();
-           wait(10);
+           wait(1);
            ble.gap().stopAdvertising();
-           wait(1);
-           stato = 1;
+           status = 1;
            NVIC_SystemReset();
            break;
      case(1): 
-             //BLE &ble_Scan = BLE::Instance();
+           printf("Scan stato 1\r\n");
            ble_Scan.init(bleScanInitComplete);
-           printf("Scan stato 1\r\n");
-           //ble.gap().stopAdvertising();
-           //ble_Scan.gap().setScanParams(100 /* scan interval */, 50 /* scan window */, 1);
            ble_Scan.gap().startScan(advertisementCallback);
-           wait(5);
-           //ble_Scan.gap().stopScan();
-          stato = 0;
-          
-          break;
+           wait(1);
+           status = 0;
+           break;
           }
         
-        /* Save power while waiting for callback events */
-       ble.waitForEvent();
-        ble_Scan.waitForEvent();
-       }
+     /* Save power while waiting for callback events */
+    ble.waitForEvent();
+    ble_Scan.waitForEvent();
+    }
 }