Holla back
Fork of BLE_API by
Embed:
(wiki syntax)
Show/hide line numbers
BatteryService.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef __BLE_BATTERY_SERVICE_H__ 00018 #define __BLE_BATTERY_SERVICE_H__ 00019 00020 #include "BLEDevice.h" 00021 00022 /* Battery Service */ 00023 /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml */ 00024 /* Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml */ 00025 class BatteryService { 00026 public: 00027 BatteryService(BLEDevice &_ble, uint8_t level = 100) : 00028 ble(_ble), 00029 batteryLevel(level), 00030 batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, sizeof(batteryLevel), sizeof(batteryLevel), 00031 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { 00032 00033 static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ 00034 if (serviceAdded) { 00035 return; 00036 } 00037 00038 GattCharacteristic *charTable[] = {&batteryLevelCharacteristic}; 00039 GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); 00040 00041 ble.addService(batteryService); 00042 serviceAdded = true; 00043 } 00044 00045 /** 00046 * Update the battery level with a new value. Valid values range from 00047 * 0..100. Anything outside this range will be ignored. 00048 * @param newLevel New level. 00049 */ 00050 void updateBatteryLevel(uint8_t newLevel) { 00051 batteryLevel = newLevel; 00052 ble.updateCharacteristicValue(batteryLevelCharacteristic.getValueAttribute().getHandle(), &batteryLevel, 1); 00053 } 00054 00055 private: 00056 BLEDevice &ble; 00057 uint8_t batteryLevel; 00058 GattCharacteristic batteryLevelCharacteristic; 00059 }; 00060 00061 #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/
Generated on Tue Jul 12 2022 14:23:50 by 1.7.2