A simple demo that directly uses BLE library to define service and characteristics

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_HeartRate by Bluetooth Low Energy

Committer:
rgrover1
Date:
Fri Aug 22 14:55:52 2014 +0000
Revision:
39:6390604f904c
Child:
41:9cef0129da5f
Extract HeartRateService as a template to simplify the main application.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 39:6390604f904c 1 /* mbed Microcontroller Library
rgrover1 39:6390604f904c 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 39:6390604f904c 3 *
rgrover1 39:6390604f904c 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 39:6390604f904c 5 * you may not use this file except in compliance with the License.
rgrover1 39:6390604f904c 6 * You may obtain a copy of the License at
rgrover1 39:6390604f904c 7 *
rgrover1 39:6390604f904c 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 39:6390604f904c 9 *
rgrover1 39:6390604f904c 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 39:6390604f904c 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 39:6390604f904c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 39:6390604f904c 13 * See the License for the specific language governing permissions and
rgrover1 39:6390604f904c 14 * limitations under the License.
rgrover1 39:6390604f904c 15 */
rgrover1 39:6390604f904c 16
rgrover1 39:6390604f904c 17 #ifndef __BLE_HEART_RATE_SERVICE_H__
rgrover1 39:6390604f904c 18 #define __BLE_HEART_RATE_SERVICE_H__
rgrover1 39:6390604f904c 19
rgrover1 39:6390604f904c 20 #include "BLEDevice.h"
rgrover1 39:6390604f904c 21
rgrover1 39:6390604f904c 22 /* Heart Rate Service */
rgrover1 39:6390604f904c 23 /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */
rgrover1 39:6390604f904c 24 /* HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
rgrover1 39:6390604f904c 25 /* Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */
rgrover1 39:6390604f904c 26 class HeartRateService {
rgrover1 39:6390604f904c 27 public:
rgrover1 39:6390604f904c 28 static const unsigned BLE_HRS_BODY_SENSOR_LOCATION_OTHER = 0;
rgrover1 39:6390604f904c 29 static const unsigned BLE_HRS_BODY_SENSOR_LOCATION_CHEST = 1;
rgrover1 39:6390604f904c 30 static const unsigned BLE_HRS_BODY_SENSOR_LOCATION_WRIST = 2;
rgrover1 39:6390604f904c 31 static const unsigned BLE_HRS_BODY_SENSOR_LOCATION_FINGER = 3;
rgrover1 39:6390604f904c 32 static const unsigned BLE_HRS_BODY_SENSOR_LOCATION_HAND = 4;
rgrover1 39:6390604f904c 33 static const unsigned BLE_HRS_BODY_SENSOR_LOCATION_EAR_LOBE = 5;
rgrover1 39:6390604f904c 34 static const unsigned BLE_HRS_BODY_SENSOR_LOCATION_FOOT = 6;
rgrover1 39:6390604f904c 35
rgrover1 39:6390604f904c 36 public:
rgrover1 39:6390604f904c 37 HeartRateService(BLEDevice &_ble, uint8_t _hrmCounter, uint8_t _location) :
rgrover1 39:6390604f904c 38 ble(_ble),
rgrover1 39:6390604f904c 39 bpm(_hrmCounter),
rgrover1 39:6390604f904c 40 hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm.getPointer(), HeartRateValueBytes::SIZEOF_ARRAY, HeartRateValueBytes::SIZEOF_ARRAY,
rgrover1 39:6390604f904c 41 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
rgrover1 39:6390604f904c 42 hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, (uint8_t *)&_location, sizeof(_location), sizeof(_location),
rgrover1 39:6390604f904c 43 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ) {
rgrover1 39:6390604f904c 44
rgrover1 39:6390604f904c 45 static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
rgrover1 39:6390604f904c 46 if (serviceAdded) {
rgrover1 39:6390604f904c 47 return;
rgrover1 39:6390604f904c 48 }
rgrover1 39:6390604f904c 49
rgrover1 39:6390604f904c 50 GattCharacteristic *hrmChars[] = {&hrmRate, &hrmLocation, };
rgrover1 39:6390604f904c 51 GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *));
rgrover1 39:6390604f904c 52
rgrover1 39:6390604f904c 53 ble.addService(hrmService);
rgrover1 39:6390604f904c 54 serviceAdded = true;
rgrover1 39:6390604f904c 55 }
rgrover1 39:6390604f904c 56
rgrover1 39:6390604f904c 57 void updateHeartRate(uint8_t hrmCounter) {
rgrover1 39:6390604f904c 58 if (ble.getGapState().connected) {
rgrover1 39:6390604f904c 59 bpm.updateHeartRate(hrmCounter);
rgrover1 39:6390604f904c 60 ble.updateCharacteristicValue(hrmRate.getHandle(), bpm.getPointer(), HeartRateValueBytes::SIZEOF_ARRAY);
rgrover1 39:6390604f904c 61 }
rgrover1 39:6390604f904c 62 }
rgrover1 39:6390604f904c 63
rgrover1 39:6390604f904c 64 private:
rgrover1 39:6390604f904c 65 /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */
rgrover1 39:6390604f904c 66 struct HeartRateValueBytes {
rgrover1 39:6390604f904c 67 static const unsigned SIZEOF_ARRAY = 2;
rgrover1 39:6390604f904c 68
rgrover1 39:6390604f904c 69 HeartRateValueBytes(uint8_t hrmCounter) : beatsPerMinute() {
rgrover1 39:6390604f904c 70 updateHeartRate(hrmCounter);
rgrover1 39:6390604f904c 71 }
rgrover1 39:6390604f904c 72
rgrover1 39:6390604f904c 73 void updateHeartRate(uint8_t hrmCounter) {
rgrover1 39:6390604f904c 74 beatsPerMinute[1] = hrmCounter;
rgrover1 39:6390604f904c 75 }
rgrover1 39:6390604f904c 76
rgrover1 39:6390604f904c 77 uint8_t *getPointer(void) {
rgrover1 39:6390604f904c 78 return beatsPerMinute;
rgrover1 39:6390604f904c 79 }
rgrover1 39:6390604f904c 80
rgrover1 39:6390604f904c 81 const uint8_t *getPointer(void) const {
rgrover1 39:6390604f904c 82 return beatsPerMinute;
rgrover1 39:6390604f904c 83 }
rgrover1 39:6390604f904c 84
rgrover1 39:6390604f904c 85 /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */
rgrover1 39:6390604f904c 86 /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
rgrover1 39:6390604f904c 87 uint8_t beatsPerMinute[SIZEOF_ARRAY];
rgrover1 39:6390604f904c 88 };
rgrover1 39:6390604f904c 89
rgrover1 39:6390604f904c 90 private:
rgrover1 39:6390604f904c 91 BLEDevice &ble;
rgrover1 39:6390604f904c 92 HeartRateValueBytes bpm;
rgrover1 39:6390604f904c 93 GattCharacteristic hrmRate;
rgrover1 39:6390604f904c 94 GattCharacteristic hrmLocation;
rgrover1 39:6390604f904c 95 };
rgrover1 39:6390604f904c 96
rgrover1 39:6390604f904c 97 #endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/