This is a demonstration of how to create a GATT service and characteristic.

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_EvothingsExample_GATT by Austin Blackstone

Intro

This code demonstrates how to use the BLE_API to create a GATT service and characteristic to toggle a LED on / off.

Overview

This code is a demonstration of how to create a custom service (UUID=0xA0000) with two characteristics, a read only characteristic (UUID=0xA001) and a write characteristic (UUID=0xA002). What is written to the write characteristic will be copied across to the read characteristic and broadcast out. If a single byte is written it will be used to toggle the on board LED, if more than 1 byte is written the data will be written out to the terminal. The default max size is 10bytes.

Video

This is a video of how to use this example. Please note that in this video Android is used to read / write the characteristics. This could just as easily been done with the Evothings App listed below or an equivalent iOS app. Any app that can read/write characteristics could have been used.

Details

characteristic and service UUID's are initialized here

UUID's

uint16_t customServiceUUID  = 0xA000;
uint16_t readCharUUID       = 0xA001;
uint16_t writeCharUUID      = 0xA002;


We set up the list of available UUID's that will be advertised as part of the GAP advertising packet here. (it is good form to make this match the services you are actually advertising, so in this case we should set it to be 0xA000, but we are choosing to set it to 0xFFFF so it is easier to distinguish on the scanning app)

Set_ServiceID's

static const uint16_t uuid16_list[]        = {0xFFFF};
...
ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet


Next we set up the characteristics and then put them together into a service. Notice that each characteristic has a maximum of 10Bytes of Value space, this can be expanded up to 512B.

Setup_Service_&_Characteristics

// Set Up custom Characteristics
static uint8_t readValue[10] = {0};
ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue);
 
static uint8_t writeValue[10] = {0};
WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
 
// Set up custom service
GattCharacteristic *characteristics[] = {&readChar, &writeChar};
GattService        customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));


Next we setup the writeCharCallback() function that will be called when a BLE onDataWritten event is triggered (when someone writes to a characteristic on the device). This function will check to see what characteristic is being written to and then handle it appropriately. In this case there is only one writable characteristic so its redundant, but good form for more complex service/characteristic combinations.

On_Characteristic_written_to_callback

void writeCharCallback(const GattCharacteristicWriteCBParams *params)
{
    // check to see what characteristic was written, by handle
    if(params->charHandle == writeChar.getValueHandle()) {
        // toggle LED if only 1 byte is written
        if(params->len == 1) {
            led = params->data[0];
            ...}
        // print the data if more than 1 byte is written
        else {
            printf("\n\r Data received: length = %d, data = 0x",params->len); 
            ...}
        // update the readChar with the value of writeChar
        ble.updateCharacteristicValue(readChar.getValueHandle(),params->data,params->len);
    }
}
...
// register callback function
ble.onDataWritten(writeCharCallback);
...


The last thing to do is register the service with the BLE object so it is available.

Add_Service_to_BLE_Object

...
    // add our custom service
    ble.addService(customService);
...

Viewing Data

You can use either the LightBlue app on iOS or the nRF Master Control Panel application on Android to view the advertising data. Alternatively you can use a custom GATT Evothings App to view the data.

Evothings?

Evothings is a rapid prototyping environment that uses cordova to enable you to rapidly develop smartphone applications in Javascript. Please download the Evothings workbench to your computer and the Evothings client to your smartphone. The Evothings workbench will come with a program called "mbed Evothings GATT", this Evothings Smartphone program is meant to be used with the embedded mbed code. For instructions on how to use evothings please see the reference section below.

Reference

