ble nano hid over gatt
Dependencies: BLE_API mbed-dev nRF51822
BatteryService.h@48:d6938de02f62, 2016-08-29 (annotated)
- Committer:
- cho45
- Date:
- Mon Aug 29 14:09:09 2016 +0000
- Revision:
- 48:d6938de02f62
- Parent:
- 44:916b70fd1c40
- Child:
- 54:899fc2b0a76b
const
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cho45 | 0:be89b5fdea09 | 1 | /* mbed Microcontroller Library |
cho45 | 0:be89b5fdea09 | 2 | * Copyright (c) 2006-2013 ARM Limited |
cho45 | 0:be89b5fdea09 | 3 | * |
cho45 | 0:be89b5fdea09 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
cho45 | 0:be89b5fdea09 | 5 | * you may not use this file except in compliance with the License. |
cho45 | 0:be89b5fdea09 | 6 | * You may obtain a copy of the License at |
cho45 | 0:be89b5fdea09 | 7 | * |
cho45 | 0:be89b5fdea09 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
cho45 | 0:be89b5fdea09 | 9 | * |
cho45 | 0:be89b5fdea09 | 10 | * Unless required by applicable law or agreed to in writing, software |
cho45 | 0:be89b5fdea09 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
cho45 | 0:be89b5fdea09 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
cho45 | 0:be89b5fdea09 | 13 | * See the License for the specific language governing permissions and |
cho45 | 0:be89b5fdea09 | 14 | * limitations under the License. |
cho45 | 0:be89b5fdea09 | 15 | */ |
cho45 | 0:be89b5fdea09 | 16 | |
cho45 | 0:be89b5fdea09 | 17 | #ifndef __BLE_BATTERY_SERVICE_H__ |
cho45 | 0:be89b5fdea09 | 18 | #define __BLE_BATTERY_SERVICE_H__ |
cho45 | 0:be89b5fdea09 | 19 | |
cho45 | 0:be89b5fdea09 | 20 | #include "ble/BLE.h" |
cho45 | 0:be89b5fdea09 | 21 | |
cho45 | 0:be89b5fdea09 | 22 | /** |
cho45 | 0:be89b5fdea09 | 23 | * @class BatteryService |
cho45 | 0:be89b5fdea09 | 24 | * @brief BLE Battery Service. This service displays the battery level from 0% to 100%, represented as an 8bit number. |
cho45 | 0:be89b5fdea09 | 25 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml |
cho45 | 0:be89b5fdea09 | 26 | * Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml |
cho45 | 0:be89b5fdea09 | 27 | */ |
cho45 | 0:be89b5fdea09 | 28 | class BatteryService { |
cho45 | 0:be89b5fdea09 | 29 | public: |
cho45 |
44:916b70fd1c40 | 30 | /** |
cho45 |
44:916b70fd1c40 | 31 | * @param[in] _ble |
cho45 |
44:916b70fd1c40 | 32 | * BLE object for the underlying controller. |
cho45 |
44:916b70fd1c40 | 33 | * @param[in] level |
cho45 |
44:916b70fd1c40 | 34 | * 8bit batterly level. Usually used to represent percentage of batterly charge remaining. |
cho45 |
44:916b70fd1c40 | 35 | */ |
cho45 |
44:916b70fd1c40 | 36 | BatteryService(BLE &_ble, uint8_t level = 100) : |
cho45 |
44:916b70fd1c40 | 37 | ble(_ble), |
cho45 |
44:916b70fd1c40 | 38 | batteryLevel(level), |
cho45 |
44:916b70fd1c40 | 39 | batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
cho45 |
44:916b70fd1c40 | 40 | |
cho45 |
44:916b70fd1c40 | 41 | // required for OS X bonding |
cho45 |
44:916b70fd1c40 | 42 | SecurityManager::SecurityMode_t securityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM; |
cho45 |
44:916b70fd1c40 | 43 | batteryLevelCharacteristic.requireSecurity(securityMode); |
cho45 | 0:be89b5fdea09 | 44 | |
cho45 |
44:916b70fd1c40 | 45 | GattCharacteristic *charTable[] = {&batteryLevelCharacteristic}; |
cho45 |
44:916b70fd1c40 | 46 | GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
cho45 | 0:be89b5fdea09 | 47 | |
cho45 |
44:916b70fd1c40 | 48 | ble.addService(batteryService); |
cho45 |
44:916b70fd1c40 | 49 | } |
cho45 | 0:be89b5fdea09 | 50 | |
cho45 |
44:916b70fd1c40 | 51 | /** |
cho45 |
44:916b70fd1c40 | 52 | * @brief Update the battery level with a new value. Valid values lie between 0 and 100, |
cho45 |
44:916b70fd1c40 | 53 | * anything outside this range will be ignored. |
cho45 |
44:916b70fd1c40 | 54 | * |
cho45 |
44:916b70fd1c40 | 55 | * @param newLevel |
cho45 |
44:916b70fd1c40 | 56 | * Update to battery level. |
cho45 |
44:916b70fd1c40 | 57 | */ |
cho45 | 48:d6938de02f62 | 58 | void updateBatteryLevel(const uint8_t newLevel) { |
cho45 |
44:916b70fd1c40 | 59 | batteryLevel = newLevel; |
cho45 |
44:916b70fd1c40 | 60 | ble.gattServer().write(batteryLevelCharacteristic.getValueHandle(), &batteryLevel, 1); |
cho45 |
44:916b70fd1c40 | 61 | } |
cho45 | 0:be89b5fdea09 | 62 | |
cho45 | 0:be89b5fdea09 | 63 | protected: |
cho45 |
44:916b70fd1c40 | 64 | /** |
cho45 |
44:916b70fd1c40 | 65 | * A reference to the underlying BLE instance that this object is attached to. |
cho45 |
44:916b70fd1c40 | 66 | * The services and characteristics will be registered in this BLE instance. |
cho45 |
44:916b70fd1c40 | 67 | */ |
cho45 |
44:916b70fd1c40 | 68 | BLE &ble; |
cho45 | 0:be89b5fdea09 | 69 | |
cho45 |
44:916b70fd1c40 | 70 | /** |
cho45 |
44:916b70fd1c40 | 71 | * The current battery level represented as an integer from 0% to 100%. |
cho45 |
44:916b70fd1c40 | 72 | */ |
cho45 |
44:916b70fd1c40 | 73 | uint8_t batteryLevel; |
cho45 |
44:916b70fd1c40 | 74 | /** |
cho45 |
44:916b70fd1c40 | 75 | * A ReadOnlyGattCharacteristic that allows access to the peer device to the |
cho45 |
44:916b70fd1c40 | 76 | * batteryLevel value through BLE. |
cho45 |
44:916b70fd1c40 | 77 | */ |
cho45 |
44:916b70fd1c40 | 78 | ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic; |
cho45 | 0:be89b5fdea09 | 79 | }; |
cho45 | 0:be89b5fdea09 | 80 | |
cho45 |
44:916b70fd1c40 | 81 | #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/ |