template for an actuator service
Dependencies: BLE_API mbed nRF51822
Demo for an Actuator Service
To help you create your own BLE services, we've created a series of service templates. The *actuator service template* demonstrates the use of a read-write characteristic to control a LED through a phone app.
The template covers:
1. Setting up advertising and connection states.
2. Assigning UUIDs to the service and its characteristic.
3. Creating an input characteristic: read-write, boolean. This characteristic offers control of the LED.
4. Constructing a service class and adding it to the BLE stack.
Revision 10:af76616e4d75, committed 2016-01-12
- Comitter:
- andresag
- Date:
- Tue Jan 12 10:34:34 2016 +0000
- Parent:
- 9:35a5a5796286
- Commit message:
- Update example to latest BLE API.
Changed in this revision
diff -r 35a5a5796286 -r af76616e4d75 BLE_API.lib --- a/BLE_API.lib Tue Sep 29 11:50:28 2015 +0000 +++ b/BLE_API.lib Tue Jan 12 10:34:34 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 35a5a5796286 -r af76616e4d75 main.cpp --- a/main.cpp Tue Sep 29 11:50:28 2015 +0000 +++ b/main.cpp Tue Jan 12 10:34:34 2016 +0000 @@ -15,26 +15,27 @@ */ #include "mbed.h" -#include "BLE.h" +#include "ble/BLE.h" #include "LEDService.h" -BLE ble; -DigitalOut alivenessLED(LED1); -DigitalOut actuatedLED(LED2); +DigitalOut alivenessLED(LED1, 0); +DigitalOut actuatedLED(LED2, 0); const static char DEVICE_NAME[] = "LED"; static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID}; LEDService *ledServicePtr; +Ticker ticker; + void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { - ble.gap().startAdvertising(); + BLE::Instance().gap().startAdvertising(); } void periodicCallback(void) { - //alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */ + alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */ } /** @@ -49,21 +50,38 @@ } } -int main(void) +/** + * This function is called when the ble initialization process has failed + */ +void onBleInitError(BLE &ble, ble_error_t error) { - alivenessLED = 0; - actuatedLED = 0; + /* Initialization error handling should go here */ +} - Ticker ticker; - ticker.attach(periodicCallback, 1); +/** + * Callback triggered when the ble initialization process has finished + */ +void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) +{ + BLE& ble = params->ble; + ble_error_t error = params->error; - ble.init(); + 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; + } + ble.gap().onDisconnection(disconnectionCallback); ble.gattServer().onDataWritten(onDataWrittenCallback); bool initialValueForLEDCharacteristic = false; - LEDService ledService(ble, initialValueForLEDCharacteristic); - ledServicePtr = &ledService; + ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic); /* setup advertising */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); @@ -72,6 +90,18 @@ ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ ble.gap().startAdvertising(); +} + +int main(void) +{ + ticker.attach(periodicCallback, 1); /* Blink LED every second */ + + 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) { ble.waitForEvent();
diff -r 35a5a5796286 -r af76616e4d75 mbed.bld --- a/mbed.bld Tue Sep 29 11:50:28 2015 +0000 +++ b/mbed.bld Tue Jan 12 10:34:34 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 35a5a5796286 -r af76616e4d75 nRF51822.lib --- a/nRF51822.lib Tue Sep 29 11:50:28 2015 +0000 +++ b/nRF51822.lib Tue Jan 12 10:34:34 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#088f5738bf18 +http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#3cc0718d98d0