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
ble/services/BatteryService.h@934:5e3acddfcd82, 2015-11-26 (annotated)
- Committer:
- rgrover1
- Date:
- Thu Nov 26 12:52:06 2015 +0000
- Revision:
- 934:5e3acddfcd82
- Parent:
- 933:3ec277a0d780
- Child:
- 935:e9b595e6b0ed
Synchronized with git rev fea34e77
Author: Marcus Chang
Updated GapAdvertisementData.h to handle insertion and appending of data fields. Made appendField private.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 712:b04b5db36865 | 1 | /* mbed Microcontroller Library |
rgrover1 | 712:b04b5db36865 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 712:b04b5db36865 | 3 | * |
rgrover1 | 712:b04b5db36865 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 712:b04b5db36865 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 712:b04b5db36865 | 6 | * You may obtain a copy of the License at |
rgrover1 | 712:b04b5db36865 | 7 | * |
rgrover1 | 712:b04b5db36865 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 712:b04b5db36865 | 9 | * |
rgrover1 | 712:b04b5db36865 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 712:b04b5db36865 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 712:b04b5db36865 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 712:b04b5db36865 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 712:b04b5db36865 | 14 | * limitations under the License. |
rgrover1 | 712:b04b5db36865 | 15 | */ |
rgrover1 | 712:b04b5db36865 | 16 | |
rgrover1 | 712:b04b5db36865 | 17 | #ifndef __BLE_BATTERY_SERVICE_H__ |
rgrover1 | 712:b04b5db36865 | 18 | #define __BLE_BATTERY_SERVICE_H__ |
rgrover1 | 712:b04b5db36865 | 19 | |
rgrover1 | 712:b04b5db36865 | 20 | #include "ble/BLE.h" |
rgrover1 | 712:b04b5db36865 | 21 | |
rgrover1 | 712:b04b5db36865 | 22 | /** |
rgrover1 | 712:b04b5db36865 | 23 | * @class BatteryService |
rgrover1 | 934:5e3acddfcd82 | 24 | * @brief BLE Battery Service. This service displays the battery level from 0%->100% represented as a 8bit number.<br> |
rgrover1 | 934:5e3acddfcd82 | 25 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml <br> |
rgrover1 | 712:b04b5db36865 | 26 | * Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml |
rgrover1 | 712:b04b5db36865 | 27 | */ |
rgrover1 | 712:b04b5db36865 | 28 | class BatteryService { |
rgrover1 | 712:b04b5db36865 | 29 | public: |
rgrover1 | 712:b04b5db36865 | 30 | /** |
rgrover1 | 934:5e3acddfcd82 | 31 | * @param[ref] _ble |
rgrover1 | 934:5e3acddfcd82 | 32 | * BLE object for the underlying controller. |
rgrover1 | 934:5e3acddfcd82 | 33 | * @param[in] level |
rgrover1 | 934:5e3acddfcd82 | 34 | * 8bit batterly level. Usually used to represent percentage of batterly charge remaining. |
rgrover1 | 934:5e3acddfcd82 | 35 | */ |
rgrover1 | 712:b04b5db36865 | 36 | BatteryService(BLE &_ble, uint8_t level = 100) : |
rgrover1 | 712:b04b5db36865 | 37 | ble(_ble), |
rgrover1 | 712:b04b5db36865 | 38 | batteryLevel(level), |
rgrover1 | 712:b04b5db36865 | 39 | batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
rgrover1 | 712:b04b5db36865 | 40 | |
rgrover1 | 712:b04b5db36865 | 41 | GattCharacteristic *charTable[] = {&batteryLevelCharacteristic}; |
rgrover1 | 712:b04b5db36865 | 42 | GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
rgrover1 | 712:b04b5db36865 | 43 | |
rgrover1 | 712:b04b5db36865 | 44 | ble.addService(batteryService); |
rgrover1 | 712:b04b5db36865 | 45 | } |
rgrover1 | 712:b04b5db36865 | 46 | |
rgrover1 | 712:b04b5db36865 | 47 | /** |
rgrover1 | 934:5e3acddfcd82 | 48 | * @brief Update the battery level with a new value. Valid values range from |
rgrover1 | 934:5e3acddfcd82 | 49 | * 0..100. Anything outside this range will be ignored. |
rgrover1 | 712:b04b5db36865 | 50 | * |
rgrover1 | 712:b04b5db36865 | 51 | * @param newLevel |
rgrover1 | 934:5e3acddfcd82 | 52 | * update to battery level. |
rgrover1 | 712:b04b5db36865 | 53 | */ |
rgrover1 | 712:b04b5db36865 | 54 | void updateBatteryLevel(uint8_t newLevel) { |
rgrover1 | 712:b04b5db36865 | 55 | batteryLevel = newLevel; |
rgrover1 | 728:997ba5e7b3b6 | 56 | ble.gattServer().write(batteryLevelCharacteristic.getValueHandle(), &batteryLevel, 1); |
rgrover1 | 712:b04b5db36865 | 57 | } |
rgrover1 | 712:b04b5db36865 | 58 | |
rgrover1 | 712:b04b5db36865 | 59 | protected: |
rgrover1 | 712:b04b5db36865 | 60 | BLE &ble; |
rgrover1 | 712:b04b5db36865 | 61 | |
rgrover1 | 712:b04b5db36865 | 62 | uint8_t batteryLevel; |
rgrover1 | 712:b04b5db36865 | 63 | ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic; |
rgrover1 | 712:b04b5db36865 | 64 | }; |
rgrover1 | 712:b04b5db36865 | 65 | |
rgrover1 | 712:b04b5db36865 | 66 | #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/ |