A template for applications where some small amount of data needs to be notified to a phone app over BLE. It is a good starting point for notifications.

Dependencies:   BLE_API mbed nRF51822

Demo for an Input Service

To help you create your own BLE services, we've created a series of service templates. The *input service template* demonstrates the use of a simple input (boolean values) from a read-only characteristic.

The template covers:

1. Setting up advertising and connection states.

2. Assigning UUIDs to the service and its characteristic.

3. Creating an input characteristic: read-only, boolean, with notifications. This characteristic is updated according to the button's state.

4. Constructing a service class and adding it to the BLE stack.

Files at this revision

API Documentation at this revision

Comitter:
rgrover1
Date:
Sat Jun 20 23:52:29 2015 +0000
Parent:
5:43a3ab27f2e4
Child:
7:a02fdfbc253e
Commit message:
switch to newer APIs; rename BLEDevice to BLE

Changed in this revision

ButtonService.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/ButtonService.h	Sat Jun 20 00:00:32 2015 +0000
+++ b/ButtonService.h	Sat Jun 20 23:52:29 2015 +0000
@@ -22,20 +22,20 @@
     const static uint16_t BUTTON_SERVICE_UUID              = 0xA000;
     const static uint16_t BUTTON_STATE_CHARACTERISTIC_UUID = 0xA001;
 
-    ButtonService(BLEDevice &_ble, bool buttonPressedInitial) :
+    ButtonService(BLE &_ble, bool buttonPressedInitial) :
         ble(_ble), buttonState(BUTTON_STATE_CHARACTERISTIC_UUID, &buttonPressedInitial, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
     {
         GattCharacteristic *charTable[] = {&buttonState};
         GattService         buttonService(ButtonService::BUTTON_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
-        ble.addService(buttonService);
+        ble.gattServer().addService(buttonService);
     }
 
     void updateButtonState(bool newState) {
-        ble.updateCharacteristicValue(buttonState.getValueHandle(), (uint8_t *)&newState, sizeof(bool));
+        ble.gattServer().write(buttonState.getValueHandle(), (uint8_t *)&newState, sizeof(bool));
     }
 
 private:
-    BLEDevice                        &ble;
+    BLE                              &ble;
     ReadOnlyGattCharacteristic<bool>  buttonState;
 };
 
--- a/main.cpp	Sat Jun 20 00:00:32 2015 +0000
+++ b/main.cpp	Sat Jun 20 23:52:29 2015 +0000
@@ -18,7 +18,7 @@
 #include "BLE.h"
 #include "ButtonService.h"
 
-BLEDevice   ble;
+BLE         ble;
 DigitalOut  led1(LED1);
 InterruptIn button(BUTTON1);
 
@@ -39,7 +39,7 @@
 
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
-    ble.startAdvertising();
+    ble.gap().startAdvertising();
 }
 
 void periodicCallback(void)
@@ -56,18 +56,18 @@
     button.rise(buttonReleasedCallback);
 
     ble.init();
-    ble.onDisconnection(disconnectionCallback);
+    ble.gap().onDisconnection(disconnectionCallback);
 
     ButtonService buttonService(ble, false /* initial value for button pressed */);
     buttonServicePtr = &buttonService;
 
     /* setup advertising */
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
-    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.setAdvertisingInterval(1000); /* 1000ms. */
-    ble.startAdvertising();
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
+    ble.gap().startAdvertising();
 
     while (true) {
         ble.waitForEvent();