High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
services/BatteryService.h@715:6d415ac147aa, 2015-07-02 (annotated)
- Committer:
- rgrover1
- Date:
- Thu Jul 02 09:06:11 2015 +0100
- Revision:
- 715:6d415ac147aa
- Parent:
- 714:a6130aaa0fd9
Synchronized with git rev 69726547
Author: Rohit Grover
Release 0.3.9
=============
A minor patch to fix a build error introduced by the previous
release. This has to do with certain declarations being made members
of class UUID.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 714:a6130aaa0fd9 | 1 | /* mbed Microcontroller Library |
rgrover1 | 714:a6130aaa0fd9 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 714:a6130aaa0fd9 | 3 | * |
rgrover1 | 714:a6130aaa0fd9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 714:a6130aaa0fd9 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 714:a6130aaa0fd9 | 6 | * You may obtain a copy of the License at |
rgrover1 | 714:a6130aaa0fd9 | 7 | * |
rgrover1 | 714:a6130aaa0fd9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 714:a6130aaa0fd9 | 9 | * |
rgrover1 | 714:a6130aaa0fd9 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 714:a6130aaa0fd9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 714:a6130aaa0fd9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 714:a6130aaa0fd9 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 714:a6130aaa0fd9 | 14 | * limitations under the License. |
rgrover1 | 714:a6130aaa0fd9 | 15 | */ |
rgrover1 | 714:a6130aaa0fd9 | 16 | |
rgrover1 | 714:a6130aaa0fd9 | 17 | #ifndef __BLE_BATTERY_SERVICE_H__ |
rgrover1 | 714:a6130aaa0fd9 | 18 | #define __BLE_BATTERY_SERVICE_H__ |
rgrover1 | 714:a6130aaa0fd9 | 19 | |
rgrover1 | 714:a6130aaa0fd9 | 20 | #include "BLEDevice.h" |
rgrover1 | 714:a6130aaa0fd9 | 21 | |
rgrover1 | 714:a6130aaa0fd9 | 22 | /** |
rgrover1 | 714:a6130aaa0fd9 | 23 | * @class BatteryService |
rgrover1 | 714:a6130aaa0fd9 | 24 | * @brief BLE Battery Service. This service displays the battery level from 0%->100% represented as a 8bit number.<br> |
rgrover1 | 714:a6130aaa0fd9 | 25 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml <br> |
rgrover1 | 714:a6130aaa0fd9 | 26 | * Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml |
rgrover1 | 714:a6130aaa0fd9 | 27 | */ |
rgrover1 | 714:a6130aaa0fd9 | 28 | class BatteryService { |
rgrover1 | 714:a6130aaa0fd9 | 29 | public: |
rgrover1 | 714:a6130aaa0fd9 | 30 | /** |
rgrover1 | 714:a6130aaa0fd9 | 31 | * @param[ref] _ble |
rgrover1 | 714:a6130aaa0fd9 | 32 | * BLEDevice object for the underlying controller. |
rgrover1 | 714:a6130aaa0fd9 | 33 | * @param[in] level |
rgrover1 | 714:a6130aaa0fd9 | 34 | * 8bit batterly level. Usually used to represent percentage of batterly charge remaining. |
rgrover1 | 714:a6130aaa0fd9 | 35 | */ |
rgrover1 | 714:a6130aaa0fd9 | 36 | BatteryService(BLEDevice &_ble, uint8_t level = 100) : |
rgrover1 | 714:a6130aaa0fd9 | 37 | ble(_ble), |
rgrover1 | 714:a6130aaa0fd9 | 38 | batteryLevel(level), |
rgrover1 | 714:a6130aaa0fd9 | 39 | batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
rgrover1 | 714:a6130aaa0fd9 | 40 | |
rgrover1 | 714:a6130aaa0fd9 | 41 | static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
rgrover1 | 714:a6130aaa0fd9 | 42 | if (serviceAdded) { |
rgrover1 | 714:a6130aaa0fd9 | 43 | return; |
rgrover1 | 714:a6130aaa0fd9 | 44 | } |
rgrover1 | 714:a6130aaa0fd9 | 45 | |
rgrover1 | 714:a6130aaa0fd9 | 46 | GattCharacteristic *charTable[] = {&batteryLevelCharacteristic}; |
rgrover1 | 714:a6130aaa0fd9 | 47 | GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
rgrover1 | 714:a6130aaa0fd9 | 48 | |
rgrover1 | 714:a6130aaa0fd9 | 49 | ble.addService(batteryService); |
rgrover1 | 714:a6130aaa0fd9 | 50 | serviceAdded = true; |
rgrover1 | 714:a6130aaa0fd9 | 51 | } |
rgrover1 | 714:a6130aaa0fd9 | 52 | |
rgrover1 | 714:a6130aaa0fd9 | 53 | /** |
rgrover1 | 714:a6130aaa0fd9 | 54 | * @brief Update the battery level with a new value. Valid values range from |
rgrover1 | 714:a6130aaa0fd9 | 55 | * 0..100. Anything outside this range will be ignored. |
rgrover1 | 714:a6130aaa0fd9 | 56 | * |
rgrover1 | 714:a6130aaa0fd9 | 57 | * @param newLevel |
rgrover1 | 714:a6130aaa0fd9 | 58 | * update to battery level. |
rgrover1 | 714:a6130aaa0fd9 | 59 | */ |
rgrover1 | 714:a6130aaa0fd9 | 60 | void updateBatteryLevel(uint8_t newLevel) { |
rgrover1 | 714:a6130aaa0fd9 | 61 | batteryLevel = newLevel; |
rgrover1 | 714:a6130aaa0fd9 | 62 | ble.updateCharacteristicValue(batteryLevelCharacteristic.getValueAttribute().getHandle(), &batteryLevel, 1); |
rgrover1 | 714:a6130aaa0fd9 | 63 | } |
rgrover1 | 714:a6130aaa0fd9 | 64 | |
rgrover1 | 714:a6130aaa0fd9 | 65 | private: |
rgrover1 | 714:a6130aaa0fd9 | 66 | BLEDevice &ble; |
rgrover1 | 714:a6130aaa0fd9 | 67 | |
rgrover1 | 714:a6130aaa0fd9 | 68 | uint8_t batteryLevel; |
rgrover1 | 714:a6130aaa0fd9 | 69 | ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic; |
rgrover1 | 714:a6130aaa0fd9 | 70 | }; |
rgrover1 | 714:a6130aaa0fd9 | 71 | |
rgrover1 | 714:a6130aaa0fd9 | 72 | #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/ |