High level Bluetooth Low Energy API and radio abstraction layer
Dependents: BLE_ANCS_SDAPI BLE_temperature BLE_HeartRate BLE_ANCS_SDAPI_IRC ... more
Overview
The BLE_API is a high level abstraction for using Bluetooth Low Energy on multiple platforms. For details and examples using the BLE_API please see the BLE_API Summary Page. Or click on the API Documentation tab above.
Supported Services
Supported services can be found in the BLE_API/services folder.
services/BatteryService.h@122:4b68a819ab4f, 2014-09-30 (annotated)
- Committer:
- Rohit Grover
- Date:
- Tue Sep 30 01:03:56 2014 +0100
- Revision:
- 122:4b68a819ab4f
- Parent:
- 118:620d28e7a1ba
- Child:
- 238:905d6fc17fda
DFUService object needs to remain alive after ble.init(); declaring as static.
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 | |
Rohit Grover |
118:620d28e7a1ba | 20 | #include "BLEDevice.h" |
Rohit Grover |
118:620d28e7a1ba | 21 | |
Rohit Grover |
118:620d28e7a1ba | 22 | /* Battery Service */ |
Rohit Grover |
118:620d28e7a1ba | 23 | /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml */ |
Rohit Grover |
118:620d28e7a1ba | 24 | /* Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml */ |
Rohit Grover |
118:620d28e7a1ba | 25 | class BatteryService { |
Rohit Grover |
118:620d28e7a1ba | 26 | public: |
Rohit Grover |
118:620d28e7a1ba | 27 | BatteryService(BLEDevice &_ble, uint8_t level = 100) : |
Rohit Grover |
118:620d28e7a1ba | 28 | ble(_ble), |
Rohit Grover |
118:620d28e7a1ba | 29 | batteryLevel(level), |
Rohit Grover |
118:620d28e7a1ba | 30 | batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, sizeof(batteryLevel), sizeof(batteryLevel), |
Rohit Grover |
118:620d28e7a1ba | 31 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
Rohit Grover |
118:620d28e7a1ba | 32 | |
Rohit Grover |
118:620d28e7a1ba | 33 | static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
Rohit Grover |
118:620d28e7a1ba | 34 | if (serviceAdded) { |
Rohit Grover |
118:620d28e7a1ba | 35 | return; |
Rohit Grover |
118:620d28e7a1ba | 36 | } |
Rohit Grover |
118:620d28e7a1ba | 37 | |
Rohit Grover |
118:620d28e7a1ba | 38 | GattCharacteristic *charTable[] = {&batteryLevelCharacteristic}; |
Rohit Grover |
118:620d28e7a1ba | 39 | GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
Rohit Grover |
118:620d28e7a1ba | 40 | |
Rohit Grover |
118:620d28e7a1ba | 41 | ble.addService(batteryService); |
Rohit Grover |
118:620d28e7a1ba | 42 | serviceAdded = true; |
Rohit Grover |
118:620d28e7a1ba | 43 | } |
Rohit Grover |
118:620d28e7a1ba | 44 | |
Rohit Grover |
118:620d28e7a1ba | 45 | /** |
Rohit Grover |
118:620d28e7a1ba | 46 | * Update the battery level with a new value. Valid values range from |
Rohit Grover |
118:620d28e7a1ba | 47 | * 0..100. Anything outside this range will be ignored. |
Rohit Grover |
118:620d28e7a1ba | 48 | * @param newLevel New level. |
Rohit Grover |
118:620d28e7a1ba | 49 | */ |
Rohit Grover |
118:620d28e7a1ba | 50 | void updateBatteryLevel(uint8_t newLevel) { |
Rohit Grover |
118:620d28e7a1ba | 51 | batteryLevel = newLevel; |
Rohit Grover |
118:620d28e7a1ba | 52 | ble.updateCharacteristicValue(batteryLevelCharacteristic.getValueAttribute().getHandle(), &batteryLevel, 1); |
Rohit Grover |
118:620d28e7a1ba | 53 | } |
Rohit Grover |
118:620d28e7a1ba | 54 | |
Rohit Grover |
118:620d28e7a1ba | 55 | private: |
Rohit Grover |
118:620d28e7a1ba | 56 | BLEDevice &ble; |
Rohit Grover |
118:620d28e7a1ba | 57 | uint8_t batteryLevel; |
Rohit Grover |
118:620d28e7a1ba | 58 | GattCharacteristic batteryLevelCharacteristic; |
Rohit Grover |
118:620d28e7a1ba | 59 | }; |
Rohit Grover |
118:620d28e7a1ba | 60 | |
Rohit Grover |
118:620d28e7a1ba | 61 | #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/ |