Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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.
Diff: main.cpp
- Revision:
- 8:45beed07b093
- Parent:
- 5:77ad8d8dc9c5
- Child:
- 12:4024aa3f72b0
diff -r 85c603b950f3 -r 45beed07b093 main.cpp
--- a/main.cpp Fri Jun 13 12:12:18 2014 +0000
+++ b/main.cpp Mon Sep 22 10:28:55 2014 +0000
@@ -16,6 +16,7 @@
#include "mbed.h"
#include "BLEDevice.h"
+#include "BatteryService.h"
BLEDevice ble;
@@ -32,17 +33,11 @@
#define DEBUG(...) /* nothing */
#endif /* #if NEED_CONSOLE_OUTPUT */
-/* Battery Level Service */
-uint8_t batt = 72; /* Battery level */
-GattCharacteristic battLevel (GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batt, sizeof(batt), sizeof(batt),
- GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
-GattCharacteristic *battChars[] = {&battLevel};
-GattService battService (GattService::UUID_BATTERY_SERVICE, battChars, sizeof(battChars) / sizeof(GattCharacteristic *));
+BatteryService *batteryServicePtr = NULL;
-
-void disconnectionCallback(void)
+void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
- DEBUG("Disconnected!\n\r");
+ DEBUG("Disconnected handle %u!\n\r", handle);
DEBUG("Restarting the advertising process\n\r");
ble.startAdvertising();
}
@@ -57,11 +52,12 @@
if (ble.getGapState().connected) {
/* Update battery level */
+ static uint8_t batt = 50;
batt++;
if (batt > 100) {
batt = 72;
}
- ble.updateCharacteristicValue(battLevel.getHandle(), (uint8_t *)&batt, sizeof(batt));
+ batteryServicePtr->updateBatteryLevel(batt);
}
}
@@ -81,7 +77,8 @@
ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
ble.startAdvertising();
- ble.addService(battService);
+ BatteryService batterService(ble);
+ batteryServicePtr = &batterService;
while (true) {
ble.waitForEvent();