A very simple BLE temperature beacon.
Dependencies: BLE_API mbed nRF51822
Fork of BLE_TemperatureAdvertising by
Revision 7:0a8bbb6dea16, committed 2016-01-12
- Comitter:
- andresag
- Date:
- Tue Jan 12 10:12:43 2016 +0000
- Parent:
- 6:58fd6767069e
- Commit message:
- Update example to latest BLE API.
Changed in this revision
diff -r 58fd6767069e -r 0a8bbb6dea16 BLE_API.lib --- a/BLE_API.lib Tue Sep 29 12:07:28 2015 +0000 +++ b/BLE_API.lib Tue Jan 12 10:12:43 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#d494ad3e87bd +http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#bfc5b9b6ecf5
diff -r 58fd6767069e -r 0a8bbb6dea16 main.cpp --- a/main.cpp Tue Sep 29 12:07:28 2015 +0000 +++ b/main.cpp Tue Jan 12 10:12:43 2016 +0000 @@ -19,9 +19,7 @@ #include "ble/BLE.h" #include "TMP_nrf51/TMP_nrf51.h" -BLE ble; - -static Ticker ticker; +static Ticker ticker; static TMP_nrf51 tempSensor; static bool triggerTempValueUpdate = false; static DigitalOut alivenessLED(LED1, 1); @@ -43,12 +41,38 @@ void setupApplicationData(ApplicationData_t &appData) { static const uint16_t APP_SPECIFIC_ID_TEST = 0xFEFE; + appData.applicationSpecificId = APP_SPECIFIC_ID_TEST; appData.tmpSensorValue = tempSensor.get(); } -void startAdvertisingTemperature(void) +/** + * This function is called when the ble initialization process has failled + */ +void onBleInitError(BLE &ble, ble_error_t error) +{ + /* Initialization error handling should go here */ +} + +/** + * Callback triggered when the ble initialization process has finished + */ +void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) { + BLE& ble = params->ble; + ble_error_t error = params->error; + + if (error != BLE_ERROR_NONE) { + /* In case of error, forward the error handling to onBleInitError */ + onBleInitError(ble, error); + return; + } + + /* Ensure that it is the default instance of BLE */ + if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { + return; + } + /* Setup advertising payload */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); @@ -67,15 +91,20 @@ { ticker.attach(periodicCallback, 2); /* trigger sensor polling every 2 seconds */ - ble.init(); - startAdvertisingTemperature(); + BLE &ble = BLE::Instance(); + ble.init(bleInitComplete); + + /* SpinWait for initialization to complete. This is necessary because the + * BLE object is used in the main loop below. */ + while (ble.hasInitialized() == false) { /* spin loop */ } while (true) { if (triggerTempValueUpdate) { - // Do blocking calls or whatever hardware-specific action is necessary to poll the sensor. + /* Do blocking calls or whatever hardware-specific action is + * necessary to poll the sensor. */ ApplicationData_t appData; setupApplicationData(appData); - ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t)); + ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *) &appData, sizeof(ApplicationData_t)); triggerTempValueUpdate = false; }
diff -r 58fd6767069e -r 0a8bbb6dea16 mbed.bld --- a/mbed.bld Tue Sep 29 12:07:28 2015 +0000 +++ b/mbed.bld Tue Jan 12 10:12:43 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/4f6c30876dfa \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/4336505e4b1c \ No newline at end of file
diff -r 58fd6767069e -r 0a8bbb6dea16 nRF51822.lib --- a/nRF51822.lib Tue Sep 29 12:07:28 2015 +0000 +++ b/nRF51822.lib Tue Jan 12 10:12:43 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#088f5738bf18 +http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#3cc0718d98d0