An example of creating and updating a simple GATT Service using the BLE_API

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

This example creates and updates a standard Battery Level service, and a single GATT characteristic that contains the battery level.

Committer:
Rohit Grover
Date:
Thu Jun 12 14:16:55 2014 +0100
Revision:
5:77ad8d8dc9c5
Parent:
4:5b64235d1b85
Child:
8:45beed07b093
removing un-necessary white space

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rohit Grover 4:5b64235d1b85 1 /* mbed Microcontroller Library
Rohit Grover 4:5b64235d1b85 2 * Copyright (c) 2006-2013 ARM Limited
Rohit Grover 4:5b64235d1b85 3 *
Rohit Grover 4:5b64235d1b85 4 * Licensed under the Apache License, Version 2.0 (the "License");
Rohit Grover 4:5b64235d1b85 5 * you may not use this file except in compliance with the License.
Rohit Grover 4:5b64235d1b85 6 * You may obtain a copy of the License at
Rohit Grover 4:5b64235d1b85 7 *
Rohit Grover 4:5b64235d1b85 8 * http://www.apache.org/licenses/LICENSE-2.0
Rohit Grover 4:5b64235d1b85 9 *
Rohit Grover 4:5b64235d1b85 10 * Unless required by applicable law or agreed to in writing, software
Rohit Grover 4:5b64235d1b85 11 * distributed under the License is distributed on an "AS IS" BASIS,
Rohit Grover 4:5b64235d1b85 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Rohit Grover 4:5b64235d1b85 13 * See the License for the specific language governing permissions and
Rohit Grover 4:5b64235d1b85 14 * limitations under the License.
Rohit Grover 4:5b64235d1b85 15 */
Rohit Grover 4:5b64235d1b85 16
Rohit Grover 4:5b64235d1b85 17 #include "mbed.h"
Rohit Grover 4:5b64235d1b85 18 #include "BLEDevice.h"
Rohit Grover 4:5b64235d1b85 19
Rohit Grover 4:5b64235d1b85 20 BLEDevice ble;
Rohit Grover 4:5b64235d1b85 21
Rohit Grover 4:5b64235d1b85 22 DigitalOut led1(LED1);
Rohit Grover 4:5b64235d1b85 23
Rohit Grover 4:5b64235d1b85 24 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
Rohit Grover 4:5b64235d1b85 25 * it will have an impact on code-size and power
Rohit Grover 4:5b64235d1b85 26 * consumption. */
Rohit Grover 4:5b64235d1b85 27
Rohit Grover 4:5b64235d1b85 28 #if NEED_CONSOLE_OUTPUT
Rohit Grover 4:5b64235d1b85 29 Serial pc(USBTX, USBRX);
Rohit Grover 4:5b64235d1b85 30 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
Rohit Grover 4:5b64235d1b85 31 #else
Rohit Grover 4:5b64235d1b85 32 #define DEBUG(...) /* nothing */
Rohit Grover 4:5b64235d1b85 33 #endif /* #if NEED_CONSOLE_OUTPUT */
Rohit Grover 4:5b64235d1b85 34
Rohit Grover 4:5b64235d1b85 35 /* Battery Level Service */
Rohit Grover 4:5b64235d1b85 36 uint8_t batt = 72; /* Battery level */
Rohit Grover 4:5b64235d1b85 37 GattCharacteristic battLevel (GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batt, sizeof(batt), sizeof(batt),
Rohit Grover 4:5b64235d1b85 38 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
Rohit Grover 5:77ad8d8dc9c5 39 GattCharacteristic *battChars[] = {&battLevel};
Rohit Grover 4:5b64235d1b85 40 GattService battService (GattService::UUID_BATTERY_SERVICE, battChars, sizeof(battChars) / sizeof(GattCharacteristic *));
Rohit Grover 4:5b64235d1b85 41
Rohit Grover 4:5b64235d1b85 42
Rohit Grover 4:5b64235d1b85 43 void disconnectionCallback(void)
Rohit Grover 4:5b64235d1b85 44 {
Rohit Grover 4:5b64235d1b85 45 DEBUG("Disconnected!\n\r");
Rohit Grover 4:5b64235d1b85 46 DEBUG("Restarting the advertising process\n\r");
Rohit Grover 4:5b64235d1b85 47 ble.startAdvertising();
Rohit Grover 4:5b64235d1b85 48 }
Rohit Grover 4:5b64235d1b85 49
Rohit Grover 4:5b64235d1b85 50 /**
Rohit Grover 4:5b64235d1b85 51 * Runs once a second in interrupt context triggered by the 'ticker'; updates
Rohit Grover 4:5b64235d1b85 52 * battery level if there is a connection.
Rohit Grover 4:5b64235d1b85 53 */
Rohit Grover 4:5b64235d1b85 54 void periodicCallback(void)
Rohit Grover 4:5b64235d1b85 55 {
Rohit Grover 4:5b64235d1b85 56 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
Rohit Grover 4:5b64235d1b85 57
Rohit Grover 4:5b64235d1b85 58 if (ble.getGapState().connected) {
Rohit Grover 4:5b64235d1b85 59 /* Update battery level */
Rohit Grover 4:5b64235d1b85 60 batt++;
Rohit Grover 4:5b64235d1b85 61 if (batt > 100) {
Rohit Grover 4:5b64235d1b85 62 batt = 72;
Rohit Grover 4:5b64235d1b85 63 }
Rohit Grover 4:5b64235d1b85 64 ble.updateCharacteristicValue(battLevel.getHandle(), (uint8_t *)&batt, sizeof(batt));
Rohit Grover 4:5b64235d1b85 65 }
Rohit Grover 4:5b64235d1b85 66 }
Rohit Grover 4:5b64235d1b85 67
Rohit Grover 4:5b64235d1b85 68 int main(void)
Rohit Grover 4:5b64235d1b85 69 {
Rohit Grover 4:5b64235d1b85 70 led1 = 1;
Rohit Grover 4:5b64235d1b85 71 Ticker ticker;
Rohit Grover 4:5b64235d1b85 72 ticker.attach(periodicCallback, 1);
Rohit Grover 4:5b64235d1b85 73
Rohit Grover 4:5b64235d1b85 74 DEBUG("Initialising the nRF51822\n\r");
Rohit Grover 4:5b64235d1b85 75 ble.init();
Rohit Grover 4:5b64235d1b85 76 ble.onDisconnection(disconnectionCallback);
Rohit Grover 4:5b64235d1b85 77
Rohit Grover 4:5b64235d1b85 78 /* setup advertising */
Rohit Grover 4:5b64235d1b85 79 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Rohit Grover 4:5b64235d1b85 80 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Rohit Grover 4:5b64235d1b85 81 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
Rohit Grover 4:5b64235d1b85 82 ble.startAdvertising();
Rohit Grover 4:5b64235d1b85 83
Rohit Grover 4:5b64235d1b85 84 ble.addService(battService);
Rohit Grover 4:5b64235d1b85 85
Rohit Grover 4:5b64235d1b85 86 while (true) {
Rohit Grover 4:5b64235d1b85 87 ble.waitForEvent();
Rohit Grover 4:5b64235d1b85 88 }
Rohit Grover 4:5b64235d1b85 89 }