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:
Vincent Coubard
Date:
Tue Sep 20 12:27:09 2016 +0100
Revision:
19:1e34f137b7ce
Parent:
17:5afb0e5a48fc
Update libraries, add support for ST shield.

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 12:4024aa3f72b0 21 DigitalOut led1(LED1, 1);
rgrover1 12:4024aa3f72b0 22 Ticker t;
vcoubard 17:5afb0e5a48fc 23 BatteryService *batteryService = NULL;
vcoubard 17:5afb0e5a48fc 24 uint8_t batteryLevel = 50;
Rohit Grover 4:5b64235d1b85 25
vcoubard 17:5afb0e5a48fc 26 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *disconnectionParams)
Rohit Grover 4:5b64235d1b85 27 {
vcoubard 17:5afb0e5a48fc 28 printf("Disconnected handle %u!\n\r", disconnectionParams->handle);
rgrover1 12:4024aa3f72b0 29 printf("Restarting the advertising process\n\r");
vcoubard 17:5afb0e5a48fc 30 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
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
vcoubard 17:5afb0e5a48fc 38 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
vcoubard 17:5afb0e5a48fc 39 {
vcoubard 17:5afb0e5a48fc 40 BLE &ble = params->ble;
vcoubard 17:5afb0e5a48fc 41 ble_error_t error = params->error;
vcoubard 17:5afb0e5a48fc 42 Gap& gap = ble.gap();
vcoubard 17:5afb0e5a48fc 43
vcoubard 17:5afb0e5a48fc 44 if (error != BLE_ERROR_NONE) {
vcoubard 17:5afb0e5a48fc 45 return;
vcoubard 17:5afb0e5a48fc 46 }
vcoubard 17:5afb0e5a48fc 47
vcoubard 17:5afb0e5a48fc 48 gap.onDisconnection(disconnectionCallback);
vcoubard 17:5afb0e5a48fc 49
vcoubard 17:5afb0e5a48fc 50 batteryService = new BatteryService(ble, batteryLevel);
vcoubard 17:5afb0e5a48fc 51
vcoubard 17:5afb0e5a48fc 52 /* setup advertising */
vcoubard 17:5afb0e5a48fc 53 gap.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
vcoubard 17:5afb0e5a48fc 54 gap.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
vcoubard 17:5afb0e5a48fc 55 gap.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
vcoubard 17:5afb0e5a48fc 56 gap.startAdvertising();
vcoubard 17:5afb0e5a48fc 57 }
vcoubard 17:5afb0e5a48fc 58
Rohit Grover 4:5b64235d1b85 59 int main(void)
Rohit Grover 4:5b64235d1b85 60 {
rgrover1 12:4024aa3f72b0 61 t.attach(blink, 1.0f);
Rohit Grover 4:5b64235d1b85 62
rgrover1 12:4024aa3f72b0 63 printf("Initialising the nRF51822\n\r");
rgrover1 12:4024aa3f72b0 64
vcoubard 17:5afb0e5a48fc 65 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
vcoubard 17:5afb0e5a48fc 66 ble.init(bleInitComplete);
rgrover1 13:60e095fe4b45 67
vcoubard 17:5afb0e5a48fc 68 /* SpinWait for initialization to complete. This is necessary because the
vcoubard 17:5afb0e5a48fc 69 * BLE object is used in the main loop below. */
vcoubard 17:5afb0e5a48fc 70 while (ble.hasInitialized() == false) { /* spin loop */ }
Rohit Grover 4:5b64235d1b85 71
Rohit Grover 4:5b64235d1b85 72 while (true) {
rgrover1 12:4024aa3f72b0 73 ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup)
rgrover1 12:4024aa3f72b0 74
rgrover1 12:4024aa3f72b0 75 // the magic battery processing
rgrover1 12:4024aa3f72b0 76 batteryLevel++;
rgrover1 12:4024aa3f72b0 77 if (batteryLevel > 100) {
rgrover1 12:4024aa3f72b0 78 batteryLevel = 20;
rgrover1 12:4024aa3f72b0 79 }
rgrover1 12:4024aa3f72b0 80
vcoubard 17:5afb0e5a48fc 81 batteryService->updateBatteryLevel(batteryLevel);
Rohit Grover 4:5b64235d1b85 82 }
Rohit Grover 4:5b64235d1b85 83 }