ST / Mbed OS mbed-os-example-ble-Beacon
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <events/mbed_events.h>
00018 #include <mbed.h>
00019 #include "ble/BLE.h"
00020 #include "ble/services/iBeacon.h"
00021 
00022 static iBeacon* ibeaconPtr;
00023 
00024 static EventQueue eventQueue(/* event count */ 4 * EVENTS_EVENT_SIZE);
00025 
00026 /**
00027  * This function is called when the ble initialization process has failled
00028  */
00029 void onBleInitError(BLE &ble, ble_error_t error)
00030 {
00031     /* Initialization error handling should go here */
00032 }
00033 
00034 /**
00035  * Callback triggered when the ble initialization process has finished
00036  */
00037 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00038 {
00039     BLE&        ble   = params->ble;
00040     ble_error_t error = params->error;
00041 
00042     if (error != BLE_ERROR_NONE) {
00043         /* In case of error, forward the error handling to onBleInitError */
00044         onBleInitError(ble, error);
00045         return;
00046     }
00047 
00048     /* Ensure that it is the default instance of BLE */
00049     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00050         return;
00051     }
00052 
00053     /**
00054      * The Beacon payload has the following composition:
00055      * 128-Bit / 16byte UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61
00056      * Major/Minor  = 0x1122 / 0x3344
00057      * Tx Power     = 0xC8 = 200, 2's compliment is 256-200 = (-56dB)
00058      *
00059      * Note: please remember to calibrate your beacons TX Power for more accurate results.
00060      */
00061     static const uint8_t uuid[] = {0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4,
00062                                    0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61};
00063     uint16_t majorNumber = 1122;
00064     uint16_t minorNumber = 3344;
00065     uint16_t txPower     = 0xC8;
00066     ibeaconPtr = new iBeacon(ble, uuid, majorNumber, minorNumber, txPower);
00067 
00068     ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
00069     ble.gap().startAdvertising();
00070 }
00071 
00072 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
00073     BLE &ble = BLE::Instance();
00074     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
00075 }
00076 
00077 int main()
00078 {
00079     BLE &ble = BLE::Instance();
00080     ble.onEventsToProcess(scheduleBleEventsProcessing);
00081     ble.init(bleInitComplete);
00082 
00083     eventQueue.dispatch_forever();
00084 
00085     return 0;
00086 }