Bluetooth Low Energy / Mbed 2 deprecated BLE_EddystoneBeaconConfigService Featured

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

Fork of BLE_EddystoneBeaconConfigServiceRelease by Austin Blackstone

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-2013 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 "mbed.h"
00018 #include "ble/BLE.h"
00019 #include "EddystoneService.h"
00020 
00021 EddystoneService *eddyServicePtr;
00022 
00023 /* Duration after power-on that config service is available. */
00024 static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 30;
00025 
00026 Ticker configAdvertisementTimeout;
00027 
00028 /**
00029  * Stop advertising the Config Service after a delay; and switch to a non-connectable advertising mode only beacon.
00030  */
00031 void timeout(void)
00032 {
00033     Gap::GapState_t state;
00034     state = BLE::Instance(BLE::DEFAULT_INSTANCE).gap().getState();
00035     if (!state.connected) { /* don't switch if we're in a connected state. */
00036         configAdvertisementTimeout.detach(); /* disable the callback from the timeout Ticker. */
00037         eddyServicePtr->startBeaconService(5, 5, 5);  
00038     }
00039 }
00040 
00041 /**
00042  * Callback triggered upon a disconnection event.
00043  */
00044 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *cbParams)
00045 {
00046     (void) cbParams;
00047     BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
00048 }
00049 
00050 void onBleInitError(BLE::InitializationCompleteCallbackContext* initContext)
00051 {
00052     /* Initialization error handling goes here... */
00053     (void) initContext;
00054 }
00055 
00056 void bleInitComplete(BLE::InitializationCompleteCallbackContext* initContext)
00057 {
00058     BLE         &ble  = initContext->ble;
00059     ble_error_t error = initContext->error;
00060 
00061     if (error != BLE_ERROR_NONE) {
00062         onBleInitError(initContext);
00063         return;
00064     }
00065 
00066     ble.gap().onDisconnection(disconnectionCallback);
00067 
00068     /* Set UID and TLM frame data */
00069     const UIDNamespaceID_t uidNamespaceID = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
00070     const UIDInstanceID_t  uidInstanceID  = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
00071     uint8_t tlmVersion = 0x00;
00072 
00073     /* Initialize a EddystoneBeaconConfig service providing config params, default URI, and power levels. */
00074     static const PowerLevels_t defaultAdvPowerLevels = {-47, -33, -21, -13}; // Values for ADV packets related to firmware levels, calibrated based on measured values at 1m
00075     static const PowerLevels_t radioPowerLevels      = {-30, -16, -4, 4};    // Values for radio power levels, provided by manufacturer.
00076 
00077     /* Set everything to defaults */
00078     eddyServicePtr = new EddystoneService(ble, defaultAdvPowerLevels, radioPowerLevels, 500);
00079 
00080     /* Set default URL, UID and TLM frame data if not initialized through the config service */
00081     eddyServicePtr->setURLData("http://mbed.org");
00082     eddyServicePtr->setUIDData(&uidNamespaceID, &uidInstanceID);
00083     eddyServicePtr->setTLMData(tlmVersion);
00084 
00085     /* Start Eddystone in config mode */
00086     eddyServicePtr->startConfigService();
00087 
00088     configAdvertisementTimeout.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS);
00089 }
00090 
00091 int main(void)
00092 {
00093     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
00094     ble.init(bleInitComplete);
00095 
00096     /* SpinWait for initialization to complete. This is necessary because the
00097      * BLE object is used in the main loop below. */
00098     while (ble.hasInitialized()  == false) { /* spin loop */ }
00099     
00100     while (true) {
00101         ble.waitForEvent(); /* this will return upon any system event (such as an interrupt or a ticker wakeup) */
00102     }
00103 }