
A simple demo that directly uses BLE library to define service and characteristics
Dependencies: BLE_API mbed nRF51822
Fork of BLE_HeartRate by
Revision 39:6390604f904c, committed 2014-08-22
- Comitter:
- rgrover1
- Date:
- Fri Aug 22 14:55:52 2014 +0000
- Parent:
- 38:b2efa4f73d3a
- Child:
- 40:e73130c6f2bb
- Commit message:
- Extract HeartRateService as a template to simplify the main application.
Changed in this revision
--- a/BLE_API.lib Fri Jul 25 17:23:34 2014 +0100 +++ b/BLE_API.lib Fri Aug 22 14:55:52 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#8559a2da6f41 +http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#189ff241dae1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HeartRateService.h Fri Aug 22 14:55:52 2014 +0000 @@ -0,0 +1,97 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __BLE_HEART_RATE_SERVICE_H__ +#define __BLE_HEART_RATE_SERVICE_H__ + +#include "BLEDevice.h" + +/* Heart Rate Service */ +/* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */ +/* HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ +/* Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */ +class HeartRateService { +public: + static const unsigned BLE_HRS_BODY_SENSOR_LOCATION_OTHER = 0; + static const unsigned BLE_HRS_BODY_SENSOR_LOCATION_CHEST = 1; + static const unsigned BLE_HRS_BODY_SENSOR_LOCATION_WRIST = 2; + static const unsigned BLE_HRS_BODY_SENSOR_LOCATION_FINGER = 3; + static const unsigned BLE_HRS_BODY_SENSOR_LOCATION_HAND = 4; + static const unsigned BLE_HRS_BODY_SENSOR_LOCATION_EAR_LOBE = 5; + static const unsigned BLE_HRS_BODY_SENSOR_LOCATION_FOOT = 6; + +public: + HeartRateService(BLEDevice &_ble, uint8_t _hrmCounter, uint8_t _location) : + ble(_ble), + bpm(_hrmCounter), + hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm.getPointer(), HeartRateValueBytes::SIZEOF_ARRAY, HeartRateValueBytes::SIZEOF_ARRAY, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), + hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, (uint8_t *)&_location, sizeof(_location), sizeof(_location), + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ) { + + static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ + if (serviceAdded) { + return; + } + + GattCharacteristic *hrmChars[] = {&hrmRate, &hrmLocation, }; + GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *)); + + ble.addService(hrmService); + serviceAdded = true; + } + + void updateHeartRate(uint8_t hrmCounter) { + if (ble.getGapState().connected) { + bpm.updateHeartRate(hrmCounter); + ble.updateCharacteristicValue(hrmRate.getHandle(), bpm.getPointer(), HeartRateValueBytes::SIZEOF_ARRAY); + } + } + +private: + /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */ + struct HeartRateValueBytes { + static const unsigned SIZEOF_ARRAY = 2; + + HeartRateValueBytes(uint8_t hrmCounter) : beatsPerMinute() { + updateHeartRate(hrmCounter); + } + + void updateHeartRate(uint8_t hrmCounter) { + beatsPerMinute[1] = hrmCounter; + } + + uint8_t *getPointer(void) { + return beatsPerMinute; + } + + const uint8_t *getPointer(void) const { + return beatsPerMinute; + } + + /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */ + /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ + uint8_t beatsPerMinute[SIZEOF_ARRAY]; + }; + +private: + BLEDevice &ble; + HeartRateValueBytes bpm; + GattCharacteristic hrmRate; + GattCharacteristic hrmLocation; +}; + +#endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/
--- a/main.cpp Fri Jul 25 17:23:34 2014 +0100 +++ b/main.cpp Fri Aug 22 14:55:52 2014 +0000 @@ -16,7 +16,7 @@ #include "mbed.h" #include "BLEDevice.h" -#include "ble_hrs.h" +#include "HeartRateService.h" BLEDevice ble; DigitalOut led1(LED1); @@ -31,28 +31,9 @@ #define DEBUG(...) /* nothing */ #endif /* #if NEED_CONSOLE_OUTPUT */ -const static char DEVICE_NAME[] = "Nordic_HRM"; - -/* Heart Rate Service */ -/* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */ -/* HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ -/* Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */ -static uint8_t hrmCounter = 100; -static uint8_t bpm[2] = {0x00, hrmCounter}; -GattCharacteristic hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm, sizeof(bpm), sizeof(bpm), - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); -static uint8_t location = BLE_HRS_BODY_SENSOR_LOCATION_FINGER; -GattCharacteristic hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, - (uint8_t *)&location, sizeof(location), sizeof(location), - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); -GattCharacteristic *hrmChars[] = {&hrmRate, &hrmLocation, }; -GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *)); - -static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE}; - -static volatile bool triggerSensorPolling = false; /* set to high periodically to indicate to the main thread that - * polling is necessary. */ -static Gap::ConnectionParams_t connectionParams; +const static char DEVICE_NAME[] = "Nordic_HRM"; +static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE}; +static volatile bool triggerSensorPolling = false; void disconnectionCallback(Gap::Handle_t handle) { @@ -61,26 +42,13 @@ ble.startAdvertising(); } -void onConnectionCallback(Gap::Handle_t handle) -{ - DEBUG("connected. Got handle %u\r\n", handle); - - connectionParams.slaveLatency = 1; - if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) { - DEBUG("failed to update connection paramter\r\n"); - } -} - -/** - * Triggered periodically by the 'ticker' interrupt. - */ void periodicCallback(void) { led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ - triggerSensorPolling = true; /* Note that the periodicCallback() executes in - * interrupt context, so it is safer to do - * heavy-weight sensor polling from the main - * thread.*/ + + /* Note that the periodicCallback() executes in interrupt context, so it is safer to do + * heavy-weight sensor polling from the main thread. */ + triggerSensorPolling = true; } int main(void) @@ -92,9 +60,6 @@ DEBUG("Initialising the nRF51822\n\r"); ble.init(); ble.onDisconnection(disconnectionCallback); - ble.onConnection(onConnectionCallback); - - ble.getPreferredConnectionParams(&connectionParams); /* setup advertising */ ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); @@ -102,10 +67,11 @@ ble.accumulateAdvertisingPayload(GapAdvertisingData::HEART_RATE_SENSOR_HEART_RATE_BELT); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ + ble.setAdvertisingInterval(1600); /* 100ms; in multiples of 0.625ms. */ ble.startAdvertising(); - ble.addService(hrmService); + uint8_t hrmCounter = 100 ; + HeartRateService hrService(ble, hrmCounter, HeartRateService::BLE_HRS_BODY_SENSOR_LOCATION_FINGER); while (true) { if (triggerSensorPolling) { @@ -118,12 +84,7 @@ hrmCounter = 100; } - if (ble.getGapState().connected) { - /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */ - /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ - bpm[1] = hrmCounter; - ble.updateCharacteristicValue(hrmRate.getHandle(), bpm, sizeof(bpm)); - } + hrService.updateHeartRate(hrmCounter); } else { ble.waitForEvent(); }
--- a/mbed.bld Fri Jul 25 17:23:34 2014 +0100 +++ b/mbed.bld Fri Aug 22 14:55:52 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/6213f644d804 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/9327015d4013 \ No newline at end of file