Small Demo demonstrating BLE Advertising
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp@24:7bd093ad7d63, 2016-12-10 (annotated)
- Committer:
- hux
- Date:
- Sat Dec 10 23:13:00 2016 +0000
- Revision:
- 24:7bd093ad7d63
- Parent:
- 23:f06c9851c72c
- Child:
- 25:f3b44a34cf5d
disconnect callback setup implemented
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:eb7f02ad28a7 | 1 | /* mbed Microcontroller Library |
screamer | 0:eb7f02ad28a7 | 2 | * Copyright (c) 2006-2015 ARM Limited |
screamer | 0:eb7f02ad28a7 | 3 | * |
screamer | 0:eb7f02ad28a7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
screamer | 0:eb7f02ad28a7 | 5 | * you may not use this file except in compliance with the License. |
screamer | 0:eb7f02ad28a7 | 6 | * You may obtain a copy of the License at |
screamer | 0:eb7f02ad28a7 | 7 | * |
screamer | 0:eb7f02ad28a7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
screamer | 0:eb7f02ad28a7 | 9 | * |
screamer | 0:eb7f02ad28a7 | 10 | * Unless required by applicable law or agreed to in writing, software |
screamer | 0:eb7f02ad28a7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
screamer | 0:eb7f02ad28a7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
screamer | 0:eb7f02ad28a7 | 13 | * See the License for the specific language governing permissions and |
screamer | 0:eb7f02ad28a7 | 14 | * limitations under the License. |
screamer | 0:eb7f02ad28a7 | 15 | */ |
hux | 22:e82c7b8a6072 | 16 | |
screamer | 0:eb7f02ad28a7 | 17 | #include "mbed.h" |
screamer | 0:eb7f02ad28a7 | 18 | #include "ble/BLE.h" |
hux | 22:e82c7b8a6072 | 19 | #include "bricks/blob.h" |
hux | 22:e82c7b8a6072 | 20 | #include "bricks/blink.h" |
screamer | 0:eb7f02ad28a7 | 21 | |
hux | 22:e82c7b8a6072 | 22 | const char *idle = "x "; // 'idle' blinking sequence |
hux | 24:7bd093ad7d63 | 23 | const char *advertise = "x xxx "; // 'advertise' blinking sequence |
hux | 22:e82c7b8a6072 | 24 | const char *action = "x x x x x "; // 'action' blinking sequence |
hux | 24:7bd093ad7d63 | 25 | const char *faulty = "x x xxx "; // 'fault' blinking sequence |
hux | 24:7bd093ad7d63 | 26 | |
hux | 24:7bd093ad7d63 | 27 | // Add a device name for human readability |
screamer | 0:eb7f02ad28a7 | 28 | |
hux | 24:7bd093ad7d63 | 29 | const static char DEVICE_NAME[] = "T05N_Advertising"; |
hux | 24:7bd093ad7d63 | 30 | |
hux | 24:7bd093ad7d63 | 31 | // You have up to 26 bytes of advertising data to use. |
hux | 24:7bd093ad7d63 | 32 | // Hex data example: {0x01,0x02,0x03,0x04,0x05} |
hux | 24:7bd093ad7d63 | 33 | // Char data example: {"ChangeThisData"} |
hux | 24:7bd093ad7d63 | 34 | |
hux | 24:7bd093ad7d63 | 35 | const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05}; |
apalmieri | 13:227a0149b677 | 36 | |
hux | 24:7bd093ad7d63 | 37 | //============================================================================== |
hux | 24:7bd093ad7d63 | 38 | // Some Callbacks |
hux | 24:7bd093ad7d63 | 39 | //============================================================================== |
screamer | 0:eb7f02ad28a7 | 40 | |
hux | 24:7bd093ad7d63 | 41 | void onDisconnect(Blob &blob) // Disconnection Callback |
hux | 24:7bd093ad7d63 | 42 | { |
hux | 24:7bd093ad7d63 | 43 | BLE::Instance().gap().startAdvertising(); |
hux | 24:7bd093ad7d63 | 44 | blinking("advertise"); |
hux | 24:7bd093ad7d63 | 45 | } |
hux | 23:f06c9851c72c | 46 | |
hux | 24:7bd093ad7d63 | 47 | void onError(Blob &blob) // Error Reporting Callback |
hux | 24:7bd093ad7d63 | 48 | { |
hux | 24:7bd093ad7d63 | 49 | (void) blob; // Avoid compiler warnings |
hux | 24:7bd093ad7d63 | 50 | /* Initialization error handling should go here */ |
hux | 24:7bd093ad7d63 | 51 | } |
hux | 23:f06c9851c72c | 52 | |
hux | 23:f06c9851c72c | 53 | |
hux | 24:7bd093ad7d63 | 54 | void onSetup(Blob &blob) // Immediately After Initializing BLE |
hux | 23:f06c9851c72c | 55 | { |
hux | 23:f06c9851c72c | 56 | if (blob.error != BLE_ERROR_NONE) |
hux | 23:f06c9851c72c | 57 | { |
hux | 24:7bd093ad7d63 | 58 | onError(blob); |
hux | 23:f06c9851c72c | 59 | return; |
hux | 23:f06c9851c72c | 60 | } |
apalmieri | 13:227a0149b677 | 61 | |
hux | 24:7bd093ad7d63 | 62 | blob.device(DEVICE_NAME); // setup device name |
hux | 24:7bd093ad7d63 | 63 | blob.disconnect(onDisconnect); // setup disconnect callback |
hux | 24:7bd093ad7d63 | 64 | |
hux | 23:f06c9851c72c | 65 | /* Optional: add callback for disconnection */ |
hux | 24:7bd093ad7d63 | 66 | //blob.pble->gap().onDisconnection(onDisconnect); |
apalmieri | 13:227a0149b677 | 67 | |
hux | 23:f06c9851c72c | 68 | /* Sacrifice 3B of 31B to Advertising Flags */ |
hux | 23:f06c9851c72c | 69 | blob.pble->gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); |
hux | 23:f06c9851c72c | 70 | blob.pble->gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
screamer | 0:eb7f02ad28a7 | 71 | |
hux | 23:f06c9851c72c | 72 | /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */ |
hux | 23:f06c9851c72c | 73 | blob.pble->gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData)); |
screamer | 0:eb7f02ad28a7 | 74 | |
hux | 23:f06c9851c72c | 75 | /* Optional: Add name to device */ |
hux | 23:f06c9851c72c | 76 | blob.pble->gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
screamer | 0:eb7f02ad28a7 | 77 | |
hux | 23:f06c9851c72c | 78 | /* Set advertising interval. Longer interval == longer battery life */ |
hux | 23:f06c9851c72c | 79 | blob.pble->gap().setAdvertisingInterval(100); /* 100ms */ |
screamer | 0:eb7f02ad28a7 | 80 | |
hux | 23:f06c9851c72c | 81 | /* Start advertising */ |
hux | 23:f06c9851c72c | 82 | blob.pble->gap().startAdvertising(); |
hux | 24:7bd093ad7d63 | 83 | |
hux | 24:7bd093ad7d63 | 84 | blinking("advertise"); |
hux | 23:f06c9851c72c | 85 | } |
hux | 23:f06c9851c72c | 86 | |
apalmieri | 13:227a0149b677 | 87 | |
hux | 22:e82c7b8a6072 | 88 | int main(void) |
hux | 22:e82c7b8a6072 | 89 | { |
hux | 22:e82c7b8a6072 | 90 | Blob blob; |
hux | 24:7bd093ad7d63 | 91 | blinking(idle); // idle blinking - everything is OK! |
hux | 24:7bd093ad7d63 | 92 | wait(5.0); |
hux | 23:f06c9851c72c | 93 | |
hux | 24:7bd093ad7d63 | 94 | blob.init(onSetup); // init BLE base layer, always do first |
hux | 24:7bd093ad7d63 | 95 | blinking(advertise); // idle blinking - everything is OK! |
hux | 24:7bd093ad7d63 | 96 | |
hux | 24:7bd093ad7d63 | 97 | while (true) // Infinite loop waiting for BLE events */ |
hux | 22:e82c7b8a6072 | 98 | { |
hux | 24:7bd093ad7d63 | 99 | blob.sleep(); // low power waiting for BLE events |
hux | 22:e82c7b8a6072 | 100 | } |
hux | 22:e82c7b8a6072 | 101 | } |