Luca Mottola / Mbed OS IOTAtelier1819-BLEBeacon
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <events/mbed_events.h>
00002 #include <mbed.h>
00003 #include "ble/BLE.h"
00004 #include "ble/services/iBeacon.h"
00005 
00006 static iBeacon* ibeaconPtr;
00007 
00008 static EventQueue eventQueue(/* event count */ 4 * EVENTS_EVENT_SIZE);
00009 
00010 /**
00011  * This function is called when the ble initialization process has failled
00012  */
00013 void onBleInitError(BLE &ble, ble_error_t error)
00014 {
00015     /* Initialization error handling should go here */
00016 }
00017 
00018 /**
00019  * Callback triggered when the ble initialization process has finished
00020  */
00021 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00022 {
00023     BLE&        ble   = params->ble;
00024     ble_error_t error = params->error;
00025 
00026     if (error != BLE_ERROR_NONE) {
00027         /* In case of error, forward the error handling to onBleInitError */
00028         onBleInitError(ble, error);
00029         return;
00030     }
00031 
00032     /* Ensure that it is the default instance of BLE */
00033     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00034         return;
00035     }
00036 
00037     /**
00038      * The Beacon payload has the following composition:
00039      * 128-Bit / 16byte UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61
00040      * Major/Minor  = 0x1122 / 0x3344
00041      * Tx Power     = 0xC8 = 200, 2's compliment is 256-200 = (-56dB)
00042      *
00043      * Note: please remember to calibrate your beacons TX Power for more accurate results
00044      */
00045     static const uint8_t uuid[] = {0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4,
00046                                    0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61};
00047     uint16_t majorNumber = 1122;
00048     uint16_t minorNumber = 3344;
00049     uint16_t txPower     = 0xC8;
00050     ibeaconPtr = new iBeacon(ble, uuid, majorNumber, minorNumber, txPower);
00051 
00052     ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
00053     ble.gap().startAdvertising();
00054 }
00055 
00056 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
00057     BLE &ble = BLE::Instance();
00058     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
00059 }
00060 
00061 int main()
00062 {
00063     BLE &ble = BLE::Instance();
00064     ble.onEventsToProcess(scheduleBleEventsProcessing);
00065     ble.init(bleInitComplete);
00066 
00067     eventQueue.dispatch_forever();
00068 
00069     return 0;
00070 }