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:
Sat Jun 20 23:22:18 2015 +0000
Revision:
16:5cdd04cf1ed4
Parent:
14:39b4b11d9cf5
Child:
17:5afb0e5a48fc
updating underlying libraries.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rohit Grover 4:5b64235d1b85 1 /* mbed Microcontroller Library
rgrover1 12:4024aa3f72b0 2 * Copyright (c) 2006-2014 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"
rgrover1 16:5cdd04cf1ed4 18 #include "BLE.h"
rgrover1 8:45beed07b093 19 #include "BatteryService.h"
Rohit Grover 4:5b64235d1b85 20
rgrover1 16:5cdd04cf1ed4 21 BLE ble;
Rohit Grover 4:5b64235d1b85 22
rgrover1 12:4024aa3f72b0 23 DigitalOut led1(LED1, 1);
rgrover1 12:4024aa3f72b0 24 Ticker t;
Rohit Grover 4:5b64235d1b85 25
rgrover1 8:45beed07b093 26 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
Rohit Grover 4:5b64235d1b85 27 {
rgrover1 12:4024aa3f72b0 28 printf("Disconnected handle %u!\n\r", handle);
rgrover1 12:4024aa3f72b0 29 printf("Restarting the advertising process\n\r");
Rohit Grover 4:5b64235d1b85 30 ble.startAdvertising();
Rohit Grover 4:5b64235d1b85 31 }
Rohit Grover 4:5b64235d1b85 32
rgrover1 12:4024aa3f72b0 33 void blink(void)
Rohit Grover 4:5b64235d1b85 34 {
rgrover1 12:4024aa3f72b0 35 led1 = !led1;
Rohit Grover 4:5b64235d1b85 36 }
Rohit Grover 4:5b64235d1b85 37
Rohit Grover 4:5b64235d1b85 38 int main(void)
Rohit Grover 4:5b64235d1b85 39 {
rgrover1 12:4024aa3f72b0 40 uint8_t batteryLevel = 50;
rgrover1 12:4024aa3f72b0 41 t.attach(blink, 1.0f);
Rohit Grover 4:5b64235d1b85 42
rgrover1 12:4024aa3f72b0 43 printf("Initialising the nRF51822\n\r");
rgrover1 12:4024aa3f72b0 44
Rohit Grover 4:5b64235d1b85 45 ble.init();
Rohit Grover 4:5b64235d1b85 46 ble.onDisconnection(disconnectionCallback);
Rohit Grover 4:5b64235d1b85 47
rgrover1 13:60e095fe4b45 48 BatteryService batteryService(ble, batteryLevel);
rgrover1 13:60e095fe4b45 49
Rohit Grover 4:5b64235d1b85 50 /* setup advertising */
Rohit Grover 4:5b64235d1b85 51 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Rohit Grover 4:5b64235d1b85 52 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 14:39b4b11d9cf5 53 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
Rohit Grover 4:5b64235d1b85 54 ble.startAdvertising();
Rohit Grover 4:5b64235d1b85 55
Rohit Grover 4:5b64235d1b85 56 while (true) {
rgrover1 12:4024aa3f72b0 57 ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup)
rgrover1 12:4024aa3f72b0 58
rgrover1 12:4024aa3f72b0 59 // the magic battery processing
rgrover1 12:4024aa3f72b0 60 batteryLevel++;
rgrover1 12:4024aa3f72b0 61 if (batteryLevel > 100) {
rgrover1 12:4024aa3f72b0 62 batteryLevel = 20;
rgrover1 12:4024aa3f72b0 63 }
rgrover1 12:4024aa3f72b0 64
rgrover1 12:4024aa3f72b0 65 batteryService.updateBatteryLevel(batteryLevel);
Rohit Grover 4:5b64235d1b85 66 }
Rohit Grover 4:5b64235d1b85 67 }