Minimal usage example of BLE API

Dependencies:   BLE_API mbed nRF51822

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

main.cpp

Committer:
waynek
Date:
2016-01-18
Revision:
2:4f0d8bf09690
Parent:
1:b84d6e0b404e

File content as of revision 2:4f0d8bf09690:

#include "mbed.h"
#include "ble/BLE.h"

// DFUService is already included & automatically advertised by the mbed lib dependancies

const static char     DEVICE_NAME[]        = "HelloBlue";

BLEDevice ble;

void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
    BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
}

void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
    BLE &ble          = params->ble;
    ble_error_t error = params->error;

    if (error != BLE_ERROR_NONE) {
        return;
    }

    ble.gap().onDisconnection(disconnectionCallback);
    
    /* Setup advertising. */
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.gap().setAdvertisingInterval(1000); /* ms */
    ble.gap().startAdvertising();
}


int main(void)
{
    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
    ble.init(bleInitComplete);
    
    /* SpinWait for initialization to complete. This is necessary because the
     * BLE object is used in the main loop below. */
    while (ble.hasInitialized()  == false) { /* spin loop */ }
    
    while (1) 
    {
        // to see if a Central device is currently connected you can call: ble.getGapState().connected
        ble.waitForEvent(); // low power wait for event
    }
}