
A simple demo that directly uses BLE library to define service and characteristics
Dependencies: BLE_API mbed nRF51822
Fork of BLE_HeartRate by
Revision 49:14b2df099dfc, committed 2014-10-22
- Comitter:
- nebgnahz
- Date:
- Wed Oct 22 05:47:25 2014 +0000
- Parent:
- 48:908b4ec086ba
- Child:
- 50:4c02add9abba
- Commit message:
- style fix
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Oct 22 05:39:38 2014 +0000 +++ b/main.cpp Wed Oct 22 05:47:25 2014 +0000 @@ -4,8 +4,9 @@ BLEDevice ble; DigitalOut led1(LED1); -const static char DEVICE_NAME[] = "Nordic_HRM"; -static const uint16_t uuid16_list[] = {0xFEDB}; +const static char DEVICE_NAME[] = "Nordic"; +// we only broadcast a single service +static const uint16_t uuid16_list[] = {0xFFFF}; static volatile bool is_button_pressed = false; static volatile uint16_t led1_handler; @@ -16,8 +17,9 @@ ble.startAdvertising(); // restart advertising } -// eventDataP->charHandle is just uint16_t void changeLED(const GattCharacteristicWriteCBParams *eventDataP) { + // eventDataP->charHandle is just uint16_t + // it's used to dispatch the callbacks if (eventDataP->charHandle == led1_handler) { led1 = eventDataP->data[0] % 2; } @@ -58,7 +60,8 @@ GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); GattCharacteristic *charTable[] = {&led1_characteristics, &button_characteristics}; - GattService testService(0xFFFF, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); + GattService testService(0xFFFF, charTable, + sizeof(charTable) / sizeof(GattCharacteristic *)); // BLE setup, mainly we add service and callbacks ble.init(); @@ -67,15 +70,19 @@ ble.onDisconnection(disconnectionCallback); // setup advertising - ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); - ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); - ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); + ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | + GapAdvertisingData::LE_GENERAL_DISCOVERABLE); + ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, + (uint8_t *)uuid16_list, sizeof(uuid16_list)); + ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, + (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.setAdvertisingInterval(1600); /* 1000ms; in multiples of 0.625ms. */ ble.startAdvertising(); while (true) { if (is_button_pressed) { + // if button pressed, we update the characteristics is_button_pressed = false; ble.updateCharacteristicValue(button_characteristics.getValueAttribute().getHandle(), &button_state, sizeof(button_state));