ble nano hid over gatt
Dependencies: BLE_API mbed-dev nRF51822
BatteryService.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef __BLE_BATTERY_SERVICE_H__ 00018 #define __BLE_BATTERY_SERVICE_H__ 00019 00020 #include "ble/BLE.h" 00021 00022 static const UUID UUID_BATTERY_VOLTAGE = UUID("416B6DFF-D80C-477E-9841-30CB59C65C93"); 00023 00024 00025 /** 00026 * @class BatteryService 00027 * @brief BLE Battery Service. This service displays the battery level from 0% to 100%, represented as an 8bit number. 00028 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml 00029 * Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml 00030 */ 00031 class BatteryService { 00032 public: 00033 00034 /** 00035 * @param[in] _ble 00036 * BLE object for the underlying controller. 00037 * @param[in] level 00038 * 8bit batterly level. Usually used to represent percentage of batterly charge remaining. 00039 */ 00040 BatteryService (BLE &_ble, uint8_t level = 100, uint16_t voltage = 0) : 00041 ble(_ble), 00042 batteryLevel(level), 00043 batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00044 batteryVoltage(voltage), 00045 batteryVoltageCharacteristic(UUID_BATTERY_VOLTAGE, &batteryVoltage, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) 00046 { 00047 00048 // required for OS X bonding 00049 SecurityManager::SecurityMode_t securityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM; 00050 batteryLevelCharacteristic.requireSecurity(securityMode); 00051 00052 GattCharacteristic *charTable[] = {&batteryLevelCharacteristic, &batteryVoltageCharacteristic}; 00053 GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); 00054 00055 ble.addService(batteryService); 00056 } 00057 00058 /** 00059 * @brief Update the battery level with a new value. Valid values lie between 0 and 100, 00060 * anything outside this range will be ignored. 00061 * 00062 * @param newLevel 00063 * Update to battery level. 00064 */ 00065 void updateBatteryLevel(const uint8_t newLevel, const uint16_t newVoltage) { 00066 if (batteryLevel != newLevel) { 00067 batteryLevel = newLevel; 00068 ble.gattServer().write(batteryLevelCharacteristic.getValueHandle(), &batteryLevel, 1); 00069 } 00070 if (batteryVoltage != newVoltage) { 00071 batteryVoltage = newVoltage; 00072 ble.gattServer().write(batteryVoltageCharacteristic.getValueHandle(), reinterpret_cast<uint8_t*>(&batteryVoltage), 2); 00073 } 00074 } 00075 00076 protected: 00077 /** 00078 * A reference to the underlying BLE instance that this object is attached to. 00079 * The services and characteristics will be registered in this BLE instance. 00080 */ 00081 BLE &ble; 00082 00083 /** 00084 * The current battery level represented as an integer from 0% to 100%. 00085 */ 00086 uint8_t batteryLevel; 00087 /** 00088 * A ReadOnlyGattCharacteristic that allows access to the peer device to the 00089 * batteryLevel value through BLE. 00090 */ 00091 ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic; 00092 00093 uint16_t batteryVoltage; // voltage in mV 00094 ReadOnlyGattCharacteristic<uint16_t> batteryVoltageCharacteristic; 00095 }; 00096 00097 #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/
Generated on Tue Jul 12 2022 14:16:47 by 1.7.2