
A simple demo that directly uses BLE library to define service and characteristics
Dependencies: BLE_API mbed nRF51822
Fork of BLE_HeartRate by
HeartRateService.h@41:9cef0129da5f, 2014-09-02 (annotated)
- Committer:
- rgrover1
- Date:
- Tue Sep 02 16:17:18 2014 +0000
- Revision:
- 41:9cef0129da5f
- Parent:
- 39:6390604f904c
updated underlying libraries
Who changed what in which revision?
User | Revision | Line number | New 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 | 41:9cef0129da5f | 28 | enum { |
rgrover1 | 41:9cef0129da5f | 29 | LOCATION_OTHER = 0, |
rgrover1 | 41:9cef0129da5f | 30 | LOCATION_CHEST, |
rgrover1 | 41:9cef0129da5f | 31 | LOCATION_WRIST, |
rgrover1 | 41:9cef0129da5f | 32 | LOCATION_FINGER, |
rgrover1 | 41:9cef0129da5f | 33 | LOCATION_HAND, |
rgrover1 | 41:9cef0129da5f | 34 | LOCATION_EAR_LOBE, |
rgrover1 | 41:9cef0129da5f | 35 | LOCATION_FOOT, |
rgrover1 | 41:9cef0129da5f | 36 | }; |
rgrover1 | 39:6390604f904c | 37 | |
rgrover1 | 39:6390604f904c | 38 | public: |
rgrover1 | 39:6390604f904c | 39 | HeartRateService(BLEDevice &_ble, uint8_t _hrmCounter, uint8_t _location) : |
rgrover1 | 39:6390604f904c | 40 | ble(_ble), |
rgrover1 | 39:6390604f904c | 41 | bpm(_hrmCounter), |
rgrover1 | 39:6390604f904c | 42 | hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm.getPointer(), HeartRateValueBytes::SIZEOF_ARRAY, HeartRateValueBytes::SIZEOF_ARRAY, |
rgrover1 | 39:6390604f904c | 43 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
rgrover1 | 39:6390604f904c | 44 | hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, (uint8_t *)&_location, sizeof(_location), sizeof(_location), |
rgrover1 | 39:6390604f904c | 45 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ) { |
rgrover1 | 39:6390604f904c | 46 | |
rgrover1 | 39:6390604f904c | 47 | static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
rgrover1 | 39:6390604f904c | 48 | if (serviceAdded) { |
rgrover1 | 39:6390604f904c | 49 | return; |
rgrover1 | 39:6390604f904c | 50 | } |
rgrover1 | 39:6390604f904c | 51 | |
rgrover1 | 39:6390604f904c | 52 | GattCharacteristic *hrmChars[] = {&hrmRate, &hrmLocation, }; |
rgrover1 | 39:6390604f904c | 53 | GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *)); |
rgrover1 | 39:6390604f904c | 54 | |
rgrover1 | 39:6390604f904c | 55 | ble.addService(hrmService); |
rgrover1 | 39:6390604f904c | 56 | serviceAdded = true; |
rgrover1 | 39:6390604f904c | 57 | } |
rgrover1 | 39:6390604f904c | 58 | |
rgrover1 | 39:6390604f904c | 59 | void updateHeartRate(uint8_t hrmCounter) { |
rgrover1 | 39:6390604f904c | 60 | if (ble.getGapState().connected) { |
rgrover1 | 39:6390604f904c | 61 | bpm.updateHeartRate(hrmCounter); |
rgrover1 | 41:9cef0129da5f | 62 | ble.updateCharacteristicValue(hrmRate.getValueAttribute().getHandle(), bpm.getPointer(), HeartRateValueBytes::SIZEOF_ARRAY); |
rgrover1 | 39:6390604f904c | 63 | } |
rgrover1 | 39:6390604f904c | 64 | } |
rgrover1 | 39:6390604f904c | 65 | |
rgrover1 | 39:6390604f904c | 66 | private: |
rgrover1 | 39:6390604f904c | 67 | /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */ |
rgrover1 | 39:6390604f904c | 68 | struct HeartRateValueBytes { |
rgrover1 | 39:6390604f904c | 69 | static const unsigned SIZEOF_ARRAY = 2; |
rgrover1 | 39:6390604f904c | 70 | |
rgrover1 | 39:6390604f904c | 71 | HeartRateValueBytes(uint8_t hrmCounter) : beatsPerMinute() { |
rgrover1 | 39:6390604f904c | 72 | updateHeartRate(hrmCounter); |
rgrover1 | 39:6390604f904c | 73 | } |
rgrover1 | 39:6390604f904c | 74 | |
rgrover1 | 39:6390604f904c | 75 | void updateHeartRate(uint8_t hrmCounter) { |
rgrover1 | 39:6390604f904c | 76 | beatsPerMinute[1] = hrmCounter; |
rgrover1 | 39:6390604f904c | 77 | } |
rgrover1 | 39:6390604f904c | 78 | |
rgrover1 | 39:6390604f904c | 79 | uint8_t *getPointer(void) { |
rgrover1 | 39:6390604f904c | 80 | return beatsPerMinute; |
rgrover1 | 39:6390604f904c | 81 | } |
rgrover1 | 39:6390604f904c | 82 | |
rgrover1 | 39:6390604f904c | 83 | const uint8_t *getPointer(void) const { |
rgrover1 | 39:6390604f904c | 84 | return beatsPerMinute; |
rgrover1 | 39:6390604f904c | 85 | } |
rgrover1 | 39:6390604f904c | 86 | |
rgrover1 | 41:9cef0129da5f | 87 | private: |
rgrover1 | 39:6390604f904c | 88 | /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */ |
rgrover1 | 39:6390604f904c | 89 | /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ |
rgrover1 | 39:6390604f904c | 90 | uint8_t beatsPerMinute[SIZEOF_ARRAY]; |
rgrover1 | 39:6390604f904c | 91 | }; |
rgrover1 | 39:6390604f904c | 92 | |
rgrover1 | 39:6390604f904c | 93 | private: |
rgrover1 | 39:6390604f904c | 94 | BLEDevice &ble; |
rgrover1 | 39:6390604f904c | 95 | HeartRateValueBytes bpm; |
rgrover1 | 39:6390604f904c | 96 | GattCharacteristic hrmRate; |
rgrover1 | 39:6390604f904c | 97 | GattCharacteristic hrmLocation; |
rgrover1 | 39:6390604f904c | 98 | }; |
rgrover1 | 39:6390604f904c | 99 | |
rgrover1 | 39:6390604f904c | 100 | #endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/ |