A simple demo that directly uses BLE library to define service and characteristics
Dependencies: BLE_API mbed nRF51822
Fork of BLE_HeartRate by
main.cpp@48:908b4ec086ba, 2014-10-22 (annotated)
- Committer:
- nebgnahz
- Date:
- Wed Oct 22 05:39:38 2014 +0000
- Revision:
- 48:908b4ec086ba
- Parent:
- 47:430545f41113
- Child:
- 49:14b2df099dfc
a simple demo
Who changed what in which revision?
User | Revision | Line number | New 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 | |
rgrover1 | 45:98c5a34b07a4 | 7 | const static char DEVICE_NAME[] = "Nordic_HRM"; |
nebgnahz | 48:908b4ec086ba | 8 | static const uint16_t uuid16_list[] = {0xFEDB}; |
nebgnahz | 48:908b4ec086ba | 9 | static volatile bool is_button_pressed = false; |
nebgnahz | 48:908b4ec086ba | 10 | static volatile uint16_t led1_handler; |
nebgnahz | 48:908b4ec086ba | 11 | |
nebgnahz | 48:908b4ec086ba | 12 | uint8_t led_state, button_state; |
Rohit Grover |
36:ea2a1b4f51c1 | 13 | |
rgrover1 | 41:9cef0129da5f | 14 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
ktownsend | 0:87a7fc231fae | 15 | { |
rgrover1 | 46:ee7c55907f36 | 16 | ble.startAdvertising(); // restart advertising |
rgrover1 | 7:daab8ba5139e | 17 | } |
Rohit Grover |
3:24e2b056d229 | 18 | |
nebgnahz | 48:908b4ec086ba | 19 | // eventDataP->charHandle is just uint16_t |
nebgnahz | 48:908b4ec086ba | 20 | void changeLED(const GattCharacteristicWriteCBParams *eventDataP) { |
nebgnahz | 48:908b4ec086ba | 21 | if (eventDataP->charHandle == led1_handler) { |
nebgnahz | 48:908b4ec086ba | 22 | led1 = eventDataP->data[0] % 2; |
nebgnahz | 48:908b4ec086ba | 23 | } |
nebgnahz | 48:908b4ec086ba | 24 | } |
rgrover1 | 47:430545f41113 | 25 | |
nebgnahz | 48:908b4ec086ba | 26 | void button1Pressed() { |
nebgnahz | 48:908b4ec086ba | 27 | button_state = 1; |
nebgnahz | 48:908b4ec086ba | 28 | is_button_pressed = true; |
nebgnahz | 48:908b4ec086ba | 29 | } |
nebgnahz | 48:908b4ec086ba | 30 | void button2Pressed() { |
nebgnahz | 48:908b4ec086ba | 31 | button_state = 2; |
nebgnahz | 48:908b4ec086ba | 32 | is_button_pressed = true; |
Rohit Grover |
11:1d9aafee4984 | 33 | } |
Rohit Grover |
11:1d9aafee4984 | 34 | |
ktownsend | 0:87a7fc231fae | 35 | int main(void) |
ktownsend | 0:87a7fc231fae | 36 | { |
nebgnahz | 48:908b4ec086ba | 37 | // button initialization |
nebgnahz | 48:908b4ec086ba | 38 | InterruptIn button1(BUTTON1); |
nebgnahz | 48:908b4ec086ba | 39 | InterruptIn button2(BUTTON2); |
nebgnahz | 48:908b4ec086ba | 40 | button1.mode(PullUp); |
nebgnahz | 48:908b4ec086ba | 41 | button2.mode(PullUp); |
nebgnahz | 48:908b4ec086ba | 42 | button1.rise(&button1Pressed); |
nebgnahz | 48:908b4ec086ba | 43 | button2.rise(&button2Pressed); |
nebgnahz | 48:908b4ec086ba | 44 | led1 = 0; |
ktownsend | 0:87a7fc231fae | 45 | |
nebgnahz | 48:908b4ec086ba | 46 | // just a simple service example |
nebgnahz | 48:908b4ec086ba | 47 | // o led1 characteristics, you can write from the phone to control led1 |
nebgnahz | 48:908b4ec086ba | 48 | // o button characteristics, you can read and get notified |
nebgnahz | 48:908b4ec086ba | 49 | GattCharacteristic led1_characteristics( |
nebgnahz | 48:908b4ec086ba | 50 | 0xFF00, &led_state, sizeof(led_state), sizeof(led_state), |
nebgnahz | 48:908b4ec086ba | 51 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
nebgnahz | 48:908b4ec086ba | 52 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
nebgnahz | 48:908b4ec086ba | 53 | led1_handler = led1_characteristics.getValueAttribute().getHandle(); |
rgrover1 | 45:98c5a34b07a4 | 54 | |
nebgnahz | 48:908b4ec086ba | 55 | GattCharacteristic button_characteristics( |
nebgnahz | 48:908b4ec086ba | 56 | 0xFF01, &button_state, sizeof(button_state), sizeof(button_state), |
nebgnahz | 48:908b4ec086ba | 57 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
nebgnahz | 48:908b4ec086ba | 58 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
nebgnahz | 48:908b4ec086ba | 59 | |
nebgnahz | 48:908b4ec086ba | 60 | GattCharacteristic *charTable[] = {&led1_characteristics, &button_characteristics}; |
nebgnahz | 48:908b4ec086ba | 61 | GattService testService(0xFFFF, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
nebgnahz | 48:908b4ec086ba | 62 | |
nebgnahz | 48:908b4ec086ba | 63 | // BLE setup, mainly we add service and callbacks |
nebgnahz | 48:908b4ec086ba | 64 | ble.init(); |
nebgnahz | 48:908b4ec086ba | 65 | ble.addService(testService); |
nebgnahz | 48:908b4ec086ba | 66 | ble.onDataWritten(&changeLED); |
nebgnahz | 48:908b4ec086ba | 67 | ble.onDisconnection(disconnectionCallback); |
nebgnahz | 48:908b4ec086ba | 68 | |
nebgnahz | 48:908b4ec086ba | 69 | // setup advertising |
Rohit Grover |
29:76d865c718a6 | 70 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
rgrover1 | 40:e73130c6f2bb | 71 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
Rohit Grover |
29:76d865c718a6 | 72 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
rgrover1 | 7:daab8ba5139e | 73 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
rgrover1 | 40:e73130c6f2bb | 74 | ble.setAdvertisingInterval(1600); /* 1000ms; in multiples of 0.625ms. */ |
rgrover1 | 7:daab8ba5139e | 75 | ble.startAdvertising(); |
Rohit Grover |
3:24e2b056d229 | 76 | |
Rohit Grover |
11:1d9aafee4984 | 77 | while (true) { |
nebgnahz | 48:908b4ec086ba | 78 | if (is_button_pressed) { |
nebgnahz | 48:908b4ec086ba | 79 | is_button_pressed = false; |
nebgnahz | 48:908b4ec086ba | 80 | ble.updateCharacteristicValue(button_characteristics.getValueAttribute().getHandle(), |
nebgnahz | 48:908b4ec086ba | 81 | &button_state, sizeof(button_state)); |
Rohit Grover |
36:ea2a1b4f51c1 | 82 | } else { |
Rohit Grover |
36:ea2a1b4f51c1 | 83 | ble.waitForEvent(); |
Rohit Grover |
36:ea2a1b4f51c1 | 84 | } |
ktownsend | 0:87a7fc231fae | 85 | } |
ktownsend | 0:87a7fc231fae | 86 | } |