A template for applications where some small amount of data needs to be notified to a phone app over BLE. It is a good starting point for notifications.

Dependencies:   BLE_API mbed nRF51822

Demo for an Input Service

To help you create your own BLE services, we've created a series of service templates. The *input service template* demonstrates the use of a simple input (boolean values) from a read-only characteristic.

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-only, boolean, with notifications. This characteristic is updated according to the button's state.

4. Constructing a service class and adding it to the BLE stack.

Committer:
rgrover1
Date:
Fri Oct 09 13:37:52 2015 +0000
Revision:
9:0f6951db24f1
Parent:
8:a7ba7aaba460
Child:
10:7943b5c1117a
pulling in an improvement suggested by Andrea Palmieri

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:28f095301cb2 1 /* mbed Microcontroller Library
rgrover1 0:28f095301cb2 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 0:28f095301cb2 3 *
rgrover1 0:28f095301cb2 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:28f095301cb2 5 * you may not use this file except in compliance with the License.
rgrover1 0:28f095301cb2 6 * You may obtain a copy of the License at
rgrover1 0:28f095301cb2 7 *
rgrover1 0:28f095301cb2 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:28f095301cb2 9 *
rgrover1 0:28f095301cb2 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:28f095301cb2 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:28f095301cb2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:28f095301cb2 13 * See the License for the specific language governing permissions and
rgrover1 0:28f095301cb2 14 * limitations under the License.
rgrover1 0:28f095301cb2 15 */
rgrover1 0:28f095301cb2 16
rgrover1 0:28f095301cb2 17 #include "mbed.h"
rgrover1 5:43a3ab27f2e4 18 #include "BLE.h"
rgrover1 0:28f095301cb2 19 #include "ButtonService.h"
rgrover1 0:28f095301cb2 20
rgrover1 6:18d8750f39ee 21 BLE ble;
rgrover1 0:28f095301cb2 22 DigitalOut led1(LED1);
rgrover1 0:28f095301cb2 23 InterruptIn button(BUTTON1);
rgrover1 0:28f095301cb2 24
rgrover1 0:28f095301cb2 25 const static char DEVICE_NAME[] = "Button";
rgrover1 0:28f095301cb2 26 static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID};
rgrover1 0:28f095301cb2 27
rgrover1 9:0f6951db24f1 28 enum {
rgrover1 9:0f6951db24f1 29 RELEASED = 0,
rgrover1 9:0f6951db24f1 30 PRESSED,
rgrover1 9:0f6951db24f1 31 IDLE
rgrover1 9:0f6951db24f1 32 };
rgrover1 9:0f6951db24f1 33 static uint8_t buttonState = IDLE;
rgrover1 9:0f6951db24f1 34
rgrover1 0:28f095301cb2 35 ButtonService *buttonServicePtr;
rgrover1 0:28f095301cb2 36
rgrover1 0:28f095301cb2 37 void buttonPressedCallback(void)
rgrover1 0:28f095301cb2 38 {
rgrover1 9:0f6951db24f1 39 /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access
rgrover1 9:0f6951db24f1 40 * BLE device API from the main thread. */
rgrover1 9:0f6951db24f1 41 buttonState = PRESSED;
rgrover1 0:28f095301cb2 42 }
rgrover1 0:28f095301cb2 43
rgrover1 0:28f095301cb2 44 void buttonReleasedCallback(void)
rgrover1 0:28f095301cb2 45 {
rgrover1 9:0f6951db24f1 46 /* Note that the buttonReleasedCallback() executes in interrupt context, so it is safer to access
rgrover1 9:0f6951db24f1 47 * BLE device API from the main thread. */
rgrover1 9:0f6951db24f1 48 buttonState = RELEASED;
rgrover1 0:28f095301cb2 49 }
rgrover1 0:28f095301cb2 50
rgrover1 8:a7ba7aaba460 51 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
rgrover1 0:28f095301cb2 52 {
rgrover1 6:18d8750f39ee 53 ble.gap().startAdvertising();
rgrover1 0:28f095301cb2 54 }
rgrover1 0:28f095301cb2 55
rgrover1 0:28f095301cb2 56 void periodicCallback(void)
rgrover1 0:28f095301cb2 57 {
rgrover1 0:28f095301cb2 58 led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
rgrover1 0:28f095301cb2 59 }
rgrover1 0:28f095301cb2 60
rgrover1 0:28f095301cb2 61 int main(void)
rgrover1 0:28f095301cb2 62 {
rgrover1 0:28f095301cb2 63 led1 = 1;
rgrover1 0:28f095301cb2 64 Ticker ticker;
rgrover1 0:28f095301cb2 65 ticker.attach(periodicCallback, 1);
rgrover1 0:28f095301cb2 66 button.fall(buttonPressedCallback);
rgrover1 0:28f095301cb2 67 button.rise(buttonReleasedCallback);
rgrover1 0:28f095301cb2 68
rgrover1 0:28f095301cb2 69 ble.init();
rgrover1 6:18d8750f39ee 70 ble.gap().onDisconnection(disconnectionCallback);
rgrover1 0:28f095301cb2 71
rgrover1 0:28f095301cb2 72 ButtonService buttonService(ble, false /* initial value for button pressed */);
rgrover1 0:28f095301cb2 73 buttonServicePtr = &buttonService;
rgrover1 0:28f095301cb2 74
rgrover1 0:28f095301cb2 75 /* setup advertising */
rgrover1 6:18d8750f39ee 76 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 6:18d8750f39ee 77 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
rgrover1 6:18d8750f39ee 78 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 6:18d8750f39ee 79 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 6:18d8750f39ee 80 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
rgrover1 6:18d8750f39ee 81 ble.gap().startAdvertising();
rgrover1 0:28f095301cb2 82
rgrover1 0:28f095301cb2 83 while (true) {
rgrover1 9:0f6951db24f1 84 if (buttonState!=IDLE) {
rgrover1 9:0f6951db24f1 85 buttonServicePtr->updateButtonState(buttonState);
rgrover1 9:0f6951db24f1 86 buttonState = IDLE;
rgrover1 9:0f6951db24f1 87 }
rgrover1 9:0f6951db24f1 88
rgrover1 0:28f095301cb2 89 ble.waitForEvent();
rgrover1 0:28f095301cb2 90 }
rgrover1 0:28f095301cb2 91 }