ble nano hid over gatt

Dependencies:   BLE_API mbed-dev nRF51822

Committer:
cho45
Date:
Tue Aug 30 14:44:24 2016 +0000
Revision:
58:64df960619ce
Parent:
54:899fc2b0a76b
???????????????mV???????????????

Who changed what in which revision?

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