Working program for data sending over BLE

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_GAP_Example by Bluetooth Low Energy

Revision:
16:9d681e53f310
Parent:
15:7e06fce6e4f8
--- a/main.cpp	Tue Jan 12 12:00:16 2016 +0000
+++ b/main.cpp	Fri Apr 28 12:47:23 2017 +0000
@@ -1,56 +1,64 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2015 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- 
+
 #include "mbed.h"
 #include "ble/BLE.h"
+#include "ble/Gap.h"
+#include "ble/UUID.h"
+#include "ble/GattCharacteristic.h"
 
-/* Optional: Device Name, add for human read-ability */
-const static char     DEVICE_NAME[] = "ChangeMe!!";
+BLEDevice ble;
+
+/* Device Name, simpel discovey */
+const static char     DEVICE_NAME[] = "ThisIsBLE1";
+
+/*SET UUID*/
+const UUID GATT_SERVICE = "88888888-5555-4444-1111-777777777777";
+const UUID ADV_DATA = "11001100-2200-3300-4400-550055005500";
+
+#define CHARACT_DATA 0
+
+
 
 /* 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 */
+// for
+uint8_t AdvData[] = {0x43,0x10,'d','u','m','m','y',0x02,0x02,0,1,7,81};   /* Example of hex data */
+uint8_t *datapointer = &AdvData[2];
+
+GattCharacteristic gatt_characteristics(ADV_DATA,AdvData,(sizeof(AdvData)-2),26,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
+
+GattCharacteristic *CHARACT_ARRAY[] = {&gatt_characteristics};
 
-/* Optional: Restart advertising when peer disconnects */
+GattService gatt_service(GATT_SERVICE,CHARACT_ARRAY,1);
+// end
+const BLEProtocol::AddressBytes_t  peerAddr = {0xb8, 0x27, 0xeb, 0x25, 0x00, 0xc4};
+BLEProtocol::AddressType_t         peerAddrType = (BLEProtocol::AddressType::RANDOM_STATIC);
+//const ConnectionParams_t          *connectionParams;
+//const GapScanningParams           *scanParams;
+
+
+/* Restart advertising when the peer disconnects */
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
     BLE::Instance().gap().startAdvertising();
 }
-/**
- * This function is called when the ble initialization process has failed
- */
+
+/*This function is called when the ble initialization process has failed*/
 void onBleInitError(BLE &ble, ble_error_t error)
 {
-    /* Avoid compiler warnings */
+    /* Avoid compiler warnings, library said to do this */
     (void) ble;
     (void) error;
     
     /* Initialization error handling should go here */
 }    
 
-/**
- * Callback triggered when the ble initialization process has finished
- */
+/*Callback triggered when ble initialization process has finished*/
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
 {
     BLE&        ble   = params->ble;
     ble_error_t error = params->error;
 
+/* In case of error, forward the error handling to onBleInitError */
     if (error != BLE_ERROR_NONE) {
-        /* In case of error, forward the error handling to onBleInitError */
         onBleInitError(ble, error);
         return;
     }
@@ -62,37 +70,52 @@
     
     /* Set device name characteristic data */
     ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);
-
-    /* Optional: add callback for disconnection */
+        
+    
+    /* callback when disconnected */
     ble.gap().onDisconnection(disconnectionCallback);
 
-    /* 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);
+    /* Sacrifice 3B of 31B to some Advertising Flags (see nrfconnect toolbox for flag parameters :) ) */
+    // for
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);// say this device can be discovered by other BLE supportingdevices
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // say that a connection can be set ( connectable) and that it does not matter with who it connects (unidirected)
 
-    /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
+    
+    /* Sacrifice another 2B of 31B (actually 29B) to AdvType overhead, rest goes to AdvData array --> about 26B of actual sendable data :o */
+    //Overhead  = 1B Length, 1B Type
+    //Type = AdvData
+    //Length = sizeof(AdvData)
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, AdvData, sizeof(AdvData));
 
-    /* Optional: Add name to device */
-    //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    /*fill in device name as local name*/
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+
+    /* Set advertising interval. Longer interval == Less radiothingies use == longer battery life :) */
+    ble.gap().setAdvertisingInterval(100); /* 100ms */
+    
+
 
-    /* Set advertising interval. Longer interval == longer battery life */
-    ble.gap().setAdvertisingInterval(100); /* 100ms */
+    ble.addService(gatt_service);
 
+    //ble.gap().addData(GapAdvertisingData::SERVICE_DATA, (uint8_t *)AdvData, sizeof(AdvData));
     /* Start advertising */
-    ble.gap().startAdvertising();
+    ble.gap().startAdvertising(); //start sending advertising packets
+    
+    ble.initializeSecurity();
+    //end
 }
 
 int main(void)
 {
+    // VERSPILT ENERGIE wait(1);
     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
  
-    /* Initialize BLE baselayer, always do this first! */
+    /* Initialize BLE baselayer, library said : "always do this first!" so we will do  */
     ble.init(bleInitComplete);
 
     /* Infinite loop waiting for BLE events */
     while (true) {
         /* Save power while waiting for callback events */
-        ble.waitForEvent();
+        ble.waitForEvent(); //this is like a sleep function, waking up on interrupts like disconnection or so 
     }
 }