Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BLE_API by
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 "BLEDevice.h" 00021 00022 /** 00023 * @class BatteryService 00024 * @brief BLE Battery Service. This service displays the battery level from 0%->100% represented as a 8bit number.<br> 00025 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml <br> 00026 * Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml 00027 */ 00028 class BatteryService { 00029 public: 00030 /** 00031 * @param[ref] _ble 00032 * BLEDevice object for the underlying controller. 00033 * @param[in] level 00034 * 8bit batterly level. Usually used to represent percentage of batterly charge remaining. 00035 */ 00036 BatteryService (BLEDevice &_ble, uint8_t level = 100) : 00037 ble(_ble), 00038 batteryLevel(level), 00039 batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { 00040 00041 static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ 00042 if (serviceAdded) { 00043 return; 00044 } 00045 00046 GattCharacteristic *charTable[] = {&batteryLevelCharacteristic}; 00047 GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); 00048 00049 ble.addService(batteryService); 00050 serviceAdded = true; 00051 } 00052 00053 /** 00054 * @brief Update the battery level with a new value. Valid values range from 00055 * 0..100. Anything outside this range will be ignored. 00056 * 00057 * @param newLevel 00058 * update to battery level. 00059 */ 00060 void updateBatteryLevel(uint8_t newLevel) { 00061 batteryLevel = newLevel; 00062 ble.updateCharacteristicValue (batteryLevelCharacteristic.getValueAttribute().getHandle(), &batteryLevel, 1); 00063 } 00064 00065 private: 00066 BLEDevice &ble; 00067 00068 uint8_t batteryLevel; 00069 ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic; 00070 }; 00071 00072 #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/
Generated on Tue Jul 12 2022 18:47:13 by