Committer:
mbedAustin
Date:
Sat Feb 14 06:49:01 2015 +0000
Revision:
1:94152e7d8b5c
added a little more skeleton to it, still needs to be fully flushed out, tossed the HeartRate service code into the custom service.h file, added link to Grove Colour Sensor example for inspiration for later

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 1:94152e7d8b5c 1 /* mbed Microcontroller Library
mbedAustin 1:94152e7d8b5c 2 * Copyright (c) 2006-2013 ARM Limited
mbedAustin 1:94152e7d8b5c 3 *
mbedAustin 1:94152e7d8b5c 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbedAustin 1:94152e7d8b5c 5 * you may not use this file except in compliance with the License.
mbedAustin 1:94152e7d8b5c 6 * You may obtain a copy of the License at
mbedAustin 1:94152e7d8b5c 7 *
mbedAustin 1:94152e7d8b5c 8 * http://www.apache.org/licenses/LICENSE-2.0
mbedAustin 1:94152e7d8b5c 9 *
mbedAustin 1:94152e7d8b5c 10 * Unless required by applicable law or agreed to in writing, software
mbedAustin 1:94152e7d8b5c 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbedAustin 1:94152e7d8b5c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbedAustin 1:94152e7d8b5c 13 * See the License for the specific language governing permissions and
mbedAustin 1:94152e7d8b5c 14 * limitations under the License.
mbedAustin 1:94152e7d8b5c 15 */
mbedAustin 1:94152e7d8b5c 16
mbedAustin 1:94152e7d8b5c 17 #ifndef __BLE_HEART_RATE_SERVICE_H__
mbedAustin 1:94152e7d8b5c 18 #define __BLE_HEART_RATE_SERVICE_H__
mbedAustin 1:94152e7d8b5c 19
mbedAustin 1:94152e7d8b5c 20 #include "BLEDevice.h"
mbedAustin 1:94152e7d8b5c 21
mbedAustin 1:94152e7d8b5c 22 /**
mbedAustin 1:94152e7d8b5c 23 * @class HeartRateService
mbedAustin 1:94152e7d8b5c 24 * @brief BLE Service for HeartRate. This BLE Service contains the location of the sensor, the heartrate in beats per minute. <br>
mbedAustin 1:94152e7d8b5c 25 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml <br>
mbedAustin 1:94152e7d8b5c 26 * HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml <br>
mbedAustin 1:94152e7d8b5c 27 * Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml
mbedAustin 1:94152e7d8b5c 28 */
mbedAustin 1:94152e7d8b5c 29 class HeartRateService {
mbedAustin 1:94152e7d8b5c 30 public:
mbedAustin 1:94152e7d8b5c 31 /**
mbedAustin 1:94152e7d8b5c 32 * @enum SensorLocation
mbedAustin 1:94152e7d8b5c 33 * @brief Location of HeartRate sensor on body.
mbedAustin 1:94152e7d8b5c 34 */
mbedAustin 1:94152e7d8b5c 35 enum {
mbedAustin 1:94152e7d8b5c 36 LOCATION_OTHER = 0, /*!< Other Location */
mbedAustin 1:94152e7d8b5c 37 LOCATION_CHEST, /*!< Chest */
mbedAustin 1:94152e7d8b5c 38 LOCATION_WRIST, /*!< Wrist */
mbedAustin 1:94152e7d8b5c 39 LOCATION_FINGER, /*!< Finger */
mbedAustin 1:94152e7d8b5c 40 LOCATION_HAND, /*!< Hand */
mbedAustin 1:94152e7d8b5c 41 LOCATION_EAR_LOBE, /*!< Earlobe */
mbedAustin 1:94152e7d8b5c 42 LOCATION_FOOT, /*!< Foot */
mbedAustin 1:94152e7d8b5c 43 };
mbedAustin 1:94152e7d8b5c 44
mbedAustin 1:94152e7d8b5c 45 public:
mbedAustin 1:94152e7d8b5c 46 /**
mbedAustin 1:94152e7d8b5c 47 * @brief Constructor with 8bit HRM Counter value.
mbedAustin 1:94152e7d8b5c 48 *
mbedAustin 1:94152e7d8b5c 49 * @param[ref] _ble
mbedAustin 1:94152e7d8b5c 50 * Reference to the underlying BLEDevice.
mbedAustin 1:94152e7d8b5c 51 * @param[in] hrmCounter (8-bit)
mbedAustin 1:94152e7d8b5c 52 * initial value for the hrm counter.
mbedAustin 1:94152e7d8b5c 53 * @param[in] location
mbedAustin 1:94152e7d8b5c 54 * Sensor's location.
mbedAustin 1:94152e7d8b5c 55 */
mbedAustin 1:94152e7d8b5c 56 HeartRateService(BLEDevice &_ble, uint8_t hrmCounter, uint8_t location) :
mbedAustin 1:94152e7d8b5c 57 ble(_ble),
mbedAustin 1:94152e7d8b5c 58 valueBytes(hrmCounter),
mbedAustin 1:94152e7d8b5c 59 hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(),
mbedAustin 1:94152e7d8b5c 60 valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
mbedAustin 1:94152e7d8b5c 61 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
mbedAustin 1:94152e7d8b5c 62 hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location),
mbedAustin 1:94152e7d8b5c 63 controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) {
mbedAustin 1:94152e7d8b5c 64 setupService();
mbedAustin 1:94152e7d8b5c 65 }
mbedAustin 1:94152e7d8b5c 66
mbedAustin 1:94152e7d8b5c 67 /**
mbedAustin 1:94152e7d8b5c 68 * @brief Constructor with a 16-bit HRM Counter value.
mbedAustin 1:94152e7d8b5c 69 *
mbedAustin 1:94152e7d8b5c 70 * @param[in] _ble
mbedAustin 1:94152e7d8b5c 71 * Reference to the underlying BLEDevice.
mbedAustin 1:94152e7d8b5c 72 * @param[in] hrmCounter (8-bit)
mbedAustin 1:94152e7d8b5c 73 * initial value for the hrm counter.
mbedAustin 1:94152e7d8b5c 74 * @param[in] location
mbedAustin 1:94152e7d8b5c 75 * Sensor's location.
mbedAustin 1:94152e7d8b5c 76 */
mbedAustin 1:94152e7d8b5c 77 HeartRateService(BLEDevice &_ble, uint16_t hrmCounter, uint8_t location) :
mbedAustin 1:94152e7d8b5c 78 ble(_ble),
mbedAustin 1:94152e7d8b5c 79 valueBytes(hrmCounter),
mbedAustin 1:94152e7d8b5c 80 hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(),
mbedAustin 1:94152e7d8b5c 81 valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
mbedAustin 1:94152e7d8b5c 82 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
mbedAustin 1:94152e7d8b5c 83 hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location),
mbedAustin 1:94152e7d8b5c 84 controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) {
mbedAustin 1:94152e7d8b5c 85 setupService();
mbedAustin 1:94152e7d8b5c 86 }
mbedAustin 1:94152e7d8b5c 87
mbedAustin 1:94152e7d8b5c 88 /**
mbedAustin 1:94152e7d8b5c 89 * @brief Set a new 8-bit value for heart rate.
mbedAustin 1:94152e7d8b5c 90 *
mbedAustin 1:94152e7d8b5c 91 * @param[in] hrmCounter
mbedAustin 1:94152e7d8b5c 92 * HeartRate in bpm.
mbedAustin 1:94152e7d8b5c 93 */
mbedAustin 1:94152e7d8b5c 94 void updateHeartRate(uint8_t hrmCounter) {
mbedAustin 1:94152e7d8b5c 95 valueBytes.updateHeartRate(hrmCounter);
mbedAustin 1:94152e7d8b5c 96 ble.updateCharacteristicValue(hrmRate.getValueAttribute().getHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
mbedAustin 1:94152e7d8b5c 97 }
mbedAustin 1:94152e7d8b5c 98
mbedAustin 1:94152e7d8b5c 99 /**
mbedAustin 1:94152e7d8b5c 100 * Set a new 16-bit value for heart rate.
mbedAustin 1:94152e7d8b5c 101 *
mbedAustin 1:94152e7d8b5c 102 * @param[in] hrmCounter
mbedAustin 1:94152e7d8b5c 103 * HeartRate in bpm.
mbedAustin 1:94152e7d8b5c 104 */
mbedAustin 1:94152e7d8b5c 105 void updateHeartRate(uint16_t hrmCounter) {
mbedAustin 1:94152e7d8b5c 106 valueBytes.updateHeartRate(hrmCounter);
mbedAustin 1:94152e7d8b5c 107 ble.updateCharacteristicValue(hrmRate.getValueAttribute().getHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes());
mbedAustin 1:94152e7d8b5c 108 }
mbedAustin 1:94152e7d8b5c 109
mbedAustin 1:94152e7d8b5c 110 /**
mbedAustin 1:94152e7d8b5c 111 * This callback allows the HeartRateService to receive updates to the
mbedAustin 1:94152e7d8b5c 112 * controlPoint Characteristic.
mbedAustin 1:94152e7d8b5c 113 *
mbedAustin 1:94152e7d8b5c 114 * @param[in] params
mbedAustin 1:94152e7d8b5c 115 * Information about the characterisitc being updated.
mbedAustin 1:94152e7d8b5c 116 */
mbedAustin 1:94152e7d8b5c 117 virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) {
mbedAustin 1:94152e7d8b5c 118 if (params->charHandle == controlPoint.getValueAttribute().getHandle()) {
mbedAustin 1:94152e7d8b5c 119 /* Do something here if the new value is 1; else you can override this method by
mbedAustin 1:94152e7d8b5c 120 * extending this class.
mbedAustin 1:94152e7d8b5c 121 * @NOTE: if you are extending this class, be sure to also call
mbedAustin 1:94152e7d8b5c 122 * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in
mbedAustin 1:94152e7d8b5c 123 * your constructor.
mbedAustin 1:94152e7d8b5c 124 */
mbedAustin 1:94152e7d8b5c 125 }
mbedAustin 1:94152e7d8b5c 126 }
mbedAustin 1:94152e7d8b5c 127
mbedAustin 1:94152e7d8b5c 128 private:
mbedAustin 1:94152e7d8b5c 129 void setupService(void) {
mbedAustin 1:94152e7d8b5c 130 static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
mbedAustin 1:94152e7d8b5c 131 if (serviceAdded) {
mbedAustin 1:94152e7d8b5c 132 return;
mbedAustin 1:94152e7d8b5c 133 }
mbedAustin 1:94152e7d8b5c 134
mbedAustin 1:94152e7d8b5c 135 GattCharacteristic *charTable[] = {&hrmRate, &hrmLocation, &controlPoint};
mbedAustin 1:94152e7d8b5c 136 GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
mbedAustin 1:94152e7d8b5c 137
mbedAustin 1:94152e7d8b5c 138 ble.addService(hrmService);
mbedAustin 1:94152e7d8b5c 139 serviceAdded = true;
mbedAustin 1:94152e7d8b5c 140
mbedAustin 1:94152e7d8b5c 141 ble.onDataWritten(this, &HeartRateService::onDataWritten);
mbedAustin 1:94152e7d8b5c 142 }
mbedAustin 1:94152e7d8b5c 143
mbedAustin 1:94152e7d8b5c 144 private:
mbedAustin 1:94152e7d8b5c 145 /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */
mbedAustin 1:94152e7d8b5c 146 struct HeartRateValueBytes {
mbedAustin 1:94152e7d8b5c 147 static const unsigned MAX_VALUE_BYTES = 3; /* FLAGS + up to two bytes for heart-rate */
mbedAustin 1:94152e7d8b5c 148 static const unsigned FLAGS_BYTE_INDEX = 0;
mbedAustin 1:94152e7d8b5c 149
mbedAustin 1:94152e7d8b5c 150 static const unsigned VALUE_FORMAT_BITNUM = 0;
mbedAustin 1:94152e7d8b5c 151 static const uint8_t VALUE_FORMAT_FLAG = (1 << VALUE_FORMAT_BITNUM);
mbedAustin 1:94152e7d8b5c 152
mbedAustin 1:94152e7d8b5c 153 HeartRateValueBytes(uint8_t hrmCounter) : valueBytes() {
mbedAustin 1:94152e7d8b5c 154 updateHeartRate(hrmCounter);
mbedAustin 1:94152e7d8b5c 155 }
mbedAustin 1:94152e7d8b5c 156
mbedAustin 1:94152e7d8b5c 157 HeartRateValueBytes(uint16_t hrmCounter) : valueBytes() {
mbedAustin 1:94152e7d8b5c 158 updateHeartRate(hrmCounter);
mbedAustin 1:94152e7d8b5c 159 }
mbedAustin 1:94152e7d8b5c 160
mbedAustin 1:94152e7d8b5c 161 void updateHeartRate(uint8_t hrmCounter) {
mbedAustin 1:94152e7d8b5c 162 valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG;
mbedAustin 1:94152e7d8b5c 163 valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter;
mbedAustin 1:94152e7d8b5c 164 }
mbedAustin 1:94152e7d8b5c 165
mbedAustin 1:94152e7d8b5c 166 void updateHeartRate(uint16_t hrmCounter) {
mbedAustin 1:94152e7d8b5c 167 valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG;
mbedAustin 1:94152e7d8b5c 168 valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF);
mbedAustin 1:94152e7d8b5c 169 valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8);
mbedAustin 1:94152e7d8b5c 170 }
mbedAustin 1:94152e7d8b5c 171
mbedAustin 1:94152e7d8b5c 172 uint8_t *getPointer(void) {
mbedAustin 1:94152e7d8b5c 173 return valueBytes;
mbedAustin 1:94152e7d8b5c 174 }
mbedAustin 1:94152e7d8b5c 175
mbedAustin 1:94152e7d8b5c 176 const uint8_t *getPointer(void) const {
mbedAustin 1:94152e7d8b5c 177 return valueBytes;
mbedAustin 1:94152e7d8b5c 178 }
mbedAustin 1:94152e7d8b5c 179
mbedAustin 1:94152e7d8b5c 180 unsigned getNumValueBytes(void) const {
mbedAustin 1:94152e7d8b5c 181 return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint16_t) : sizeof(uint8_t));
mbedAustin 1:94152e7d8b5c 182 }
mbedAustin 1:94152e7d8b5c 183
mbedAustin 1:94152e7d8b5c 184 private:
mbedAustin 1:94152e7d8b5c 185 /* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */
mbedAustin 1:94152e7d8b5c 186 /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
mbedAustin 1:94152e7d8b5c 187 uint8_t valueBytes[MAX_VALUE_BYTES];
mbedAustin 1:94152e7d8b5c 188 };
mbedAustin 1:94152e7d8b5c 189
mbedAustin 1:94152e7d8b5c 190 private:
mbedAustin 1:94152e7d8b5c 191 BLEDevice &ble;
mbedAustin 1:94152e7d8b5c 192
mbedAustin 1:94152e7d8b5c 193 HeartRateValueBytes valueBytes;
mbedAustin 1:94152e7d8b5c 194 uint8_t controlPointValue;
mbedAustin 1:94152e7d8b5c 195
mbedAustin 1:94152e7d8b5c 196 GattCharacteristic hrmRate;
mbedAustin 1:94152e7d8b5c 197 ReadOnlyGattCharacteristic<uint8_t> hrmLocation;
mbedAustin 1:94152e7d8b5c 198 WriteOnlyGattCharacteristic<uint8_t> controlPoint;
mbedAustin 1:94152e7d8b5c 199 };
mbedAustin 1:94152e7d8b5c 200
mbedAustin 1:94152e7d8b5c 201 #endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/
mbedAustin 1:94152e7d8b5c 202