This is a demonstration of how to create a GATT service and characteristic.

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_EvothingsExample_GATT by Austin Blackstone

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ble/BLE.h"
00003 
00004 DigitalOut led(LED1, 1);
00005 uint16_t customServiceUUID  = 0xA000;
00006 uint16_t readCharUUID       = 0xA001;
00007 uint16_t writeCharUUID      = 0xA002;
00008 
00009 const static char     DEVICE_NAME[]        = "ChangeMe!!"; // change this
00010 static const uint16_t uuid16_list[]        = {0xFFFF}; //Custom UUID, FFFF is reserved for development
00011 
00012 /* Set Up custom Characteristics */
00013 static uint8_t readValue[10] = {0};
00014 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue);
00015 
00016 static uint8_t writeValue[10] = {0};
00017 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
00018 
00019 /* Set up custom service */
00020 GattCharacteristic *characteristics[] = {&readChar, &writeChar};
00021 GattService        customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
00022 
00023 
00024 /*
00025  *  Restart advertising when phone app disconnects
00026 */
00027 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
00028 {
00029     BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
00030 }
00031 
00032 /*
00033  *  Handle writes to writeCharacteristic
00034 */
00035 void writeCharCallback(const GattWriteCallbackParams *params)
00036 {
00037     /* Check to see what characteristic was written, by handle */
00038     if(params->handle == writeChar.getValueHandle()) {
00039         /* toggle LED if only 1 byte is written */
00040         if(params->len == 1) {
00041             led = params->data[0];
00042             (params->data[0] == 0x00) ? printf("led on\n\r") : printf("led off\n\r"); // print led toggle
00043         }
00044         /* Print the data if more than 1 byte is written */
00045         else {
00046             printf("Data received: length = %d, data = 0x",params->len);
00047             for(int x=0; x < params->len; x++) {
00048                 printf("%x", params->data[x]);
00049             }
00050             printf("\n\r");
00051         }
00052         /* Update the readChar with the value of writeChar */
00053         BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len);
00054     }
00055 }
00056 /*
00057  * Initialization callback
00058  */
00059 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00060 {
00061     BLE &ble          = params->ble;
00062     ble_error_t error = params->error;
00063     
00064     if (error != BLE_ERROR_NONE) {
00065         return;
00066     }
00067 
00068     ble.gap().onDisconnection(disconnectionCallback);
00069     ble.gattServer().onDataWritten(writeCharCallback);
00070 
00071     /* Setup advertising */
00072     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
00073     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
00074     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
00075     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
00076     ble.gap().setAdvertisingInterval(100); // 100ms.
00077 
00078     /* Add our custom service */
00079     ble.addService(customService);
00080 
00081     /* Start advertising */
00082     ble.gap().startAdvertising();
00083 }
00084 
00085 /*
00086  *  Main loop
00087 */
00088 int main(void)
00089 {
00090     /* initialize stuff */
00091     printf("\n\r********* Starting Main Loop *********\n\r");
00092     
00093     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
00094     ble.init(bleInitComplete);
00095     
00096     /* SpinWait for initialization to complete. This is necessary because the
00097      * BLE object is used in the main loop below. */
00098     while (ble.hasInitialized()  == false) { /* spin loop */ }
00099 
00100     /* Infinite loop waiting for BLE interrupt events */
00101     while (true) {
00102         ble.waitForEvent(); /* Save power */
00103     }
00104 }