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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 #include "mbed.h"
00018 #include "ble/BLE.h"
00019 
00020 /* Optional: Device Name, add for human read-ability */
00021 const static char     DEVICE_NAME[] = "ChangeMe!!";
00022 
00023 /* You have up to 26 bytes of advertising data to use. */
00024 const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05};   /* Example of hex data */
00025 //const static uint8_t AdvData[] = {"ChangeThisData"};         /* Example of character data */
00026 
00027 /* Optional: Restart advertising when peer disconnects */
00028 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00029 {
00030     BLE::Instance().gap().startAdvertising();
00031 }
00032 /**
00033  * This function is called when the ble initialization process has failed
00034  */
00035 void onBleInitError(BLE &ble, ble_error_t error)
00036 {
00037     /* Avoid compiler warnings */
00038     (void) ble;
00039     (void) error;
00040     
00041     /* Initialization error handling should go here */
00042 }    
00043 
00044 /**
00045  * Callback triggered when the ble initialization process has finished
00046  */
00047 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00048 {
00049     BLE&        ble   = params->ble;
00050     ble_error_t error = params->error;
00051 
00052     if (error != BLE_ERROR_NONE) {
00053         /* In case of error, forward the error handling to onBleInitError */
00054         onBleInitError(ble, error);
00055         return;
00056     }
00057 
00058     /* Ensure that it is the default instance of BLE */
00059     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00060         return;
00061     }
00062     
00063     /* Set device name characteristic data */
00064     ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);
00065 
00066     /* Optional: add callback for disconnection */
00067     ble.gap().onDisconnection(disconnectionCallback);
00068 
00069     /* Sacrifice 3B of 31B to Advertising Flags */
00070     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
00071     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00072 
00073     /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
00074     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
00075 
00076     /* Optional: Add name to device */
00077     //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00078 
00079     /* Set advertising interval. Longer interval == longer battery life */
00080     ble.gap().setAdvertisingInterval(100); /* 100ms */
00081 
00082     /* Start advertising */
00083     ble.gap().startAdvertising();
00084 }
00085 
00086 int main(void)
00087 {
00088     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
00089  
00090     /* Initialize BLE baselayer, always do this first! */
00091     ble.init(bleInitComplete);
00092 
00093     /* Infinite loop waiting for BLE events */
00094     while (true) {
00095         /* Save power while waiting for callback events */
00096         ble.waitForEvent();
00097     }
00098 }