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
services/BatteryService.h@710:b2e1a2660ec2, 2015-06-19 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Jun 19 15:53:28 2015 +0100
- Revision:
- 710:b2e1a2660ec2
- Parent:
- 671:33ec93d25695
Synchronized with git rev 7e8977d8
Author: Rohit Grover
Release 0.3.8
=============
This is a minor set of enhancements before we yotta-ize BLE_API.
Enhancements
~~~~~~~~~~~~
* Minor rework for class UUID; added a default and copy constructor; and a != operator.
* Added copy constructor and accessors for GapAdvertisingParams.
* GapScanningParams:: remove unnecessary checks for SCAN_TIMEOUT_MAX.
* Add a comment header block to explain why BLEDevice::init() may not be safe
to call from global static context.
* Introduce GattAttribute::INVALID_HANDLE.
* Replace some deprecated uses of Gap::address_t with Gap::Address_t.
Bugfixes
~~~~~~~~
* None.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rohit Grover |
118:620d28e7a1ba | 1 | /* mbed Microcontroller Library |
Rohit Grover |
118:620d28e7a1ba | 2 | * Copyright (c) 2006-2013 ARM Limited |
Rohit Grover |
118:620d28e7a1ba | 3 | * |
Rohit Grover |
118:620d28e7a1ba | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Rohit Grover |
118:620d28e7a1ba | 5 | * you may not use this file except in compliance with the License. |
Rohit Grover |
118:620d28e7a1ba | 6 | * You may obtain a copy of the License at |
Rohit Grover |
118:620d28e7a1ba | 7 | * |
Rohit Grover |
118:620d28e7a1ba | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Rohit Grover |
118:620d28e7a1ba | 9 | * |
Rohit Grover |
118:620d28e7a1ba | 10 | * Unless required by applicable law or agreed to in writing, software |
Rohit Grover |
118:620d28e7a1ba | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Rohit Grover |
118:620d28e7a1ba | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Rohit Grover |
118:620d28e7a1ba | 13 | * See the License for the specific language governing permissions and |
Rohit Grover |
118:620d28e7a1ba | 14 | * limitations under the License. |
Rohit Grover |
118:620d28e7a1ba | 15 | */ |
Rohit Grover |
118:620d28e7a1ba | 16 | |
Rohit Grover |
118:620d28e7a1ba | 17 | #ifndef __BLE_BATTERY_SERVICE_H__ |
Rohit Grover |
118:620d28e7a1ba | 18 | #define __BLE_BATTERY_SERVICE_H__ |
Rohit Grover |
118:620d28e7a1ba | 19 | |
rgrover1 | 710:b2e1a2660ec2 | 20 | #include "BLEDevice.h" |
Rohit Grover |
118:620d28e7a1ba | 21 | |
rgrover1 | 242:0e9201b67e2f | 22 | /** |
mbedAustin | 238:905d6fc17fda | 23 | * @class BatteryService |
rgrover1 | 242:0e9201b67e2f | 24 | * @brief BLE Battery Service. This service displays the battery level from 0%->100% represented as a 8bit number.<br> |
mbedAustin | 238:905d6fc17fda | 25 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml <br> |
rgrover1 | 242:0e9201b67e2f | 26 | * Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml |
mbedAustin | 238:905d6fc17fda | 27 | */ |
Rohit Grover |
118:620d28e7a1ba | 28 | class BatteryService { |
Rohit Grover |
118:620d28e7a1ba | 29 | public: |
mbedAustin | 238:905d6fc17fda | 30 | /** |
mbedAustin | 238:905d6fc17fda | 31 | * @param[ref] _ble |
rgrover1 | 710:b2e1a2660ec2 | 32 | * BLEDevice object for the underlying controller. |
mbedAustin | 238:905d6fc17fda | 33 | * @param[in] level |
rgrover1 | 242:0e9201b67e2f | 34 | * 8bit batterly level. Usually used to represent percentage of batterly charge remaining. |
mbedAustin | 238:905d6fc17fda | 35 | */ |
rgrover1 | 710:b2e1a2660ec2 | 36 | BatteryService(BLEDevice &_ble, uint8_t level = 100) : |
Rohit Grover |
118:620d28e7a1ba | 37 | ble(_ble), |
Rohit Grover |
118:620d28e7a1ba | 38 | batteryLevel(level), |
rgrover1 | 277:1407d2f1ce3c | 39 | batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
Rohit Grover |
118:620d28e7a1ba | 40 | |
rgrover1 | 710:b2e1a2660ec2 | 41 | static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
rgrover1 | 710:b2e1a2660ec2 | 42 | if (serviceAdded) { |
rgrover1 | 710:b2e1a2660ec2 | 43 | return; |
rgrover1 | 710:b2e1a2660ec2 | 44 | } |
rgrover1 | 710:b2e1a2660ec2 | 45 | |
Rohit Grover |
118:620d28e7a1ba | 46 | GattCharacteristic *charTable[] = {&batteryLevelCharacteristic}; |
Rohit Grover |
118:620d28e7a1ba | 47 | GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
Rohit Grover |
118:620d28e7a1ba | 48 | |
Rohit Grover |
118:620d28e7a1ba | 49 | ble.addService(batteryService); |
rgrover1 | 710:b2e1a2660ec2 | 50 | serviceAdded = true; |
Rohit Grover |
118:620d28e7a1ba | 51 | } |
Rohit Grover |
118:620d28e7a1ba | 52 | |
Rohit Grover |
118:620d28e7a1ba | 53 | /** |
rgrover1 | 242:0e9201b67e2f | 54 | * @brief Update the battery level with a new value. Valid values range from |
Rohit Grover |
118:620d28e7a1ba | 55 | * 0..100. Anything outside this range will be ignored. |
rgrover1 | 242:0e9201b67e2f | 56 | * |
rgrover1 | 242:0e9201b67e2f | 57 | * @param newLevel |
mbedAustin | 238:905d6fc17fda | 58 | * update to battery level. |
Rohit Grover |
118:620d28e7a1ba | 59 | */ |
Rohit Grover |
118:620d28e7a1ba | 60 | void updateBatteryLevel(uint8_t newLevel) { |
Rohit Grover |
118:620d28e7a1ba | 61 | batteryLevel = newLevel; |
Rohit Grover |
118:620d28e7a1ba | 62 | ble.updateCharacteristicValue(batteryLevelCharacteristic.getValueAttribute().getHandle(), &batteryLevel, 1); |
Rohit Grover |
118:620d28e7a1ba | 63 | } |
Rohit Grover |
118:620d28e7a1ba | 64 | |
rgrover1 | 710:b2e1a2660ec2 | 65 | private: |
rgrover1 | 710:b2e1a2660ec2 | 66 | BLEDevice &ble; |
rgrover1 | 277:1407d2f1ce3c | 67 | |
rgrover1 | 277:1407d2f1ce3c | 68 | uint8_t batteryLevel; |
rgrover1 | 277:1407d2f1ce3c | 69 | ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic; |
Rohit Grover |
118:620d28e7a1ba | 70 | }; |
Rohit Grover |
118:620d28e7a1ba | 71 | |
rgrover1 | 242:0e9201b67e2f | 72 | #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/ |