BLE GAP Example Nucleo IDB0XA1
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_GAP_Example by
main.cpp@14:6892af4291f1, 2015-12-11 (annotated)
- Committer:
- anoney180133
- Date:
- Fri Dec 11 13:13:09 2015 +0000
- Revision:
- 14:6892af4291f1
- Parent:
- 13:827dd2b32bb8
BLE GAP Example Nucleo with IDB0XA1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 1:0692bee84264 | 1 | // Headers necessary for mbed and BLE device mode |
mbedAustin | 0:5375be4301ed | 2 | #include "mbed.h" |
anoney180133 | 14:6892af4291f1 | 3 | #include "ble/BLE.h" |
anoney180133 | 14:6892af4291f1 | 4 | #include "ble/Gap.h" |
anoney180133 | 14:6892af4291f1 | 5 | #include "ble/services/BatteryService.h" |
anoney180133 | 14:6892af4291f1 | 6 | #include "ble/services/DeviceInformationService.h" |
mbedAustin | 0:5375be4301ed | 7 | |
anoney180133 | 14:6892af4291f1 | 8 | BLE ble; |
anoney180133 | 14:6892af4291f1 | 9 | Serial UART(SERIAL_TX, SERIAL_RX); // TX PA_2 , RX PA_3 |
anoney180133 | 14:6892af4291f1 | 10 | |
anoney180133 | 14:6892af4291f1 | 11 | /* We can arbiturarily choose the GAPButton service UUID to be 0xAA00 |
anoney180133 | 14:6892af4291f1 | 12 | * as long as it does not overlap with the UUIDs defined here: |
anoney180133 | 14:6892af4291f1 | 13 | * https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx */ |
anoney180133 | 14:6892af4291f1 | 14 | #define GAPButtonUUID 0xAA00 |
anoney180133 | 14:6892af4291f1 | 15 | static uint16_t uuid16_list[] = {GAPButtonUUID , 0x0000}; |
mbedAustin | 0:5375be4301ed | 16 | |
mbedAustin | 11:c9c0c4586c5f | 17 | // Optional: Device Name, add for human read-ability |
anoney180133 | 14:6892af4291f1 | 18 | static char DEVICE_NAME[] = "CESA BLE 4.0"; // Optional: device name |
mbedAustin | 1:0692bee84264 | 19 | |
mbedAustin | 13:827dd2b32bb8 | 20 | // Optional: Restart advertising when phone app disconnects |
anoney180133 | 14:6892af4291f1 | 21 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
mbedAustin | 13:827dd2b32bb8 | 22 | { |
anoney180133 | 14:6892af4291f1 | 23 | ble.gap().startAdvertising(); |
mbedAustin | 8:5442739198ec | 24 | } |
mbedAustin | 8:5442739198ec | 25 | |
anoney180133 | 14:6892af4291f1 | 26 | |
mbedAustin | 1:0692bee84264 | 27 | // main program |
mbedAustin | 1:0692bee84264 | 28 | int main(void) |
mbedAustin | 0:5375be4301ed | 29 | { |
anoney180133 | 14:6892af4291f1 | 30 | UART.baud(115200); // Set BuadRate |
anoney180133 | 14:6892af4291f1 | 31 | |
mbedAustin | 1:0692bee84264 | 32 | // Initialize BLE baselayer, always do this first! |
mbedAustin | 1:0692bee84264 | 33 | ble.init(); |
mbedAustin | 13:827dd2b32bb8 | 34 | |
mbedAustin | 11:c9c0c4586c5f | 35 | // Optional: add callback for disconnection |
anoney180133 | 14:6892af4291f1 | 36 | ble.gap().onDisconnection(disconnectionCallback); |
mbedAustin | 1:0692bee84264 | 37 | |
mbedAustin | 1:0692bee84264 | 38 | // Sacrifice 3B of 31B to Advertising Flags |
anoney180133 | 14:6892af4291f1 | 39 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); |
mbedAustin | 13:827dd2b32bb8 | 40 | |
anoney180133 | 14:6892af4291f1 | 41 | // Put the device name in the advertising payload |
anoney180133 | 14:6892af4291f1 | 42 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
anoney180133 | 14:6892af4291f1 | 43 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
anoney180133 | 14:6892af4291f1 | 44 | |
anoney180133 | 14:6892af4291f1 | 45 | |
anoney180133 | 14:6892af4291f1 | 46 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
mbedAustin | 1:0692bee84264 | 47 | // Set advertising interval. Longer interval = longer battery life |
anoney180133 | 14:6892af4291f1 | 48 | ble.gap().setAdvertisingInterval(100); // 100ms, set as percentage of a second |
anoney180133 | 14:6892af4291f1 | 49 | ble.gap().startAdvertising(); |
mbedAustin | 1:0692bee84264 | 50 | |
mbedAustin | 1:0692bee84264 | 51 | // Infinite loop waiting for BLE events |
anoney180133 | 14:6892af4291f1 | 52 | while(1) |
anoney180133 | 14:6892af4291f1 | 53 | { |
anoney180133 | 14:6892af4291f1 | 54 | ble.waitForEvent(); // this saves battery while waiting for callback events |
mbedAustin | 1:0692bee84264 | 55 | } |
mbedAustin | 1:0692bee84264 | 56 | } |
anoney180133 | 14:6892af4291f1 | 57 |