Nora Vazbyte / Mbed 2 deprecated BLE_GATT_Chill

Dependencies:   mbed BLE_API nRF51822

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[]        = "OCCUPY-CRICHTON-ST"; // 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  *  Restart advertising when phone app disconnects
00025 */
00026 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
00027 {
00028     BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
00029 }
00030 
00031 void writeCharCallback(const GattWriteCallbackParams *params)
00032 {
00033     /* Check to see what characteristic was written, by handle */
00034     if(params->handle == writeChar.getValueHandle()) {
00035         /* toggle LED if only 1 byte is written */
00036         if(params->len == 1) {
00037             led = params->data[0];
00038             (params->data[0] == 0x00) ? printf("led on\n\r") : printf("led off\n\r"); // print led toggle
00039         }
00040         /* Print the data if more than 1 byte is written */
00041         else {
00042             printf("Data received: length = %d, data = 0x",params->len);
00043             for(int x=0; x < params->len; x++) {
00044                 printf("%x", params->data[x]);
00045             }
00046             printf("\n\r");
00047         }
00048         /* Update the readChar with the value of writeChar */
00049         BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len);
00050     }
00051 }
00052 /*
00053  * Initialization callback
00054  */
00055 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00056 {
00057     BLE &ble          = params->ble;
00058     ble_error_t error = params->error;
00059     
00060     if (error != BLE_ERROR_NONE) {
00061         return;
00062     }
00063 
00064     ble.gap().onDisconnection(disconnectionCallback);
00065     ble.gattServer().onDataWritten(writeCharCallback);
00066 
00067     /* Setup advertising */
00068     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
00069     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
00070     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
00071     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
00072     ble.gap().setAdvertisingInterval(100); // 100ms.
00073 
00074     /* Add our custom service */
00075     ble.addService(customService);
00076 
00077     /* Start advertising */
00078     ble.gap().startAdvertising();
00079 }
00080 void wakeup_event_cb() {
00081     led != led;
00082 }
00083 
00084 int main(void)
00085 {
00086     /* initialize stuff */
00087     printf("\n\r********* Starting Main Loop *********\n\r");
00088     
00089     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
00090     ble.init(bleInitComplete);
00091     
00092     Ticker ticker;
00093     int counter = 0;
00094     ticker.attach(wakeup_event_cb, 0.3);
00095     while (ble.hasInitialized()) {
00096         counter++;
00097         printf("hello %i!\n", counter);
00098         ble.waitForEvent(); /* Save power */
00099     }
00100 }