Mistake on this page?
Report an issue in GitHub or email us
EnvironmentalService.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__
18 #define __BLE_ENVIRONMENTAL_SERVICE_H__
19 
20 #include "ble/BLE.h"
21 
22 #if BLE_FEATURE_GATT_SERVER
23 
24 /**
25 * @class EnvironmentalService
26 * @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement.
27 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml
28 * Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml
29 * Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml
30 * Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml
31 */
33 public:
34  typedef int16_t TemperatureType_t;
35  typedef uint16_t HumidityType_t;
36  typedef uint32_t PressureType_t;
37 
38  /**
39  * @brief EnvironmentalService constructor.
40  * @param _ble Reference to BLE device.
41  */
43  ble(_ble),
44  temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, &temperature),
45  humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, &humidity),
46  pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR, &pressure)
47  {
48  static bool serviceAdded = false; /* We should only ever need to add the information service once. */
49  if (serviceAdded) {
50  return;
51  }
52 
53  GattCharacteristic *charTable[] = { &humidityCharacteristic,
54  &pressureCharacteristic,
55  &temperatureCharacteristic };
56 
57  GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
58 
59  ble.gattServer().addService(environmentalService);
60  serviceAdded = true;
61  }
62 
63  /**
64  * @brief Update humidity characteristic.
65  * @param newHumidityVal New humidity measurement.
66  */
67  void updateHumidity(HumidityType_t newHumidityVal)
68  {
69  humidity = (HumidityType_t) (newHumidityVal * 100);
70  ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t));
71  }
72 
73  /**
74  * @brief Update pressure characteristic.
75  * @param newPressureVal New pressure measurement.
76  */
77  void updatePressure(PressureType_t newPressureVal)
78  {
79  pressure = (PressureType_t) (newPressureVal * 10);
80  ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t));
81  }
82 
83  /**
84  * @brief Update temperature characteristic.
85  * @param newTemperatureVal New temperature measurement.
86  */
87  void updateTemperature(float newTemperatureVal)
88  {
89  temperature = (TemperatureType_t) (newTemperatureVal * 100);
90  ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, sizeof(TemperatureType_t));
91  }
92 
93 private:
94  BLE& ble;
95 
96  TemperatureType_t temperature;
97  HumidityType_t humidity;
98  PressureType_t pressure;
99 
100  ReadOnlyGattCharacteristic<TemperatureType_t> temperatureCharacteristic;
101  ReadOnlyGattCharacteristic<HumidityType_t> humidityCharacteristic;
102  ReadOnlyGattCharacteristic<PressureType_t> pressureCharacteristic;
103 };
104 
105 #endif // BLE_FEATURE_GATT_SERVER
106 
107 #endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/
void updateHumidity(HumidityType_t newHumidityVal)
Update humidity characteristic.
Abstract away BLE-capable radio transceivers or SOCs.
Definition: BLE.h:139
BLE Environmental Service.
void updatePressure(PressureType_t newPressureVal)
Update pressure characteristic.
EnvironmentalService(BLE &_ble)
EnvironmentalService constructor.
Representation of a GattServer characteristic.
GattAttribute::Handle_t getValueHandle(void) const
Get the characteristic&#39;s value attribute handle in the ATT table.
UUID of the environmental service.
Definition: GattService.h:74
Representation of a GattServer service.
Definition: GattService.h:38
void updateTemperature(float newTemperatureVal)
Update temperature characteristic.
Entry namespace for all BLE API definitions.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.