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:
Tue Sep 29 09:58:02 2015 +0000
Revision:
8:a7ba7aaba460
Parent:
6:18d8750f39ee
Child:
9:0f6951db24f1
updating underlying libraries.

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 0:28f095301cb2 28 ButtonService *buttonServicePtr;
rgrover1 0:28f095301cb2 29
rgrover1 0:28f095301cb2 30 void buttonPressedCallback(void)
rgrover1 0:28f095301cb2 31 {
rgrover1 0:28f095301cb2 32 buttonServicePtr->updateButtonState(true);
rgrover1 0:28f095301cb2 33 }
rgrover1 0:28f095301cb2 34
rgrover1 0:28f095301cb2 35 void buttonReleasedCallback(void)
rgrover1 0:28f095301cb2 36 {
rgrover1 0:28f095301cb2 37 buttonServicePtr->updateButtonState(false);
rgrover1 0:28f095301cb2 38 }
rgrover1 0:28f095301cb2 39
rgrover1 8:a7ba7aaba460 40 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
rgrover1 0:28f095301cb2 41 {
rgrover1 6:18d8750f39ee 42 ble.gap().startAdvertising();
rgrover1 0:28f095301cb2 43 }
rgrover1 0:28f095301cb2 44
rgrover1 0:28f095301cb2 45 void periodicCallback(void)
rgrover1 0:28f095301cb2 46 {
rgrover1 0:28f095301cb2 47 led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
rgrover1 0:28f095301cb2 48 }
rgrover1 0:28f095301cb2 49
rgrover1 0:28f095301cb2 50 int main(void)
rgrover1 0:28f095301cb2 51 {
rgrover1 0:28f095301cb2 52 led1 = 1;
rgrover1 0:28f095301cb2 53 Ticker ticker;
rgrover1 0:28f095301cb2 54 ticker.attach(periodicCallback, 1);
rgrover1 0:28f095301cb2 55 button.fall(buttonPressedCallback);
rgrover1 0:28f095301cb2 56 button.rise(buttonReleasedCallback);
rgrover1 0:28f095301cb2 57
rgrover1 0:28f095301cb2 58 ble.init();
rgrover1 6:18d8750f39ee 59 ble.gap().onDisconnection(disconnectionCallback);
rgrover1 0:28f095301cb2 60
rgrover1 0:28f095301cb2 61 ButtonService buttonService(ble, false /* initial value for button pressed */);
rgrover1 0:28f095301cb2 62 buttonServicePtr = &buttonService;
rgrover1 0:28f095301cb2 63
rgrover1 0:28f095301cb2 64 /* setup advertising */
rgrover1 6:18d8750f39ee 65 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 6:18d8750f39ee 66 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
rgrover1 6:18d8750f39ee 67 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 6:18d8750f39ee 68 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 6:18d8750f39ee 69 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
rgrover1 6:18d8750f39ee 70 ble.gap().startAdvertising();
rgrover1 0:28f095301cb2 71
rgrover1 0:28f095301cb2 72 while (true) {
rgrover1 0:28f095301cb2 73 ble.waitForEvent();
rgrover1 0:28f095301cb2 74 }
rgrover1 0:28f095301cb2 75 }