This example program shows how to set up a GAP advertising packet to broadcast data from the device out to anyone who may be listening.

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_EvothingsExample_GAP by Austin Blackstone

Intro

This code is meant to demonstrate how to use the GAP advertising layer to transmit data. Keep in mind you only have 26B of data in the Manufacturer data field to use.

Video of use

Details

Fill in the data you want to transmit by changing the following line, try using the string representation by changing hte "CHangeThisData" field or send hex data by altering the hex data field.

// You have up to 26 bytes of advertising data to use.
const static uint8_t AdvData[] = {"ChangeThisData"};             // example of character data
//const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05};   // example of hex data

Name Device

Optionally you can give your device a name by uncommenting the following two lines

//const static char     DEVICE_NAME[]        = "ChangeMe!!"; // change this
...
//ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));

Add Disconnection Callback

This code is bare bones and as such does not restart advertising when disconnected from a device. To add this functionality into your code please add the following callback

// Restart advertising when phone app disconnects
void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
    ble.startAdvertising(); 
}

and the following initialization to your main function

int 
main(void)
{
...
ble.onDisconnection(disconnectionCallback);
...

Viewing Data

You can use either the LightBlue app on iOS or the nRF Master Control Panel application on Android to view the advertising data. Alternatively you can use a custom Evothings App to view the data.

Evothings?

Evothings is a rapid prototyping environment that uses cordova to enable you to rapidly develop smartphone applications in Javascript. Please download the Evothings workbench to your computer and the Evothings client to your smartphone. In the Evothings Workbench you will find an example program called "mbed Evothings GAP", this Evothings Smartphone App is meant to be used with the above embedded mbed code. For instructions on how to use Evothings please see the reference section below.

Reference

Committer:
andresag
Date:
Tue Jan 12 11:58:24 2016 +0000
Revision:
14:1c15d473b42f
Parent:
13:827dd2b32bb8
Child:
15:7e06fce6e4f8
Update example to latest BLE API.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 0:5375be4301ed 1 #include "mbed.h"
andresag 14:1c15d473b42f 2 #include "ble/BLE.h"
andresag 14:1c15d473b42f 3
andresag 14:1c15d473b42f 4 /* Optional: Device Name, add for human read-ability */
andresag 14:1c15d473b42f 5 const static char DEVICE_NAME[] = "ChangeMe!!";
andresag 14:1c15d473b42f 6
andresag 14:1c15d473b42f 7 /* You have up to 26 bytes of advertising data to use. */
andresag 14:1c15d473b42f 8 const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05}; /* Example of hex data */
andresag 14:1c15d473b42f 9 //const static uint8_t AdvData[] = {"ChangeThisData"}; /* Example of character data */
mbedAustin 0:5375be4301ed 10
andresag 14:1c15d473b42f 11 /* Optional: Restart advertising when peer disconnects */
andresag 14:1c15d473b42f 12 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
andresag 14:1c15d473b42f 13 {
andresag 14:1c15d473b42f 14 BLE::Instance().gap().startAdvertising();
andresag 14:1c15d473b42f 15 }
andresag 14:1c15d473b42f 16 /**
andresag 14:1c15d473b42f 17 * This function is called when the ble initialization process has failed
andresag 14:1c15d473b42f 18 */
andresag 14:1c15d473b42f 19 void onBleInitError(BLE &ble, ble_error_t error)
andresag 14:1c15d473b42f 20 {
andresag 14:1c15d473b42f 21 /* Avoid compiler warnings */
andresag 14:1c15d473b42f 22 (void) ble;
andresag 14:1c15d473b42f 23 (void) error;
andresag 14:1c15d473b42f 24
andresag 14:1c15d473b42f 25 /* Initialization error handling should go here */
andresag 14:1c15d473b42f 26 }
mbedAustin 0:5375be4301ed 27
andresag 14:1c15d473b42f 28 /**
andresag 14:1c15d473b42f 29 * Callback triggered when the ble initialization process has finished
andresag 14:1c15d473b42f 30 */
andresag 14:1c15d473b42f 31 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 14:1c15d473b42f 32 {
andresag 14:1c15d473b42f 33 BLE& ble = params->ble;
andresag 14:1c15d473b42f 34 ble_error_t error = params->error;
andresag 14:1c15d473b42f 35
andresag 14:1c15d473b42f 36 if (error != BLE_ERROR_NONE) {
andresag 14:1c15d473b42f 37 /* In case of error, forward the error handling to onBleInitError */
andresag 14:1c15d473b42f 38 onBleInitError(ble, error);
andresag 14:1c15d473b42f 39 return;
andresag 14:1c15d473b42f 40 }
mbedAustin 5:fff16d283dcf 41
andresag 14:1c15d473b42f 42 /* Ensure that it is the default instance of BLE */
andresag 14:1c15d473b42f 43 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
andresag 14:1c15d473b42f 44 return;
andresag 14:1c15d473b42f 45 }
andresag 14:1c15d473b42f 46
andresag 14:1c15d473b42f 47 /* Set device name characteristic data */
andresag 14:1c15d473b42f 48 ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);
andresag 14:1c15d473b42f 49
andresag 14:1c15d473b42f 50 /* Optional: add callback for disconnection */
andresag 14:1c15d473b42f 51 ble.gap().onDisconnection(disconnectionCallback);
mbedAustin 1:0692bee84264 52
andresag 14:1c15d473b42f 53 /* Sacrifice 3B of 31B to Advertising Flags */
andresag 14:1c15d473b42f 54 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
andresag 14:1c15d473b42f 55 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
andresag 14:1c15d473b42f 56
andresag 14:1c15d473b42f 57 /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
andresag 14:1c15d473b42f 58 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
andresag 14:1c15d473b42f 59
andresag 14:1c15d473b42f 60 /* Optional: Add name to device */
andresag 14:1c15d473b42f 61 //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
andresag 14:1c15d473b42f 62
andresag 14:1c15d473b42f 63 /* Set advertising interval. Longer interval == longer battery life */
andresag 14:1c15d473b42f 64 ble.gap().setAdvertisingInterval(100); /* 100ms */
andresag 14:1c15d473b42f 65
andresag 14:1c15d473b42f 66 /* Start advertising */
andresag 14:1c15d473b42f 67 ble.gap().startAdvertising();
mbedAustin 8:5442739198ec 68 }
mbedAustin 8:5442739198ec 69
mbedAustin 1:0692bee84264 70 int main(void)
mbedAustin 0:5375be4301ed 71 {
andresag 14:1c15d473b42f 72 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
andresag 14:1c15d473b42f 73
andresag 14:1c15d473b42f 74 /* Initialize BLE baselayer, always do this first! */
andresag 14:1c15d473b42f 75 ble.init(bleInitComplete);
mbedAustin 1:0692bee84264 76
andresag 14:1c15d473b42f 77 /* Infinite loop waiting for BLE events */
andresag 14:1c15d473b42f 78 while (true) {
andresag 14:1c15d473b42f 79 /* Save power while waiting for callback events */
andresag 14:1c15d473b42f 80 ble.waitForEvent();
mbedAustin 1:0692bee84264 81 }
mbedAustin 1:0692bee84264 82 }