A nice BLE demo program which allows remote switch of an LED via GATT interface.

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Button by Bluetooth Low Energy

Committer:
hux
Date:
Sat Jan 07 23:48:53 2017 +0000
Revision:
11:80f2c19ecbce
New Blob class derived from BLE class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hux 11:80f2c19ecbce 1 // enroll.h - enroll a service
hux 11:80f2c19ecbce 2 //
hux 11:80f2c19ecbce 3 // Synopsis:
hux 11:80f2c19ecbce 4 //
hux 11:80f2c19ecbce 5 // void enroll(Blob &o, GattService &gservice);
hux 11:80f2c19ecbce 6 // void enroll(Blob &o, uint16_t bcid = 0xFFFF);
hux 11:80f2c19ecbce 7 //
hux 11:80f2c19ecbce 8 // void enroll(Blob &o, Service &service, uint16_t bcid = 0xFFFF);
hux 11:80f2c19ecbce 9 //
hux 11:80f2c19ecbce 10 // Arguments:
hux 11:80f2c19ecbce 11 //
hux 11:80f2c19ecbce 12 // o: Blob object (to avoid name clashes)
hux 11:80f2c19ecbce 13 // gservice: The GattService to be enrolled
hux 11:80f2c19ecbce 14 // service: The Service to be enrolled
hux 11:80f2c19ecbce 15 // bcid: Broadcast ID (optional); if not provided the default value
hux 11:80f2c19ecbce 16 // 0xFFFF will be used
hux 11:80f2c19ecbce 17 //
hux 11:80f2c19ecbce 18 // Description
hux 11:80f2c19ecbce 19 //
hux 11:80f2c19ecbce 20 // There are three ways to call enrollment.
hux 11:80f2c19ecbce 21 //
hux 11:80f2c19ecbce 22 // In the first case the service has been setup via a GattService class.
hux 11:80f2c19ecbce 23 // The service is enrolled by registering the service @ GAP. On advertising
hux 11:80f2c19ecbce 24 // the provided broadcast ID is used (otherwise default ID 0xFFFF).
hux 11:80f2c19ecbce 25 //
hux 11:80f2c19ecbce 26 // The second case is based on a Service class object which has been setup
hux 11:80f2c19ecbce 27 // by having added a set of characteristics to the internal collection. Upon
hux 11:80f2c19ecbce 28 // enrollment a GattService instance will be created internally and enroll-
hux 11:80f2c19ecbce 29 // ment of this GattService will be performed according to the first case.
hux 11:80f2c19ecbce 30 //
hux 11:80f2c19ecbce 31 // The third way is to enroll only the service id. This calling syntax is
hux 11:80f2c19ecbce 32 // used, if a Gatt servive is pre-enrolled without enrolling the service ID.
hux 11:80f2c19ecbce 33 //
hux 11:80f2c19ecbce 34 // Example 1: enrollment of GattService
hux 11:80f2c19ecbce 35 //
hux 11:80f2c19ecbce 36 // enroll(o,gservice);
hux 11:80f2c19ecbce 37 // enroll(o,0xFFFF);
hux 11:80f2c19ecbce 38 //
hux 11:80f2c19ecbce 39 // See also: SERVICE
hux 11:80f2c19ecbce 40 //
hux 11:80f2c19ecbce 41 #ifndef _ENROLL_H_
hux 11:80f2c19ecbce 42 #define _ENROLL_H_
hux 11:80f2c19ecbce 43
hux 11:80f2c19ecbce 44 #include "bricks/blob.h"
hux 11:80f2c19ecbce 45 #include "bricks/service.h"
hux 11:80f2c19ecbce 46
hux 11:80f2c19ecbce 47
hux 11:80f2c19ecbce 48 inline void enroll(Blob &o, uint16_t bcid = 0xFFFF)
hux 11:80f2c19ecbce 49 {
hux 11:80f2c19ecbce 50 static uint16_t list[1];
hux 11:80f2c19ecbce 51
hux 11:80f2c19ecbce 52 // Custom UUID, FFFF is reserved for development
hux 11:80f2c19ecbce 53 // Used for UUID's broadcast in advertising packet
hux 11:80f2c19ecbce 54
hux 11:80f2c19ecbce 55 list[0] = bcid; // set broadcast ID
hux 11:80f2c19ecbce 56
hux 11:80f2c19ecbce 57 // o.pble->gap().accumulateAdvertisingPayload(
hux 11:80f2c19ecbce 58 o.gap().accumulateAdvertisingPayload(
hux 11:80f2c19ecbce 59 GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
hux 11:80f2c19ecbce 60 (uint8_t *)list, sizeof(list));
hux 11:80f2c19ecbce 61 }
hux 11:80f2c19ecbce 62
hux 11:80f2c19ecbce 63
hux 11:80f2c19ecbce 64 inline void enroll(Blob &o, GattService &gservice, uint16_t bcid = 0xFFFF)
hux 11:80f2c19ecbce 65 {
hux 11:80f2c19ecbce 66 // o.pble->addService(gservice); // add service to GATT attributes
hux 11:80f2c19ecbce 67 o.addService(gservice); // add service to GATT attributes
hux 11:80f2c19ecbce 68 enroll(o,bcid); // enroll GattService (without BCID)
hux 11:80f2c19ecbce 69 }
hux 11:80f2c19ecbce 70
hux 11:80f2c19ecbce 71
hux 11:80f2c19ecbce 72 inline void enroll(Blob &o, Service &service, uint16_t bcid = 0)
hux 11:80f2c19ecbce 73 {
hux 11:80f2c19ecbce 74 Collection &collection = service.collection;
hux 11:80f2c19ecbce 75 GattService gservice(service.uuid, collection.plist, collection.count);
hux 11:80f2c19ecbce 76
hux 11:80f2c19ecbce 77 if (bcid == 0)
hux 11:80f2c19ecbce 78 bcid = service.uuid;
hux 11:80f2c19ecbce 79
hux 11:80f2c19ecbce 80 enroll(o,gservice,bcid); // enroll GattService (with BCID)
hux 11:80f2c19ecbce 81 }
hux 11:80f2c19ecbce 82
hux 11:80f2c19ecbce 83 #endif // _ENROLL_H_