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:
rgrover1
Date:
Mon Sep 22 10:28:55 2014 +0000
Revision:
8:45beed07b093
Parent:
5:77ad8d8dc9c5
Child:
12:4024aa3f72b0
updating to 0.2.0 of the BLE_API

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"
rgrover1 8:45beed07b093 19 #include "BatteryService.h"
Rohit Grover 4:5b64235d1b85 20
Rohit Grover 4:5b64235d1b85 21 BLEDevice ble;
Rohit Grover 4:5b64235d1b85 22
Rohit Grover 4:5b64235d1b85 23 DigitalOut led1(LED1);
Rohit Grover 4:5b64235d1b85 24
Rohit Grover 4:5b64235d1b85 25 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
Rohit Grover 4:5b64235d1b85 26 * it will have an impact on code-size and power
Rohit Grover 4:5b64235d1b85 27 * consumption. */
Rohit Grover 4:5b64235d1b85 28
Rohit Grover 4:5b64235d1b85 29 #if NEED_CONSOLE_OUTPUT
Rohit Grover 4:5b64235d1b85 30 Serial pc(USBTX, USBRX);
Rohit Grover 4:5b64235d1b85 31 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
Rohit Grover 4:5b64235d1b85 32 #else
Rohit Grover 4:5b64235d1b85 33 #define DEBUG(...) /* nothing */
Rohit Grover 4:5b64235d1b85 34 #endif /* #if NEED_CONSOLE_OUTPUT */
Rohit Grover 4:5b64235d1b85 35
rgrover1 8:45beed07b093 36 BatteryService *batteryServicePtr = NULL;
Rohit Grover 4:5b64235d1b85 37
rgrover1 8:45beed07b093 38 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
Rohit Grover 4:5b64235d1b85 39 {
rgrover1 8:45beed07b093 40 DEBUG("Disconnected handle %u!\n\r", handle);
Rohit Grover 4:5b64235d1b85 41 DEBUG("Restarting the advertising process\n\r");
Rohit Grover 4:5b64235d1b85 42 ble.startAdvertising();
Rohit Grover 4:5b64235d1b85 43 }
Rohit Grover 4:5b64235d1b85 44
Rohit Grover 4:5b64235d1b85 45 /**
Rohit Grover 4:5b64235d1b85 46 * Runs once a second in interrupt context triggered by the 'ticker'; updates
Rohit Grover 4:5b64235d1b85 47 * battery level if there is a connection.
Rohit Grover 4:5b64235d1b85 48 */
Rohit Grover 4:5b64235d1b85 49 void periodicCallback(void)
Rohit Grover 4:5b64235d1b85 50 {
Rohit Grover 4:5b64235d1b85 51 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
Rohit Grover 4:5b64235d1b85 52
Rohit Grover 4:5b64235d1b85 53 if (ble.getGapState().connected) {
Rohit Grover 4:5b64235d1b85 54 /* Update battery level */
rgrover1 8:45beed07b093 55 static uint8_t batt = 50;
Rohit Grover 4:5b64235d1b85 56 batt++;
Rohit Grover 4:5b64235d1b85 57 if (batt > 100) {
Rohit Grover 4:5b64235d1b85 58 batt = 72;
Rohit Grover 4:5b64235d1b85 59 }
rgrover1 8:45beed07b093 60 batteryServicePtr->updateBatteryLevel(batt);
Rohit Grover 4:5b64235d1b85 61 }
Rohit Grover 4:5b64235d1b85 62 }
Rohit Grover 4:5b64235d1b85 63
Rohit Grover 4:5b64235d1b85 64 int main(void)
Rohit Grover 4:5b64235d1b85 65 {
Rohit Grover 4:5b64235d1b85 66 led1 = 1;
Rohit Grover 4:5b64235d1b85 67 Ticker ticker;
Rohit Grover 4:5b64235d1b85 68 ticker.attach(periodicCallback, 1);
Rohit Grover 4:5b64235d1b85 69
Rohit Grover 4:5b64235d1b85 70 DEBUG("Initialising the nRF51822\n\r");
Rohit Grover 4:5b64235d1b85 71 ble.init();
Rohit Grover 4:5b64235d1b85 72 ble.onDisconnection(disconnectionCallback);
Rohit Grover 4:5b64235d1b85 73
Rohit Grover 4:5b64235d1b85 74 /* setup advertising */
Rohit Grover 4:5b64235d1b85 75 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Rohit Grover 4:5b64235d1b85 76 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Rohit Grover 4:5b64235d1b85 77 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
Rohit Grover 4:5b64235d1b85 78 ble.startAdvertising();
Rohit Grover 4:5b64235d1b85 79
rgrover1 8:45beed07b093 80 BatteryService batterService(ble);
rgrover1 8:45beed07b093 81 batteryServicePtr = &batterService;
Rohit Grover 4:5b64235d1b85 82
Rohit Grover 4:5b64235d1b85 83 while (true) {
Rohit Grover 4:5b64235d1b85 84 ble.waitForEvent();
Rohit Grover 4:5b64235d1b85 85 }
Rohit Grover 4:5b64235d1b85 86 }