Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
Fork of BLE_GATT_Example by
main.cpp.orig@20:fcc752d401ec, 2015-10-20 (annotated)
- Committer:
- andresag
- Date:
- Tue Oct 20 13:46:23 2015 +0000
- Revision:
- 20:fcc752d401ec
Merged updated version of main.cpp with upstream.
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| andresag | 20:fcc752d401ec | 1 | #include "mbed.h" | 
| andresag | 20:fcc752d401ec | 2 | #include "ble/BLE.h" | 
| andresag | 20:fcc752d401ec | 3 | |
| andresag | 20:fcc752d401ec | 4 | BLE ble; | 
| andresag | 20:fcc752d401ec | 5 | DigitalOut led(LED1, 1); | 
| andresag | 20:fcc752d401ec | 6 | uint16_t customServiceUUID = 0xA000; | 
| andresag | 20:fcc752d401ec | 7 | uint16_t readCharUUID = 0xA001; | 
| andresag | 20:fcc752d401ec | 8 | uint16_t writeCharUUID = 0xA002; | 
| andresag | 20:fcc752d401ec | 9 | |
| andresag | 20:fcc752d401ec | 10 | const static char DEVICE_NAME[] = "ChangeMe!!"; // change this | 
| andresag | 20:fcc752d401ec | 11 | static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development | 
| andresag | 20:fcc752d401ec | 12 | |
| andresag | 20:fcc752d401ec | 13 | // Set Up custom Characteristics | 
| andresag | 20:fcc752d401ec | 14 | static uint8_t readValue[10] = {0}; | 
| andresag | 20:fcc752d401ec | 15 | ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue); | 
| andresag | 20:fcc752d401ec | 16 | |
| andresag | 20:fcc752d401ec | 17 | static uint8_t writeValue[10] = {0}; | 
| andresag | 20:fcc752d401ec | 18 | WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); | 
| andresag | 20:fcc752d401ec | 19 | |
| andresag | 20:fcc752d401ec | 20 | // Set up custom service | 
| andresag | 20:fcc752d401ec | 21 | GattCharacteristic *characteristics[] = {&readChar, &writeChar}; | 
| andresag | 20:fcc752d401ec | 22 | GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); | 
| andresag | 20:fcc752d401ec | 23 | |
| andresag | 20:fcc752d401ec | 24 | |
| andresag | 20:fcc752d401ec | 25 | /* | 
| andresag | 20:fcc752d401ec | 26 | * Restart advertising when phone app disconnects | 
| andresag | 20:fcc752d401ec | 27 | */ | 
| andresag | 20:fcc752d401ec | 28 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) | 
| andresag | 20:fcc752d401ec | 29 | { | 
| andresag | 20:fcc752d401ec | 30 | ble.gap().startAdvertising(); | 
| andresag | 20:fcc752d401ec | 31 | } | 
| andresag | 20:fcc752d401ec | 32 | |
| andresag | 20:fcc752d401ec | 33 | /* | 
| andresag | 20:fcc752d401ec | 34 | * handle writes to writeCharacteristic | 
| andresag | 20:fcc752d401ec | 35 | */ | 
| andresag | 20:fcc752d401ec | 36 | void writeCharCallback(const GattWriteCallbackParams *params) | 
| andresag | 20:fcc752d401ec | 37 | { | 
| andresag | 20:fcc752d401ec | 38 | // check to see what characteristic was written, by handle | 
| andresag | 20:fcc752d401ec | 39 | if(params->handle == writeChar.getValueHandle()) { | 
| andresag | 20:fcc752d401ec | 40 | // toggle LED if only 1 byte is written | 
| andresag | 20:fcc752d401ec | 41 | if(params->len == 1) { | 
| andresag | 20:fcc752d401ec | 42 | led = params->data[0]; | 
| andresag | 20:fcc752d401ec | 43 | (params->data[0] == 0x00) ? printf("led on\n\r") : printf("led off\n\r"); // print led toggle | 
| andresag | 20:fcc752d401ec | 44 | } | 
| andresag | 20:fcc752d401ec | 45 | // print the data if more than 1 byte is written | 
| andresag | 20:fcc752d401ec | 46 | else { | 
| andresag | 20:fcc752d401ec | 47 | printf("Data received: length = %d, data = 0x",params->len); | 
| andresag | 20:fcc752d401ec | 48 | for(int x=0; x < params->len; x++) { | 
| andresag | 20:fcc752d401ec | 49 | printf("%x", params->data[x]); | 
| andresag | 20:fcc752d401ec | 50 | } | 
| andresag | 20:fcc752d401ec | 51 | printf("\n\r"); | 
| andresag | 20:fcc752d401ec | 52 | } | 
| andresag | 20:fcc752d401ec | 53 | // update the readChar with the value of writeChar | 
| andresag | 20:fcc752d401ec | 54 | ble.updateCharacteristicValue(readChar.getValueHandle(), params->data,params->len); | 
| andresag | 20:fcc752d401ec | 55 | } | 
| andresag | 20:fcc752d401ec | 56 | } | 
| andresag | 20:fcc752d401ec | 57 | |
| andresag | 20:fcc752d401ec | 58 | /* | 
| andresag | 20:fcc752d401ec | 59 | * main loop | 
| andresag | 20:fcc752d401ec | 60 | */ | 
| andresag | 20:fcc752d401ec | 61 | int | 
| andresag | 20:fcc752d401ec | 62 | main(void) | 
| andresag | 20:fcc752d401ec | 63 | { | 
| andresag | 20:fcc752d401ec | 64 | /* initialize stuff */ | 
| andresag | 20:fcc752d401ec | 65 | printf("\n\r********* Starting Main Loop *********\n\r"); | 
| andresag | 20:fcc752d401ec | 66 | ble.init(); | 
| andresag | 20:fcc752d401ec | 67 | ble.gap().onDisconnection(disconnectionCallback); | 
| andresag | 20:fcc752d401ec | 68 | ble.gattServer().onDataWritten(writeCharCallback); | 
| andresag | 20:fcc752d401ec | 69 | |
| andresag | 20:fcc752d401ec | 70 | /* setup advertising */ | 
| andresag | 20:fcc752d401ec | 71 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT | 
| andresag | 20:fcc752d401ec | 72 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type | 
| andresag | 20:fcc752d401ec | 73 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name | 
| andresag | 20:fcc752d401ec | 74 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet | 
| andresag | 20:fcc752d401ec | 75 | ble.gap().setAdvertisingInterval(100); // 100ms. | 
| andresag | 20:fcc752d401ec | 76 | |
| andresag | 20:fcc752d401ec | 77 | // add our custom service | 
| andresag | 20:fcc752d401ec | 78 | ble.addService(customService); | 
| andresag | 20:fcc752d401ec | 79 | |
| andresag | 20:fcc752d401ec | 80 | // start advertising | 
| andresag | 20:fcc752d401ec | 81 | ble.gap().startAdvertising(); | 
| andresag | 20:fcc752d401ec | 82 | |
| andresag | 20:fcc752d401ec | 83 | // infinite loop waiting for BLE interrupt events | 
| andresag | 20:fcc752d401ec | 84 | while (true) { | 
| andresag | 20:fcc752d401ec | 85 | ble.waitForEvent(); //Save power | 
| andresag | 20:fcc752d401ec | 86 | } | 
| andresag | 20:fcc752d401ec | 87 | } | 
