Bill Schilit / BLE_API

Fork of BLE_API by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BatteryService.h Source File

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, sizeof(batteryLevel), sizeof(batteryLevel),
00040                                    GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
00041 
00042         static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
00043         if (serviceAdded) {
00044             return;
00045         }
00046 
00047         GattCharacteristic *charTable[] = {&batteryLevelCharacteristic};
00048         GattService         batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00049 
00050         ble.addService(batteryService);
00051         serviceAdded = true;
00052     }
00053 
00054     /**
00055      * @brief Update the battery level with a new value. Valid values range from
00056      * 0..100. Anything outside this range will be ignored.
00057      *
00058      * @param newLevel
00059      *              update to battery level.
00060      */
00061     void updateBatteryLevel(uint8_t newLevel) {
00062         batteryLevel = newLevel;
00063         ble.updateCharacteristicValue (batteryLevelCharacteristic.getValueAttribute().getHandle(), &batteryLevel, 1);
00064     }
00065 
00066 private:
00067     BLEDevice          &ble;
00068     uint8_t             batteryLevel;
00069     GattCharacteristic  batteryLevelCharacteristic;
00070 };
00071 
00072 #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/