Runlock POC code

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_GATT_Example by Bluetooth Low Energy

Committer:
dragosIQ
Date:
Thu Nov 30 13:35:21 2017 +0000
Revision:
23:9fe39f9a9eda
Parent:
22:406127954d1f
Stable version for testing POC

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 0:cd5b6733aeb1 1 #include "mbed.h"
andresag 19:477567297aac 2 #include "ble/BLE.h"
mbedAustin 1:94152e7d8b5c 3
andresag 19:477567297aac 4 DigitalOut led(LED1, 1);
dragosIQ 23:9fe39f9a9eda 5 uint16_t customServiceUUID = 0xD00D;
dragosIQ 23:9fe39f9a9eda 6 uint16_t readCharUUID = 0x2A7D;
dragosIQ 23:9fe39f9a9eda 7 uint16_t writeCharUUID = 0x2A87;
mbedAustin 1:94152e7d8b5c 8
dragosIQ 23:9fe39f9a9eda 9 const static uint8_t iBeacon[] = {
dragosIQ 23:9fe39f9a9eda 10 0x4C, 0x00,
dragosIQ 23:9fe39f9a9eda 11 0x02, 0x15,
dragosIQ 23:9fe39f9a9eda 12 0xC0, 0x01, 0xBA, 0xBE, 0xB1, 0x6B, 0x00, 0xB5, 0x5E, 0xC5, 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE,
dragosIQ 23:9fe39f9a9eda 13 0x00, 0x01, 0x00, 0x01, 0xCE
dragosIQ 23:9fe39f9a9eda 14 };
dragosIQ 23:9fe39f9a9eda 15
dragosIQ 23:9fe39f9a9eda 16
dragosIQ 23:9fe39f9a9eda 17 const static char DEVICE_NAME[] = "00010001"; // change this
dragosIQ 23:9fe39f9a9eda 18 static const uint16_t uuid16_list[] = {0xD00D}; //Custom UUID, FFFF is reserved for development
mbedAustin 1:94152e7d8b5c 19
andresag 22:406127954d1f 20 /* Set Up custom Characteristics */
dragosIQ 23:9fe39f9a9eda 21 static uint8_t readValue[20] = {0, };
mbedAustin 12:6d1f77d0cb37 22 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue);
mbedAustin 2:e84c13abc479 23
dragosIQ 23:9fe39f9a9eda 24 static uint8_t writeValue[20] = {0, };
mbedAustin 12:6d1f77d0cb37 25 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
mbedAustin 2:e84c13abc479 26
andresag 22:406127954d1f 27 /* Set up custom service */
mbedAustin 8:60ede963dfe2 28 GattCharacteristic *characteristics[] = {&readChar, &writeChar};
mbedAustin 9:b33f42191584 29 GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
mbedAustin 2:e84c13abc479 30
mbedAustin 2:e84c13abc479 31
mbedAustin 8:60ede963dfe2 32 /*
mbedAustin 8:60ede963dfe2 33 * Restart advertising when phone app disconnects
andresag 19:477567297aac 34 */
andresag 19:477567297aac 35 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
mbedAustin 1:94152e7d8b5c 36 {
andresag 22:406127954d1f 37 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
mbedAustin 1:94152e7d8b5c 38 }
mbedAustin 0:cd5b6733aeb1 39
andresag 19:477567297aac 40 /*
andresag 22:406127954d1f 41 * Handle writes to writeCharacteristic
mbedAustin 8:60ede963dfe2 42 */
melmon 18:7d89c4fba362 43 void writeCharCallback(const GattWriteCallbackParams *params)
mbedAustin 3:0fb60f81f693 44 {
dragosIQ 23:9fe39f9a9eda 45 led=true;
dragosIQ 23:9fe39f9a9eda 46 printf("led off\n\r");
dragosIQ 23:9fe39f9a9eda 47 /* Check to see what characteristic was written, by handle */
melmon 18:7d89c4fba362 48 if(params->handle == writeChar.getValueHandle()) {
dragosIQ 23:9fe39f9a9eda 49 /* toggle LED if only if dragos.stoica@qualitance.com#1234#open or #close */
dragosIQ 23:9fe39f9a9eda 50 if(params->len == 4) {
dragosIQ 23:9fe39f9a9eda 51 led=false;
dragosIQ 23:9fe39f9a9eda 52 printf("led on\n\r"); // print led toggle
mbedAustin 8:60ede963dfe2 53 }
dragosIQ 23:9fe39f9a9eda 54
andresag 19:477567297aac 55 printf("Data received: length = %d, data = 0x",params->len);
mbedAustin 8:60ede963dfe2 56 for(int x=0; x < params->len; x++) {
andresag 19:477567297aac 57 printf("%x", params->data[x]);
mbedAustin 8:60ede963dfe2 58 }
andresag 19:477567297aac 59 printf("\n\r");
dragosIQ 23:9fe39f9a9eda 60
andresag 22:406127954d1f 61 /* Update the readChar with the value of writeChar */
andresag 22:406127954d1f 62 BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len);
mbedAustin 8:60ede963dfe2 63 }
mbedAustin 3:0fb60f81f693 64 }
andresag 22:406127954d1f 65 /*
andresag 22:406127954d1f 66 * Initialization callback
andresag 22:406127954d1f 67 */
andresag 22:406127954d1f 68 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 22:406127954d1f 69 {
andresag 22:406127954d1f 70 BLE &ble = params->ble;
andresag 22:406127954d1f 71 ble_error_t error = params->error;
andresag 22:406127954d1f 72
andresag 22:406127954d1f 73 if (error != BLE_ERROR_NONE) {
andresag 22:406127954d1f 74 return;
andresag 22:406127954d1f 75 }
mbedAustin 0:cd5b6733aeb1 76
andresag 19:477567297aac 77 ble.gap().onDisconnection(disconnectionCallback);
andresag 19:477567297aac 78 ble.gattServer().onDataWritten(writeCharCallback);
mbedAustin 2:e84c13abc479 79
andresag 22:406127954d1f 80 /* Setup advertising */
andresag 19:477567297aac 81 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
andresag 19:477567297aac 82 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
dragosIQ 23:9fe39f9a9eda 83 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, iBeacon, sizeof(iBeacon));
dragosIQ 23:9fe39f9a9eda 84 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
dragosIQ 23:9fe39f9a9eda 85 ble.gap().accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
dragosIQ 23:9fe39f9a9eda 86 ble.gap().accumulateScanResponse(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
andresag 19:477567297aac 87 ble.gap().setAdvertisingInterval(100); // 100ms.
mbedAustin 2:e84c13abc479 88
andresag 22:406127954d1f 89 /* Add our custom service */
mbedAustin 13:62b1d32745ac 90 ble.addService(customService);
mbedAustin 2:e84c13abc479 91
andresag 22:406127954d1f 92 /* Start advertising */
andresag 19:477567297aac 93 ble.gap().startAdvertising();
andresag 22:406127954d1f 94 }
andresag 19:477567297aac 95
andresag 22:406127954d1f 96 /*
andresag 22:406127954d1f 97 * Main loop
andresag 22:406127954d1f 98 */
andresag 22:406127954d1f 99 int main(void)
andresag 22:406127954d1f 100 {
andresag 22:406127954d1f 101 /* initialize stuff */
andresag 22:406127954d1f 102 printf("\n\r********* Starting Main Loop *********\n\r");
andresag 22:406127954d1f 103
andresag 22:406127954d1f 104 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
andresag 22:406127954d1f 105 ble.init(bleInitComplete);
andresag 22:406127954d1f 106
andresag 22:406127954d1f 107 /* SpinWait for initialization to complete. This is necessary because the
andresag 22:406127954d1f 108 * BLE object is used in the main loop below. */
andresag 22:406127954d1f 109 while (ble.hasInitialized() == false) { /* spin loop */ }
andresag 22:406127954d1f 110
andresag 22:406127954d1f 111 /* Infinite loop waiting for BLE interrupt events */
mbedAustin 2:e84c13abc479 112 while (true) {
andresag 22:406127954d1f 113 ble.waitForEvent(); /* Save power */
mbedAustin 2:e84c13abc479 114 }
andresag 20:fcc752d401ec 115 }