Small Demo demonstrating BLE Advertising
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp@28:114eaad388c1, 2017-01-08 (annotated)
- Committer:
- hux
- Date:
- Sun Jan 08 23:13:25 2017 +0000
- Revision:
- 28:114eaad388c1
- Parent:
- 27:09ec26511db8
- Child:
- 29:8eb46b976f0f
A bit more code for this demo to demonstrate GAP advertising with advertising data.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 28:114eaad388c1 | 1 | // N05_Advertising: Tutorial for demonstration of simple advertising |
hux | 28:114eaad388c1 | 2 | // Program has been tested on nRF51822-DK |
hux | 22:e82c7b8a6072 | 3 | |
hux | 28:114eaad388c1 | 4 | #include "bricks/bricks.h" |
hux | 24:7bd093ad7d63 | 5 | |
hux | 24:7bd093ad7d63 | 6 | // Add a device name for human readability |
screamer | 0:eb7f02ad28a7 | 7 | |
hux | 27:09ec26511db8 | 8 | const static char DEVICE_NAME[] = "Stupid Board"; |
hux | 24:7bd093ad7d63 | 9 | |
hux | 24:7bd093ad7d63 | 10 | // You have up to 26 bytes of advertising data to use. |
hux | 24:7bd093ad7d63 | 11 | // Hex data example: {0x01,0x02,0x03,0x04,0x05} |
hux | 24:7bd093ad7d63 | 12 | // Char data example: {"ChangeThisData"} |
hux | 24:7bd093ad7d63 | 13 | |
hux | 24:7bd093ad7d63 | 14 | const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05}; |
apalmieri | 13:227a0149b677 | 15 | |
hux | 24:7bd093ad7d63 | 16 | //============================================================================== |
hux | 24:7bd093ad7d63 | 17 | // Some Callbacks |
hux | 24:7bd093ad7d63 | 18 | //============================================================================== |
screamer | 0:eb7f02ad28a7 | 19 | |
hux | 28:114eaad388c1 | 20 | void cbDisconnect(O&o) // disconnection callback |
hux | 24:7bd093ad7d63 | 21 | { |
hux | 28:114eaad388c1 | 22 | advertise(o); // start advertising |
hux | 28:114eaad388c1 | 23 | blinkAdvertise(o); // indicate advertising |
hux | 24:7bd093ad7d63 | 24 | } |
hux | 23:f06c9851c72c | 25 | |
hux | 25:f3b44a34cf5d | 26 | |
hux | 28:114eaad388c1 | 27 | void cbError(O&o) // Error Reporting Callback |
hux | 24:7bd093ad7d63 | 28 | { |
hux | 28:114eaad388c1 | 29 | blinkError(o); // indicate an error |
hux | 24:7bd093ad7d63 | 30 | } |
hux | 23:f06c9851c72c | 31 | |
hux | 23:f06c9851c72c | 32 | |
hux | 28:114eaad388c1 | 33 | void cbSetup(O&o) // setup calback (after BLE init) |
hux | 23:f06c9851c72c | 34 | { |
hux | 28:114eaad388c1 | 35 | device(o,DEVICE_NAME); // setup device name |
hux | 28:114eaad388c1 | 36 | name(o,"N05#1.0 Advertise"); // add name to device |
hux | 28:114eaad388c1 | 37 | data(o,AdvData,sizeof(AdvData)); // advertising user data |
apalmieri | 13:227a0149b677 | 38 | |
hux | 28:114eaad388c1 | 39 | onDisconnect(o,cbDisconnect); // setup disconnection callback |
screamer | 0:eb7f02ad28a7 | 40 | |
hux | 28:114eaad388c1 | 41 | payload(o,"C:ng"); // no BR/EDR, general discoverable (UDSN) |
hux | 28:114eaad388c1 | 42 | advertise(o,100); // start advertising @ 100 msec interval |
hux | 28:114eaad388c1 | 43 | blinkAdvertise(o); // indicate advertising mode |
hux | 23:f06c9851c72c | 44 | } |
hux | 23:f06c9851c72c | 45 | |
apalmieri | 13:227a0149b677 | 46 | |
hux | 22:e82c7b8a6072 | 47 | int main(void) |
hux | 22:e82c7b8a6072 | 48 | { |
hux | 28:114eaad388c1 | 49 | O o; // Our Blob (BLuetooth OBject) |
hux | 28:114eaad388c1 | 50 | verbose(o); // verbose talking (all levels) |
hux | 28:114eaad388c1 | 51 | blinkIdle(o); // idle blinking - everything is OK! |
hux | 27:09ec26511db8 | 52 | |
hux | 28:114eaad388c1 | 53 | init(o,cbSetup,cbError); // init BLE base layer, always do first |
hux | 24:7bd093ad7d63 | 54 | |
hux | 28:114eaad388c1 | 55 | while (true) // Infinite loop waiting for BLE events |
hux | 28:114eaad388c1 | 56 | sleep(o); // low power waiting for BLE events |
hux | 22:e82c7b8a6072 | 57 | } |