MDS_2 / Mbed 2 deprecated BLE_connectionNdata

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_GAP_Example by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "ble/BLE.h"
00004 #include "ble/Gap.h"
00005 #include "ble/UUID.h"
00006 #include "ble/GattCharacteristic.h"
00007 
00008 BLEDevice ble;
00009 
00010 /* Device Name, simpel discovey */
00011 const static char     DEVICE_NAME[] = "ThisIsBLE1";
00012 
00013 /*SET UUID*/
00014 const UUID GATT_SERVICE = "88888888-5555-4444-1111-777777777777";
00015 const UUID ADV_DATA = "11001100-2200-3300-4400-550055005500";
00016 
00017 #define CHARACT_DATA 0
00018 
00019 
00020 
00021 /* You have up to 26 bytes of advertising data to use. */
00022 // for
00023 uint8_t AdvData[] = {0x43,0x10,'d','u','m','m','y',0x02,0x02,0,1,7,81};   /* Example of hex data */
00024 uint8_t *datapointer = &AdvData[2];
00025 
00026 GattCharacteristic gatt_characteristics(ADV_DATA,AdvData,(sizeof(AdvData)-2),26,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00027 
00028 GattCharacteristic *CHARACT_ARRAY[] = {&gatt_characteristics};
00029 
00030 GattService gatt_service(GATT_SERVICE,CHARACT_ARRAY,1);
00031 // end
00032 const BLEProtocol::AddressBytes_t  peerAddr = {0xb8, 0x27, 0xeb, 0x25, 0x00, 0xc4};
00033 BLEProtocol::AddressType_t         peerAddrType = (BLEProtocol::AddressType::RANDOM_STATIC);
00034 //const ConnectionParams_t          *connectionParams;
00035 //const GapScanningParams           *scanParams;
00036 
00037 
00038 /* Restart advertising when the peer disconnects */
00039 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00040 {
00041     BLE::Instance().gap().startAdvertising();
00042 }
00043 
00044 /*This function is called when the ble initialization process has failed*/
00045 void onBleInitError(BLE &ble, ble_error_t error)
00046 {
00047     /* Avoid compiler warnings, library said to do this */
00048     (void) ble;
00049     (void) error;
00050     
00051     /* Initialization error handling should go here */
00052 }    
00053 
00054 /*Callback triggered when ble initialization process has finished*/
00055 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00056 {
00057     BLE&        ble   = params->ble;
00058     ble_error_t error = params->error;
00059 
00060 /* In case of error, forward the error handling to onBleInitError */
00061     if (error != BLE_ERROR_NONE) {
00062         onBleInitError(ble, error);
00063         return;
00064     }
00065 
00066     /* Ensure that it is the default instance of BLE */
00067     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00068         return;
00069     }
00070     
00071     /* Set device name characteristic data */
00072     ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);
00073         
00074     
00075     /* callback when disconnected */
00076     ble.gap().onDisconnection(disconnectionCallback);
00077 
00078     /* Sacrifice 3B of 31B to some Advertising Flags (see nrfconnect toolbox for flag parameters :) ) */
00079     // for
00080     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);// say this device can be discovered by other BLE supportingdevices
00081     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)
00082 
00083     
00084     /* Sacrifice another 2B of 31B (actually 29B) to AdvType overhead, rest goes to AdvData array --> about 26B of actual sendable data :o */
00085     //Overhead  = 1B Length, 1B Type
00086     //Type = AdvData
00087     //Length = sizeof(AdvData)
00088     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, AdvData, sizeof(AdvData));
00089 
00090     /*fill in device name as local name*/
00091     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00092 
00093     /* Set advertising interval. Longer interval == Less radiothingies use == longer battery life :) */
00094     ble.gap().setAdvertisingInterval(100); /* 100ms */
00095     
00096 
00097 
00098     ble.addService(gatt_service);
00099 
00100     //ble.gap().addData(GapAdvertisingData::SERVICE_DATA, (uint8_t *)AdvData, sizeof(AdvData));
00101     /* Start advertising */
00102     ble.gap().startAdvertising(); //start sending advertising packets
00103     
00104     ble.initializeSecurity();
00105     //end
00106 }
00107 
00108 int main(void)
00109 {
00110     // VERSPILT ENERGIE wait(1);
00111     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
00112  
00113     /* Initialize BLE baselayer, library said : "always do this first!" so we will do  */
00114     ble.init(bleInitComplete);
00115 
00116     /* Infinite loop waiting for BLE events */
00117     while (true) {
00118         /* Save power while waiting for callback events */
00119         ble.waitForEvent(); //this is like a sleep function, waking up on interrupts like disconnection or so 
00120     }
00121 }