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@27:ed42554cf870, 2017-06-10 (annotated)
- Committer:
- andreydung
- Date:
- Sat Jun 10 01:03:26 2017 +0000
- Revision:
- 27:ed42554cf870
- Parent:
- 26:b3e90d7672f8
- Child:
- 28:382c630bae02
it works!!
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbedAustin | 0:cd5b6733aeb1 | 1 | #include "mbed.h" |
| andresag | 19:477567297aac | 2 | #include "ble/BLE.h" |
| mbedAustin | 1:94152e7d8b5c | 3 | |
| andreydung | 27:ed42554cf870 | 4 | DigitalOut led(P0_19, 1); |
| mbedAustin | 9:b33f42191584 | 5 | uint16_t customServiceUUID = 0xA000; |
| mbedAustin | 13:62b1d32745ac | 6 | uint16_t readCharUUID = 0xA001; |
| mbedAustin | 9:b33f42191584 | 7 | uint16_t writeCharUUID = 0xA002; |
| mbedAustin | 1:94152e7d8b5c | 8 | |
| andreydung | 27:ed42554cf870 | 9 | const static char DEVICE_NAME[] = "ChangeMe!!"; // change this |
| andreydung | 27:ed42554cf870 | 10 | static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development |
| mbedAustin | 1:94152e7d8b5c | 11 | |
| andresag | 22:406127954d1f | 12 | /* Set Up custom Characteristics */ |
| mbedAustin | 9:b33f42191584 | 13 | static uint8_t readValue[10] = {0}; |
| mbedAustin | 12:6d1f77d0cb37 | 14 | ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue); |
| mbedAustin | 2:e84c13abc479 | 15 | |
| mbedAustin | 9:b33f42191584 | 16 | static uint8_t writeValue[10] = {0}; |
| mbedAustin | 12:6d1f77d0cb37 | 17 | WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); |
| mbedAustin | 2:e84c13abc479 | 18 | |
| andresag | 22:406127954d1f | 19 | /* Set up custom service */ |
| mbedAustin | 8:60ede963dfe2 | 20 | GattCharacteristic *characteristics[] = {&readChar, &writeChar}; |
| mbedAustin | 9:b33f42191584 | 21 | GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
| mbedAustin | 2:e84c13abc479 | 22 | |
| andreydung | 27:ed42554cf870 | 23 | |
| andreydung | 27:ed42554cf870 | 24 | /* |
| andreydung | 27:ed42554cf870 | 25 | * Restart advertising when phone app disconnects |
| andreydung | 27:ed42554cf870 | 26 | */ |
| andresag | 19:477567297aac | 27 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) |
| mbedAustin | 1:94152e7d8b5c | 28 | { |
| andreydung | 27:ed42554cf870 | 29 | led = 1; // just in case |
| andresag | 22:406127954d1f | 30 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); |
| mbedAustin | 1:94152e7d8b5c | 31 | } |
| mbedAustin | 0:cd5b6733aeb1 | 32 | |
| andreydung | 27:ed42554cf870 | 33 | /* |
| andreydung | 27:ed42554cf870 | 34 | * Handle writes to writeCharacteristic |
| andreydung | 27:ed42554cf870 | 35 | */ |
| melmon | 18:7d89c4fba362 | 36 | void writeCharCallback(const GattWriteCallbackParams *params) |
| mbedAustin | 3:0fb60f81f693 | 37 | { |
| andresag | 22:406127954d1f | 38 | /* Check to see what characteristic was written, by handle */ |
| melmon | 18:7d89c4fba362 | 39 | if(params->handle == writeChar.getValueHandle()) { |
| andreydung | 27:ed42554cf870 | 40 | // since LED is pullup mode |
| andreydung | 27:ed42554cf870 | 41 | led = 0; |
| andreydung | 27:ed42554cf870 | 42 | wait(1.0); |
| andreydung | 27:ed42554cf870 | 43 | led = 1; |
| mbedAustin | 8:60ede963dfe2 | 44 | } |
| mbedAustin | 3:0fb60f81f693 | 45 | } |
| andresag | 22:406127954d1f | 46 | /* |
| andresag | 22:406127954d1f | 47 | * Initialization callback |
| andresag | 22:406127954d1f | 48 | */ |
| andresag | 22:406127954d1f | 49 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
| andresag | 22:406127954d1f | 50 | { |
| andresag | 22:406127954d1f | 51 | BLE &ble = params->ble; |
| andresag | 22:406127954d1f | 52 | ble_error_t error = params->error; |
| andresag | 22:406127954d1f | 53 | |
| andresag | 22:406127954d1f | 54 | if (error != BLE_ERROR_NONE) { |
| andresag | 22:406127954d1f | 55 | return; |
| andresag | 22:406127954d1f | 56 | } |
| mbedAustin | 0:cd5b6733aeb1 | 57 | |
| andresag | 19:477567297aac | 58 | ble.gap().onDisconnection(disconnectionCallback); |
| andresag | 19:477567297aac | 59 | ble.gattServer().onDataWritten(writeCharCallback); |
| mbedAustin | 2:e84c13abc479 | 60 | |
| andresag | 22:406127954d1f | 61 | /* Setup advertising */ |
| andresag | 19:477567297aac | 62 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT |
| andresag | 19:477567297aac | 63 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type |
| andresag | 19:477567297aac | 64 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name |
| andresag | 19:477567297aac | 65 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet |
| andresag | 19:477567297aac | 66 | ble.gap().setAdvertisingInterval(100); // 100ms. |
| mbedAustin | 2:e84c13abc479 | 67 | |
| andresag | 22:406127954d1f | 68 | /* Add our custom service */ |
| mbedAustin | 13:62b1d32745ac | 69 | ble.addService(customService); |
| mbedAustin | 2:e84c13abc479 | 70 | |
| andresag | 22:406127954d1f | 71 | /* Start advertising */ |
| andresag | 19:477567297aac | 72 | ble.gap().startAdvertising(); |
| andresag | 22:406127954d1f | 73 | } |
| andresag | 19:477567297aac | 74 | |
| andresag | 22:406127954d1f | 75 | /* |
| andresag | 22:406127954d1f | 76 | * Main loop |
| andresag | 22:406127954d1f | 77 | */ |
| andresag | 22:406127954d1f | 78 | int main(void) |
| andresag | 22:406127954d1f | 79 | { |
| andresag | 22:406127954d1f | 80 | /* initialize stuff */ |
| andresag | 22:406127954d1f | 81 | printf("\n\r********* Starting Main Loop *********\n\r"); |
| andresag | 22:406127954d1f | 82 | |
| andresag | 22:406127954d1f | 83 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
| andresag | 22:406127954d1f | 84 | ble.init(bleInitComplete); |
| andresag | 22:406127954d1f | 85 | |
| andresag | 22:406127954d1f | 86 | /* SpinWait for initialization to complete. This is necessary because the |
| andresag | 22:406127954d1f | 87 | * BLE object is used in the main loop below. */ |
| andresag | 22:406127954d1f | 88 | while (ble.hasInitialized() == false) { /* spin loop */ } |
| andresag | 22:406127954d1f | 89 | |
| andresag | 22:406127954d1f | 90 | /* Infinite loop waiting for BLE interrupt events */ |
| mbedAustin | 2:e84c13abc479 | 91 | while (true) { |
| andresag | 22:406127954d1f | 92 | ble.waitForEvent(); /* Save power */ |
| mbedAustin | 2:e84c13abc479 | 93 | } |
| andresag | 20:fcc752d401ec | 94 | } |
