Small Demo demonstrating BLE Advertising

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
hux
Date:
Sun Jan 08 23:13:25 2017 +0000
Revision:
28:114eaad388c1
A bit more code for this demo to demonstrate GAP advertising with advertising data.

Who changed what in which revision?

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