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.
main.cpp@10:af76616e4d75, 2016-01-12 (annotated)
- Committer:
- andresag
- Date:
- Tue Jan 12 10:34:34 2016 +0000
- Revision:
- 10:af76616e4d75
- Parent:
- 9:35a5a5796286
Update example to latest BLE API.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 0:ed5c53845de7 | 1 | /* mbed Microcontroller Library |
rgrover1 | 0:ed5c53845de7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 0:ed5c53845de7 | 3 | * |
rgrover1 | 0:ed5c53845de7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 0:ed5c53845de7 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 0:ed5c53845de7 | 6 | * You may obtain a copy of the License at |
rgrover1 | 0:ed5c53845de7 | 7 | * |
rgrover1 | 0:ed5c53845de7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 0:ed5c53845de7 | 9 | * |
rgrover1 | 0:ed5c53845de7 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 0:ed5c53845de7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 0:ed5c53845de7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 0:ed5c53845de7 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 0:ed5c53845de7 | 14 | * limitations under the License. |
rgrover1 | 0:ed5c53845de7 | 15 | */ |
rgrover1 | 0:ed5c53845de7 | 16 | |
rgrover1 | 0:ed5c53845de7 | 17 | #include "mbed.h" |
andresag | 10:af76616e4d75 | 18 | #include "ble/BLE.h" |
rgrover1 | 0:ed5c53845de7 | 19 | #include "LEDService.h" |
rgrover1 | 0:ed5c53845de7 | 20 | |
andresag | 10:af76616e4d75 | 21 | DigitalOut alivenessLED(LED1, 0); |
andresag | 10:af76616e4d75 | 22 | DigitalOut actuatedLED(LED2, 0); |
rgrover1 | 0:ed5c53845de7 | 23 | |
rgrover1 | 0:ed5c53845de7 | 24 | const static char DEVICE_NAME[] = "LED"; |
rgrover1 | 0:ed5c53845de7 | 25 | static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID}; |
rgrover1 | 0:ed5c53845de7 | 26 | |
rgrover1 | 0:ed5c53845de7 | 27 | LEDService *ledServicePtr; |
rgrover1 | 0:ed5c53845de7 | 28 | |
andresag | 10:af76616e4d75 | 29 | Ticker ticker; |
andresag | 10:af76616e4d75 | 30 | |
rgrover1 | 9:35a5a5796286 | 31 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
rgrover1 | 0:ed5c53845de7 | 32 | { |
andresag | 10:af76616e4d75 | 33 | BLE::Instance().gap().startAdvertising(); |
rgrover1 | 0:ed5c53845de7 | 34 | } |
rgrover1 | 0:ed5c53845de7 | 35 | |
rgrover1 | 0:ed5c53845de7 | 36 | void periodicCallback(void) |
rgrover1 | 0:ed5c53845de7 | 37 | { |
andresag | 10:af76616e4d75 | 38 | alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */ |
rgrover1 | 0:ed5c53845de7 | 39 | } |
rgrover1 | 0:ed5c53845de7 | 40 | |
rgrover1 | 0:ed5c53845de7 | 41 | /** |
rgrover1 | 0:ed5c53845de7 | 42 | * This callback allows the LEDService to receive updates to the ledState Characteristic. |
rgrover1 | 0:ed5c53845de7 | 43 | * |
rgrover1 | 0:ed5c53845de7 | 44 | * @param[in] params |
rgrover1 | 0:ed5c53845de7 | 45 | * Information about the characterisitc being updated. |
rgrover1 | 0:ed5c53845de7 | 46 | */ |
rgrover1 | 7:c9271599ec5f | 47 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
rgrover1 | 7:c9271599ec5f | 48 | if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { |
rgrover1 | 0:ed5c53845de7 | 49 | actuatedLED = *(params->data); |
rgrover1 | 0:ed5c53845de7 | 50 | } |
rgrover1 | 0:ed5c53845de7 | 51 | } |
rgrover1 | 0:ed5c53845de7 | 52 | |
andresag | 10:af76616e4d75 | 53 | /** |
andresag | 10:af76616e4d75 | 54 | * This function is called when the ble initialization process has failed |
andresag | 10:af76616e4d75 | 55 | */ |
andresag | 10:af76616e4d75 | 56 | void onBleInitError(BLE &ble, ble_error_t error) |
rgrover1 | 0:ed5c53845de7 | 57 | { |
andresag | 10:af76616e4d75 | 58 | /* Initialization error handling should go here */ |
andresag | 10:af76616e4d75 | 59 | } |
rgrover1 | 0:ed5c53845de7 | 60 | |
andresag | 10:af76616e4d75 | 61 | /** |
andresag | 10:af76616e4d75 | 62 | * Callback triggered when the ble initialization process has finished |
andresag | 10:af76616e4d75 | 63 | */ |
andresag | 10:af76616e4d75 | 64 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
andresag | 10:af76616e4d75 | 65 | { |
andresag | 10:af76616e4d75 | 66 | BLE& ble = params->ble; |
andresag | 10:af76616e4d75 | 67 | ble_error_t error = params->error; |
rgrover1 | 0:ed5c53845de7 | 68 | |
andresag | 10:af76616e4d75 | 69 | if (error != BLE_ERROR_NONE) { |
andresag | 10:af76616e4d75 | 70 | /* In case of error, forward the error handling to onBleInitError */ |
andresag | 10:af76616e4d75 | 71 | onBleInitError(ble, error); |
andresag | 10:af76616e4d75 | 72 | return; |
andresag | 10:af76616e4d75 | 73 | } |
andresag | 10:af76616e4d75 | 74 | |
andresag | 10:af76616e4d75 | 75 | /* Ensure that it is the default instance of BLE */ |
andresag | 10:af76616e4d75 | 76 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
andresag | 10:af76616e4d75 | 77 | return; |
andresag | 10:af76616e4d75 | 78 | } |
andresag | 10:af76616e4d75 | 79 | |
rgrover1 | 7:c9271599ec5f | 80 | ble.gap().onDisconnection(disconnectionCallback); |
rgrover1 | 7:c9271599ec5f | 81 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
rgrover1 | 0:ed5c53845de7 | 82 | |
rgrover1 | 0:ed5c53845de7 | 83 | bool initialValueForLEDCharacteristic = false; |
andresag | 10:af76616e4d75 | 84 | ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic); |
rgrover1 | 0:ed5c53845de7 | 85 | |
rgrover1 | 0:ed5c53845de7 | 86 | /* setup advertising */ |
rgrover1 | 7:c9271599ec5f | 87 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
rgrover1 | 7:c9271599ec5f | 88 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
rgrover1 | 7:c9271599ec5f | 89 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
rgrover1 | 7:c9271599ec5f | 90 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
rgrover1 | 7:c9271599ec5f | 91 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
rgrover1 | 7:c9271599ec5f | 92 | ble.gap().startAdvertising(); |
andresag | 10:af76616e4d75 | 93 | } |
andresag | 10:af76616e4d75 | 94 | |
andresag | 10:af76616e4d75 | 95 | int main(void) |
andresag | 10:af76616e4d75 | 96 | { |
andresag | 10:af76616e4d75 | 97 | ticker.attach(periodicCallback, 1); /* Blink LED every second */ |
andresag | 10:af76616e4d75 | 98 | |
andresag | 10:af76616e4d75 | 99 | BLE &ble = BLE::Instance(); |
andresag | 10:af76616e4d75 | 100 | ble.init(bleInitComplete); |
andresag | 10:af76616e4d75 | 101 | |
andresag | 10:af76616e4d75 | 102 | /* SpinWait for initialization to complete. This is necessary because the |
andresag | 10:af76616e4d75 | 103 | * BLE object is used in the main loop below. */ |
andresag | 10:af76616e4d75 | 104 | while (ble.hasInitialized() == false) { /* spin loop */ } |
rgrover1 | 0:ed5c53845de7 | 105 | |
rgrover1 | 0:ed5c53845de7 | 106 | while (true) { |
rgrover1 | 0:ed5c53845de7 | 107 | ble.waitForEvent(); |
rgrover1 | 0:ed5c53845de7 | 108 | } |
rgrover1 | 0:ed5c53845de7 | 109 | } |