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:
ktownsend
Date:
Thu Jan 16 22:31:47 2014 +0000
Revision:
3:288b2baffd14
Parent:
2:a872df2e051e
Child:
4:5b64235d1b85
Updated to latest BLE_API version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 0:395932faedf1 1 #include "mbed.h"
ktownsend 3:288b2baffd14 2 #include "blecommon.h"
ktownsend 3:288b2baffd14 3 #include "hw/nRF51822s/nRF51822s.h"
ktownsend 0:395932faedf1 4
ktownsend 3:288b2baffd14 5 nRF51822s nrf ( p9, p10, p30, p29 ); /* (tx, rx, rts, cts) */
ktownsend 3:288b2baffd14 6 GattService battService ( 0x180F );
ktownsend 3:288b2baffd14 7 GattCharacteristic battLevel ( 0x2A19, 1, 1,
ktownsend 3:288b2baffd14 8 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
ktownsend 3:288b2baffd14 9 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
ktownsend 0:395932faedf1 10
ktownsend 0:395932faedf1 11 int main(void)
ktownsend 0:395932faedf1 12 {
ktownsend 3:288b2baffd14 13 /* Make sure we get a clean start */
ktownsend 3:288b2baffd14 14 nrf.reset();
ktownsend 0:395932faedf1 15
ktownsend 0:395932faedf1 16 /* Add the characteristic to our service */
ktownsend 0:395932faedf1 17 battService.addCharacteristic(battLevel);
ktownsend 0:395932faedf1 18
ktownsend 0:395932faedf1 19 /* Pass the service into the radio */
ktownsend 3:288b2baffd14 20 nrf.getGattServer().addService(battService);
ktownsend 0:395932faedf1 21
ktownsend 0:395932faedf1 22 /* Configure the radio and start advertising with default values */
ktownsend 0:395932faedf1 23 /* Make sure you've added all of your services before calling this function! */
ktownsend 3:288b2baffd14 24 nrf.getGap().startAdvertising();
ktownsend 0:395932faedf1 25
ktownsend 0:395932faedf1 26 /* Now that we're live, update the battery level characteristic */
ktownsend 0:395932faedf1 27 uint8_t batt = 72;
ktownsend 3:288b2baffd14 28 nrf.getGattServer().updateValue(battLevel.handle, (uint8_t*)&batt, sizeof(batt));
ktownsend 0:395932faedf1 29
ktownsend 0:395932faedf1 30 while(1);
ktownsend 0:395932faedf1 31 }