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 Blackstone Engineering

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 12:00:16 2016 +0000
Revision:
15:7e06fce6e4f8
Parent:
14:1c15d473b42f
Add missing license information to source code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andresag15:7e06fce6e4f8 1/* mbed Microcontroller Library
andresag15:7e06fce6e4f8 2 * Copyright (c) 2006-2015 ARM Limited
andresag15:7e06fce6e4f8 3 *
andresag15:7e06fce6e4f8 4 * Licensed under the Apache License, Version 2.0 (the "License");
andresag15:7e06fce6e4f8 5 * you may not use this file except in compliance with the License.
andresag15:7e06fce6e4f8 6 * You may obtain a copy of the License at
andresag15:7e06fce6e4f8 7 *
andresag15:7e06fce6e4f8 8 * http://www.apache.org/licenses/LICENSE-2.0
andresag15:7e06fce6e4f8 9 *
andresag15:7e06fce6e4f8 10 * Unless required by applicable law or agreed to in writing, software
andresag15:7e06fce6e4f8 11 * distributed under the License is distributed on an "AS IS" BASIS,
andresag15:7e06fce6e4f8 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
andresag15:7e06fce6e4f8 13 * See the License for the specific language governing permissions and
andresag15:7e06fce6e4f8 14 * limitations under the License.
andresag15:7e06fce6e4f8 15 */
andresag15:7e06fce6e4f8 16
mbedAustin0:5375be4301ed 17#include "mbed.h"
andresag14:1c15d473b42f 18#include "ble/BLE.h"
mbedAustin0:5375be4301ed 19
andresag14:1c15d473b42f 20/* Optional: Device Name, add for human read-ability */
andresag14:1c15d473b42f 21const static char DEVICE_NAME[] = "ChangeMe!!";
mbedAustin0:5375be4301ed 22
andresag14:1c15d473b42f 23/* You have up to 26 bytes of advertising data to use. */
andresag14:1c15d473b42f 24const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05}; /* Example of hex data */
andresag14:1c15d473b42f 25//const static uint8_t AdvData[] = {"ChangeThisData"}; /* Example of character data */
mbedAustin5:fff16d283dcf 26
andresag14:1c15d473b42f 27/* Optional: Restart advertising when peer disconnects */
andresag14:1c15d473b42f 28void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
andresag14:1c15d473b42f 29{
andresag14:1c15d473b42f 30 BLE::Instance().gap().startAdvertising();
andresag14:1c15d473b42f 31}
andresag14:1c15d473b42f 32/**
andresag14:1c15d473b42f 33 * This function is called when the ble initialization process has failed
andresag14:1c15d473b42f 34 */
andresag14:1c15d473b42f 35void onBleInitError(BLE &ble, ble_error_t error)
andresag14:1c15d473b42f 36{
andresag14:1c15d473b42f 37 /* Avoid compiler warnings */
andresag14:1c15d473b42f 38 (void) ble;
andresag14:1c15d473b42f 39 (void) error;
andresag14:1c15d473b42f 40
andresag14:1c15d473b42f 41 /* Initialization error handling should go here */
andresag14:1c15d473b42f 42}
mbedAustin11:c9c0c4586c5f 43
andresag14:1c15d473b42f 44/**
andresag14:1c15d473b42f 45 * Callback triggered when the ble initialization process has finished
andresag14:1c15d473b42f 46 */
andresag14:1c15d473b42f 47void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
mbedAustin13:827dd2b32bb8 48{
andresag14:1c15d473b42f 49 BLE& ble = params->ble;
andresag14:1c15d473b42f 50 ble_error_t error = params->error;
andresag14:1c15d473b42f 51
andresag14:1c15d473b42f 52 if (error != BLE_ERROR_NONE) {
andresag14:1c15d473b42f 53 /* In case of error, forward the error handling to onBleInitError */
andresag14:1c15d473b42f 54 onBleInitError(ble, error);
andresag14:1c15d473b42f 55 return;
andresag14:1c15d473b42f 56 }
andresag14:1c15d473b42f 57
andresag14:1c15d473b42f 58 /* Ensure that it is the default instance of BLE */
andresag14:1c15d473b42f 59 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
andresag14:1c15d473b42f 60 return;
andresag14:1c15d473b42f 61 }
andresag14:1c15d473b42f 62
andresag14:1c15d473b42f 63 /* Set device name characteristic data */
andresag14:1c15d473b42f 64 ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);
andresag14:1c15d473b42f 65
andresag14:1c15d473b42f 66 /* Optional: add callback for disconnection */
andresag14:1c15d473b42f 67 ble.gap().onDisconnection(disconnectionCallback);
andresag14:1c15d473b42f 68
andresag14:1c15d473b42f 69 /* Sacrifice 3B of 31B to Advertising Flags */
andresag14:1c15d473b42f 70 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
andresag14:1c15d473b42f 71 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
andresag14:1c15d473b42f 72
andresag14:1c15d473b42f 73 /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
andresag14:1c15d473b42f 74 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
andresag14:1c15d473b42f 75
andresag14:1c15d473b42f 76 /* Optional: Add name to device */
andresag14:1c15d473b42f 77 //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
andresag14:1c15d473b42f 78
andresag14:1c15d473b42f 79 /* Set advertising interval. Longer interval == longer battery life */
andresag14:1c15d473b42f 80 ble.gap().setAdvertisingInterval(100); /* 100ms */
andresag14:1c15d473b42f 81
andresag14:1c15d473b42f 82 /* Start advertising */
andresag14:1c15d473b42f 83 ble.gap().startAdvertising();
mbedAustin8:5442739198ec 84}
mbedAustin8:5442739198ec 85
mbedAustin1:0692bee84264 86int main(void)
mbedAustin0:5375be4301ed 87{
andresag14:1c15d473b42f 88 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
andresag14:1c15d473b42f 89
andresag14:1c15d473b42f 90 /* Initialize BLE baselayer, always do this first! */
andresag14:1c15d473b42f 91 ble.init(bleInitComplete);
mbedAustin13:827dd2b32bb8 92
andresag14:1c15d473b42f 93 /* Infinite loop waiting for BLE events */
andresag14:1c15d473b42f 94 while (true) {
andresag14:1c15d473b42f 95 /* Save power while waiting for callback events */
andresag14:1c15d473b42f 96 ble.waitForEvent();
mbedAustin1:0692bee84264 97 }
mbedAustin1:0692bee84264 98}