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:
andresag
Date:
Wed Dec 30 09:54:06 2015 +0000
Revision:
10:7943b5c1117a
Parent:
9:0f6951db24f1
Update example to comply with latest BLE API changes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover10:28f095301cb2 1/* mbed Microcontroller Library
rgrover10:28f095301cb2 2 * Copyright (c) 2006-2013 ARM Limited
rgrover10:28f095301cb2 3 *
rgrover10:28f095301cb2 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover10:28f095301cb2 5 * you may not use this file except in compliance with the License.
rgrover10:28f095301cb2 6 * You may obtain a copy of the License at
rgrover10:28f095301cb2 7 *
rgrover10:28f095301cb2 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover10:28f095301cb2 9 *
rgrover10:28f095301cb2 10 * Unless required by applicable law or agreed to in writing, software
rgrover10:28f095301cb2 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover10:28f095301cb2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover10:28f095301cb2 13 * See the License for the specific language governing permissions and
rgrover10:28f095301cb2 14 * limitations under the License.
rgrover10:28f095301cb2 15 */
rgrover10:28f095301cb2 16
rgrover10:28f095301cb2 17#include "mbed.h"
andresag10:7943b5c1117a 18#include "ble/BLE.h"
rgrover10:28f095301cb2 19#include "ButtonService.h"
rgrover10:28f095301cb2 20
rgrover10:28f095301cb2 21DigitalOut led1(LED1);
rgrover10:28f095301cb2 22InterruptIn button(BUTTON1);
rgrover10:28f095301cb2 23
rgrover10:28f095301cb2 24const static char DEVICE_NAME[] = "Button";
rgrover10:28f095301cb2 25static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID};
rgrover10:28f095301cb2 26
rgrover19:0f6951db24f1 27enum {
rgrover19:0f6951db24f1 28 RELEASED = 0,
rgrover19:0f6951db24f1 29 PRESSED,
rgrover19:0f6951db24f1 30 IDLE
rgrover19:0f6951db24f1 31};
rgrover19:0f6951db24f1 32static uint8_t buttonState = IDLE;
rgrover19:0f6951db24f1 33
andresag10:7943b5c1117a 34static ButtonService *buttonServicePtr;
rgrover10:28f095301cb2 35
rgrover10:28f095301cb2 36void buttonPressedCallback(void)
rgrover10:28f095301cb2 37{
rgrover19:0f6951db24f1 38 /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access
rgrover19:0f6951db24f1 39 * BLE device API from the main thread. */
rgrover19:0f6951db24f1 40 buttonState = PRESSED;
rgrover10:28f095301cb2 41}
rgrover10:28f095301cb2 42
rgrover10:28f095301cb2 43void buttonReleasedCallback(void)
rgrover10:28f095301cb2 44{
rgrover19:0f6951db24f1 45 /* Note that the buttonReleasedCallback() executes in interrupt context, so it is safer to access
rgrover19:0f6951db24f1 46 * BLE device API from the main thread. */
rgrover19:0f6951db24f1 47 buttonState = RELEASED;
rgrover10:28f095301cb2 48}
rgrover10:28f095301cb2 49
rgrover18:a7ba7aaba460 50void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
rgrover10:28f095301cb2 51{
andresag10:7943b5c1117a 52 BLE::Instance().gap().startAdvertising();
rgrover10:28f095301cb2 53}
rgrover10:28f095301cb2 54
rgrover10:28f095301cb2 55void periodicCallback(void)
rgrover10:28f095301cb2 56{
rgrover10:28f095301cb2 57 led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
rgrover10:28f095301cb2 58}
rgrover10:28f095301cb2 59
andresag10:7943b5c1117a 60/**
andresag10:7943b5c1117a 61 * This function is called when the ble initialization process has failled
andresag10:7943b5c1117a 62 */
andresag10:7943b5c1117a 63void onBleInitError(BLE &ble, ble_error_t error)
andresag10:7943b5c1117a 64{
andresag10:7943b5c1117a 65 /* Initialization error handling should go here */
andresag10:7943b5c1117a 66}
andresag10:7943b5c1117a 67
andresag10:7943b5c1117a 68/**
andresag10:7943b5c1117a 69 * Callback triggered when the ble initialization process has finished
andresag10:7943b5c1117a 70 */
andresag10:7943b5c1117a 71void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag10:7943b5c1117a 72{
andresag10:7943b5c1117a 73 BLE& ble = params->ble;
andresag10:7943b5c1117a 74 ble_error_t error = params->error;
andresag10:7943b5c1117a 75
andresag10:7943b5c1117a 76 if (error != BLE_ERROR_NONE) {
andresag10:7943b5c1117a 77 /* In case of error, forward the error handling to onBleInitError */
andresag10:7943b5c1117a 78 onBleInitError(ble, error);
andresag10:7943b5c1117a 79 return;
andresag10:7943b5c1117a 80 }
andresag10:7943b5c1117a 81
andresag10:7943b5c1117a 82 /* Ensure that it is the default instance of BLE */
andresag10:7943b5c1117a 83 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
andresag10:7943b5c1117a 84 return;
andresag10:7943b5c1117a 85 }
andresag10:7943b5c1117a 86
andresag10:7943b5c1117a 87 ble.gap().onDisconnection(disconnectionCallback);
andresag10:7943b5c1117a 88
andresag10:7943b5c1117a 89 /* Setup primary service */
andresag10:7943b5c1117a 90 buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */);
andresag10:7943b5c1117a 91
andresag10:7943b5c1117a 92 /* setup advertising */
andresag10:7943b5c1117a 93 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
andresag10:7943b5c1117a 94 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
andresag10:7943b5c1117a 95 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
andresag10:7943b5c1117a 96 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
andresag10:7943b5c1117a 97 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
andresag10:7943b5c1117a 98 ble.gap().startAdvertising();
andresag10:7943b5c1117a 99
andresag10:7943b5c1117a 100}
andresag10:7943b5c1117a 101
rgrover10:28f095301cb2 102int main(void)
rgrover10:28f095301cb2 103{
rgrover10:28f095301cb2 104 led1 = 1;
rgrover10:28f095301cb2 105 Ticker ticker;
rgrover10:28f095301cb2 106 ticker.attach(periodicCallback, 1);
rgrover10:28f095301cb2 107 button.fall(buttonPressedCallback);
rgrover10:28f095301cb2 108 button.rise(buttonReleasedCallback);
rgrover10:28f095301cb2 109
andresag10:7943b5c1117a 110 BLE &ble = BLE::Instance();
andresag10:7943b5c1117a 111 ble.init(bleInitComplete);
andresag10:7943b5c1117a 112
andresag10:7943b5c1117a 113 /* SpinWait for initialization to complete. This is necessary because the
andresag10:7943b5c1117a 114 * BLE object is used in the main loop below. */
andresag10:7943b5c1117a 115 while (ble.hasInitialized() == false) { /* spin loop */ }
andresag10:7943b5c1117a 116
rgrover10:28f095301cb2 117 while (true) {
andresag10:7943b5c1117a 118 if (buttonState != IDLE) {
rgrover19:0f6951db24f1 119 buttonServicePtr->updateButtonState(buttonState);
rgrover19:0f6951db24f1 120 buttonState = IDLE;
rgrover19:0f6951db24f1 121 }
rgrover19:0f6951db24f1 122
rgrover10:28f095301cb2 123 ble.waitForEvent();
rgrover10:28f095301cb2 124 }
rgrover10:28f095301cb2 125}