A simple demo that directly uses BLE library to define service and characteristics

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_HeartRate by Bluetooth Low Energy

Committer:
nebgnahz
Date:
Wed Oct 22 05:47:25 2014 +0000
Revision:
49:14b2df099dfc
Parent:
48:908b4ec086ba
Child:
50:4c02add9abba
style fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 0:87a7fc231fae 1 #include "mbed.h"
Rohit Grover 10:2436164b692e 2 #include "BLEDevice.h"
ktownsend 0:87a7fc231fae 3
Rohit Grover 10:2436164b692e 4 BLEDevice ble;
rgrover1 47:430545f41113 5 DigitalOut led1(LED1);
ktownsend 0:87a7fc231fae 6
nebgnahz 49:14b2df099dfc 7 const static char DEVICE_NAME[] = "Nordic";
nebgnahz 49:14b2df099dfc 8 // we only broadcast a single service
nebgnahz 49:14b2df099dfc 9 static const uint16_t uuid16_list[] = {0xFFFF};
nebgnahz 48:908b4ec086ba 10 static volatile bool is_button_pressed = false;
nebgnahz 48:908b4ec086ba 11 static volatile uint16_t led1_handler;
nebgnahz 48:908b4ec086ba 12
nebgnahz 48:908b4ec086ba 13 uint8_t led_state, button_state;
Rohit Grover 36:ea2a1b4f51c1 14
rgrover1 41:9cef0129da5f 15 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
ktownsend 0:87a7fc231fae 16 {
rgrover1 46:ee7c55907f36 17 ble.startAdvertising(); // restart advertising
rgrover1 7:daab8ba5139e 18 }
Rohit Grover 3:24e2b056d229 19
nebgnahz 48:908b4ec086ba 20 void changeLED(const GattCharacteristicWriteCBParams *eventDataP) {
nebgnahz 49:14b2df099dfc 21 // eventDataP->charHandle is just uint16_t
nebgnahz 49:14b2df099dfc 22 // it's used to dispatch the callbacks
nebgnahz 48:908b4ec086ba 23 if (eventDataP->charHandle == led1_handler) {
nebgnahz 48:908b4ec086ba 24 led1 = eventDataP->data[0] % 2;
nebgnahz 48:908b4ec086ba 25 }
nebgnahz 48:908b4ec086ba 26 }
rgrover1 47:430545f41113 27
nebgnahz 48:908b4ec086ba 28 void button1Pressed() {
nebgnahz 48:908b4ec086ba 29 button_state = 1;
nebgnahz 48:908b4ec086ba 30 is_button_pressed = true;
nebgnahz 48:908b4ec086ba 31 }
nebgnahz 48:908b4ec086ba 32 void button2Pressed() {
nebgnahz 48:908b4ec086ba 33 button_state = 2;
nebgnahz 48:908b4ec086ba 34 is_button_pressed = true;
Rohit Grover 11:1d9aafee4984 35 }
Rohit Grover 11:1d9aafee4984 36
ktownsend 0:87a7fc231fae 37 int main(void)
ktownsend 0:87a7fc231fae 38 {
nebgnahz 48:908b4ec086ba 39 // button initialization
nebgnahz 48:908b4ec086ba 40 InterruptIn button1(BUTTON1);
nebgnahz 48:908b4ec086ba 41 InterruptIn button2(BUTTON2);
nebgnahz 48:908b4ec086ba 42 button1.mode(PullUp);
nebgnahz 48:908b4ec086ba 43 button2.mode(PullUp);
nebgnahz 48:908b4ec086ba 44 button1.rise(&button1Pressed);
nebgnahz 48:908b4ec086ba 45 button2.rise(&button2Pressed);
nebgnahz 48:908b4ec086ba 46 led1 = 0;
ktownsend 0:87a7fc231fae 47
nebgnahz 48:908b4ec086ba 48 // just a simple service example
nebgnahz 48:908b4ec086ba 49 // o led1 characteristics, you can write from the phone to control led1
nebgnahz 48:908b4ec086ba 50 // o button characteristics, you can read and get notified
nebgnahz 48:908b4ec086ba 51 GattCharacteristic led1_characteristics(
nebgnahz 48:908b4ec086ba 52 0xFF00, &led_state, sizeof(led_state), sizeof(led_state),
nebgnahz 48:908b4ec086ba 53 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ |
nebgnahz 48:908b4ec086ba 54 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
nebgnahz 48:908b4ec086ba 55 led1_handler = led1_characteristics.getValueAttribute().getHandle();
rgrover1 45:98c5a34b07a4 56
nebgnahz 48:908b4ec086ba 57 GattCharacteristic button_characteristics(
nebgnahz 48:908b4ec086ba 58 0xFF01, &button_state, sizeof(button_state), sizeof(button_state),
nebgnahz 48:908b4ec086ba 59 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ |
nebgnahz 48:908b4ec086ba 60 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
nebgnahz 48:908b4ec086ba 61
nebgnahz 48:908b4ec086ba 62 GattCharacteristic *charTable[] = {&led1_characteristics, &button_characteristics};
nebgnahz 49:14b2df099dfc 63 GattService testService(0xFFFF, charTable,
nebgnahz 49:14b2df099dfc 64 sizeof(charTable) / sizeof(GattCharacteristic *));
nebgnahz 48:908b4ec086ba 65
nebgnahz 48:908b4ec086ba 66 // BLE setup, mainly we add service and callbacks
nebgnahz 48:908b4ec086ba 67 ble.init();
nebgnahz 48:908b4ec086ba 68 ble.addService(testService);
nebgnahz 48:908b4ec086ba 69 ble.onDataWritten(&changeLED);
nebgnahz 48:908b4ec086ba 70 ble.onDisconnection(disconnectionCallback);
nebgnahz 48:908b4ec086ba 71
nebgnahz 48:908b4ec086ba 72 // setup advertising
nebgnahz 49:14b2df099dfc 73 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED |
nebgnahz 49:14b2df099dfc 74 GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
nebgnahz 49:14b2df099dfc 75 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
nebgnahz 49:14b2df099dfc 76 (uint8_t *)uuid16_list, sizeof(uuid16_list));
nebgnahz 49:14b2df099dfc 77 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
nebgnahz 49:14b2df099dfc 78 (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 7:daab8ba5139e 79 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 40:e73130c6f2bb 80 ble.setAdvertisingInterval(1600); /* 1000ms; in multiples of 0.625ms. */
rgrover1 7:daab8ba5139e 81 ble.startAdvertising();
Rohit Grover 3:24e2b056d229 82
Rohit Grover 11:1d9aafee4984 83 while (true) {
nebgnahz 48:908b4ec086ba 84 if (is_button_pressed) {
nebgnahz 49:14b2df099dfc 85 // if button pressed, we update the characteristics
nebgnahz 48:908b4ec086ba 86 is_button_pressed = false;
nebgnahz 48:908b4ec086ba 87 ble.updateCharacteristicValue(button_characteristics.getValueAttribute().getHandle(),
nebgnahz 48:908b4ec086ba 88 &button_state, sizeof(button_state));
Rohit Grover 36:ea2a1b4f51c1 89 } else {
Rohit Grover 36:ea2a1b4f51c1 90 ble.waitForEvent();
Rohit Grover 36:ea2a1b4f51c1 91 }
ktownsend 0:87a7fc231fae 92 }
ktownsend 0:87a7fc231fae 93 }