Minimal usage example of BLE API

Dependencies:   BLE_API mbed nRF51822

Just a very simple and minimal BluetoothLE API 'Hello World' example.

Committer:
waynek
Date:
Mon Jan 18 11:13:31 2016 +0000
Revision:
2:4f0d8bf09690
Parent:
1:b84d6e0b404e
updated to confirm to the latest BLE API

Who changed what in which revision?

UserRevisionLine numberNew contents of line
waynek 0:a89b73f690c1 1 #include "mbed.h"
waynek 2:4f0d8bf09690 2 #include "ble/BLE.h"
waynek 0:a89b73f690c1 3
waynek 2:4f0d8bf09690 4 // DFUService is already included & automatically advertised by the mbed lib dependancies
waynek 0:a89b73f690c1 5
waynek 0:a89b73f690c1 6 const static char DEVICE_NAME[] = "HelloBlue";
waynek 0:a89b73f690c1 7
waynek 0:a89b73f690c1 8 BLEDevice ble;
waynek 0:a89b73f690c1 9
waynek 2:4f0d8bf09690 10 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
waynek 2:4f0d8bf09690 11 {
waynek 2:4f0d8bf09690 12 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
waynek 2:4f0d8bf09690 13 }
waynek 2:4f0d8bf09690 14
waynek 2:4f0d8bf09690 15 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
waynek 0:a89b73f690c1 16 {
waynek 2:4f0d8bf09690 17 BLE &ble = params->ble;
waynek 2:4f0d8bf09690 18 ble_error_t error = params->error;
waynek 2:4f0d8bf09690 19
waynek 2:4f0d8bf09690 20 if (error != BLE_ERROR_NONE) {
waynek 2:4f0d8bf09690 21 return;
waynek 2:4f0d8bf09690 22 }
waynek 2:4f0d8bf09690 23
waynek 2:4f0d8bf09690 24 ble.gap().onDisconnection(disconnectionCallback);
waynek 2:4f0d8bf09690 25
waynek 2:4f0d8bf09690 26 /* Setup advertising. */
waynek 2:4f0d8bf09690 27 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
waynek 2:4f0d8bf09690 28 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
waynek 2:4f0d8bf09690 29 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
waynek 2:4f0d8bf09690 30 ble.gap().setAdvertisingInterval(1000); /* ms */
waynek 2:4f0d8bf09690 31 ble.gap().startAdvertising();
waynek 0:a89b73f690c1 32 }
waynek 0:a89b73f690c1 33
waynek 2:4f0d8bf09690 34
waynek 0:a89b73f690c1 35 int main(void)
waynek 0:a89b73f690c1 36 {
waynek 2:4f0d8bf09690 37 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
waynek 2:4f0d8bf09690 38 ble.init(bleInitComplete);
waynek 0:a89b73f690c1 39
waynek 2:4f0d8bf09690 40 /* SpinWait for initialization to complete. This is necessary because the
waynek 2:4f0d8bf09690 41 * BLE object is used in the main loop below. */
waynek 2:4f0d8bf09690 42 while (ble.hasInitialized() == false) { /* spin loop */ }
waynek 2:4f0d8bf09690 43
waynek 2:4f0d8bf09690 44 while (1)
waynek 2:4f0d8bf09690 45 {
waynek 2:4f0d8bf09690 46 // to see if a Central device is currently connected you can call: ble.getGapState().connected
waynek 2:4f0d8bf09690 47 ble.waitForEvent(); // low power wait for event
waynek 0:a89b73f690c1 48 }
waynek 0:a89b73f690c1 49 }
waynek 0:a89b73f690c1 50