ble nano hid over gatt

Dependencies:   BLE_API mbed-dev nRF51822

Revision:
58:64df960619ce
Parent:
54:899fc2b0a76b
--- a/BatteryService.h	Tue Aug 30 14:17:14 2016 +0000
+++ b/BatteryService.h	Tue Aug 30 14:44:24 2016 +0000
@@ -19,6 +19,9 @@
 
 #include "ble/BLE.h"
 
+static const UUID UUID_BATTERY_VOLTAGE = UUID("416B6DFF-D80C-477E-9841-30CB59C65C93");
+
+
 /**
 * @class BatteryService
 * @brief BLE Battery Service. This service displays the battery level from 0% to 100%, represented as an 8bit number.
@@ -27,22 +30,26 @@
 */
 class BatteryService {
 public:
+
 	/**
 	 * @param[in] _ble
 	 *               BLE object for the underlying controller.
 	 * @param[in] level
 	 *               8bit batterly level. Usually used to represent percentage of batterly charge remaining.
 	 */
-	BatteryService(BLE &_ble, uint8_t level = 100) :
+	BatteryService(BLE &_ble, uint8_t level = 100, uint16_t voltage = 0) :
 		ble(_ble),
 		batteryLevel(level),
-		batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
+		batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+		batteryVoltage(voltage),
+		batteryVoltageCharacteristic(UUID_BATTERY_VOLTAGE, &batteryVoltage, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
+		 {
 
 		// required for OS X bonding
 		SecurityManager::SecurityMode_t securityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM;
 		batteryLevelCharacteristic.requireSecurity(securityMode);
 
-		GattCharacteristic *charTable[] = {&batteryLevelCharacteristic};
+		GattCharacteristic *charTable[] = {&batteryLevelCharacteristic, &batteryVoltageCharacteristic};
 		GattService         batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
 
 		ble.addService(batteryService);
@@ -55,11 +62,15 @@
 	 * @param newLevel
 	 *              Update to battery level.
 	 */
-	void updateBatteryLevel(const uint8_t newLevel) {
+	void updateBatteryLevel(const uint8_t newLevel, const uint16_t newVoltage) {
 		if (batteryLevel != newLevel) {
 			batteryLevel = newLevel;
 			ble.gattServer().write(batteryLevelCharacteristic.getValueHandle(), &batteryLevel, 1);
 		}
+		if (batteryVoltage != newVoltage) {
+			batteryVoltage = newVoltage;
+			ble.gattServer().write(batteryVoltageCharacteristic.getValueHandle(), reinterpret_cast<uint8_t*>(&batteryVoltage), 2);
+		}
 	}
 
 protected:
@@ -78,6 +89,9 @@
 	 * batteryLevel value through BLE.
 	 */
 	ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic;
+	
+	uint16_t batteryVoltage; // voltage in mV
+	ReadOnlyGattCharacteristic<uint16_t> batteryVoltageCharacteristic;
 };
 
 #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/