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:
1:7202df456146
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#ifndef __BLE_BUTTON_SERVICE_H__
rgrover10:28f095301cb2 18#define __BLE_BUTTON_SERVICE_H__
rgrover10:28f095301cb2 19
rgrover10:28f095301cb2 20class ButtonService {
rgrover10:28f095301cb2 21public:
rgrover10:28f095301cb2 22 const static uint16_t BUTTON_SERVICE_UUID = 0xA000;
rgrover11:7202df456146 23 const static uint16_t BUTTON_STATE_CHARACTERISTIC_UUID = 0xA001;
rgrover10:28f095301cb2 24
rgrover16:18d8750f39ee 25 ButtonService(BLE &_ble, bool buttonPressedInitial) :
rgrover10:28f095301cb2 26 ble(_ble), buttonState(BUTTON_STATE_CHARACTERISTIC_UUID, &buttonPressedInitial, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
rgrover10:28f095301cb2 27 {
rgrover10:28f095301cb2 28 GattCharacteristic *charTable[] = {&buttonState};
rgrover10:28f095301cb2 29 GattService buttonService(ButtonService::BUTTON_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
rgrover16:18d8750f39ee 30 ble.gattServer().addService(buttonService);
rgrover10:28f095301cb2 31 }
rgrover10:28f095301cb2 32
rgrover10:28f095301cb2 33 void updateButtonState(bool newState) {
rgrover16:18d8750f39ee 34 ble.gattServer().write(buttonState.getValueHandle(), (uint8_t *)&newState, sizeof(bool));
rgrover10:28f095301cb2 35 }
rgrover10:28f095301cb2 36
rgrover10:28f095301cb2 37private:
rgrover16:18d8750f39ee 38 BLE &ble;
rgrover10:28f095301cb2 39 ReadOnlyGattCharacteristic<bool> buttonState;
rgrover10:28f095301cb2 40};
rgrover10:28f095301cb2 41
rgrover10:28f095301cb2 42#endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */