HID-over-GATT implementation with the BLE API. This library allows to create devices such as mouse, keyboard or joystick, over Bluetooth Low Energy.

Dependents:   MtConnect04S_Gesture_HID

Fork of BLE_HID by Jean-Philippe Brucker

Committer:
bcc6
Date:
Tue Jan 17 03:48:12 2017 +0000
Revision:
5:dc4e6dbcb79b
Add PNP_ID in DeviceInformationService; Add bootKeyboardInputReportCharacteristic, bootKeyboardOutputReportCharacteristic in HIDServiceBase

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcc6 5:dc4e6dbcb79b 1 /* mbed Microcontroller Library
bcc6 5:dc4e6dbcb79b 2 * Copyright (c) 2006-2013 ARM Limited
bcc6 5:dc4e6dbcb79b 3 *
bcc6 5:dc4e6dbcb79b 4 * Licensed under the Apache License, Version 2.0 (the "License");
bcc6 5:dc4e6dbcb79b 5 * you may not use this file except in compliance with the License.
bcc6 5:dc4e6dbcb79b 6 * You may obtain a copy of the License at
bcc6 5:dc4e6dbcb79b 7 *
bcc6 5:dc4e6dbcb79b 8 * http://www.apache.org/licenses/LICENSE-2.0
bcc6 5:dc4e6dbcb79b 9 *
bcc6 5:dc4e6dbcb79b 10 * Unless required by applicable law or agreed to in writing, software
bcc6 5:dc4e6dbcb79b 11 * distributed under the License is distributed on an "AS IS" BASIS,
bcc6 5:dc4e6dbcb79b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bcc6 5:dc4e6dbcb79b 13 * See the License for the specific language governing permissions and
bcc6 5:dc4e6dbcb79b 14 * limitations under the License.
bcc6 5:dc4e6dbcb79b 15 */
bcc6 5:dc4e6dbcb79b 16
bcc6 5:dc4e6dbcb79b 17 #ifndef __BLE_BATTERY_SERVICE_H__
bcc6 5:dc4e6dbcb79b 18 #define __BLE_BATTERY_SERVICE_H__
bcc6 5:dc4e6dbcb79b 19
bcc6 5:dc4e6dbcb79b 20 #include "ble/BLE.h"
bcc6 5:dc4e6dbcb79b 21
bcc6 5:dc4e6dbcb79b 22 /**
bcc6 5:dc4e6dbcb79b 23 * @class BatteryService
bcc6 5:dc4e6dbcb79b 24 * @brief BLE Battery Service. This service displays the battery level from 0% to 100%, represented as an 8bit number.
bcc6 5:dc4e6dbcb79b 25 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml
bcc6 5:dc4e6dbcb79b 26 * Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml
bcc6 5:dc4e6dbcb79b 27 */
bcc6 5:dc4e6dbcb79b 28 class BatteryService {
bcc6 5:dc4e6dbcb79b 29 public:
bcc6 5:dc4e6dbcb79b 30 /**
bcc6 5:dc4e6dbcb79b 31 * @param[in] _ble
bcc6 5:dc4e6dbcb79b 32 * BLE object for the underlying controller.
bcc6 5:dc4e6dbcb79b 33 * @param[in] level
bcc6 5:dc4e6dbcb79b 34 * 8bit batterly level. Usually used to represent percentage of batterly charge remaining.
bcc6 5:dc4e6dbcb79b 35 */
bcc6 5:dc4e6dbcb79b 36 BatteryService(BLE &_ble, uint8_t level = 100) :
bcc6 5:dc4e6dbcb79b 37 ble(_ble),
bcc6 5:dc4e6dbcb79b 38 batteryLevel(level),
bcc6 5:dc4e6dbcb79b 39 batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
bcc6 5:dc4e6dbcb79b 40
bcc6 5:dc4e6dbcb79b 41 GattCharacteristic *charTable[] = {&batteryLevelCharacteristic};
bcc6 5:dc4e6dbcb79b 42 GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
bcc6 5:dc4e6dbcb79b 43
bcc6 5:dc4e6dbcb79b 44 ble.addService(batteryService);
bcc6 5:dc4e6dbcb79b 45 }
bcc6 5:dc4e6dbcb79b 46
bcc6 5:dc4e6dbcb79b 47 /**
bcc6 5:dc4e6dbcb79b 48 * @brief Update the battery level with a new value. Valid values lie between 0 and 100,
bcc6 5:dc4e6dbcb79b 49 * anything outside this range will be ignored.
bcc6 5:dc4e6dbcb79b 50 *
bcc6 5:dc4e6dbcb79b 51 * @param newLevel
bcc6 5:dc4e6dbcb79b 52 * Update to battery level.
bcc6 5:dc4e6dbcb79b 53 */
bcc6 5:dc4e6dbcb79b 54 void updateBatteryLevel(uint8_t newLevel) {
bcc6 5:dc4e6dbcb79b 55 batteryLevel = newLevel;
bcc6 5:dc4e6dbcb79b 56 ble.gattServer().write(batteryLevelCharacteristic.getValueHandle(), &batteryLevel, 1);
bcc6 5:dc4e6dbcb79b 57 }
bcc6 5:dc4e6dbcb79b 58
bcc6 5:dc4e6dbcb79b 59 protected:
bcc6 5:dc4e6dbcb79b 60 /**
bcc6 5:dc4e6dbcb79b 61 * A reference to the underlying BLE instance that this object is attached to.
bcc6 5:dc4e6dbcb79b 62 * The services and characteristics will be registered in this BLE instance.
bcc6 5:dc4e6dbcb79b 63 */
bcc6 5:dc4e6dbcb79b 64 BLE &ble;
bcc6 5:dc4e6dbcb79b 65
bcc6 5:dc4e6dbcb79b 66 /**
bcc6 5:dc4e6dbcb79b 67 * The current battery level represented as an integer from 0% to 100%.
bcc6 5:dc4e6dbcb79b 68 */
bcc6 5:dc4e6dbcb79b 69 uint8_t batteryLevel;
bcc6 5:dc4e6dbcb79b 70 /**
bcc6 5:dc4e6dbcb79b 71 * A ReadOnlyGattCharacteristic that allows access to the peer device to the
bcc6 5:dc4e6dbcb79b 72 * batteryLevel value through BLE.
bcc6 5:dc4e6dbcb79b 73 */
bcc6 5:dc4e6dbcb79b 74 ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic;
bcc6 5:dc4e6dbcb79b 75 };
bcc6 5:dc4e6dbcb79b 76
bcc6 5:dc4e6dbcb79b 77 #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/