The simplest of Beacons with basic configuration.

Dependencies:   BLE_API mbed nRF51822

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-2013 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 const static char DEVICE_NAME[] = "LEIM Beacon";
00020 uint8_t SERVICE_DEVICE_DESC[7] ={0x10, 0x10, 0x01, 0x12, 0x34, 0x56, 0x78};
00021 
00022 
00023 
00024 /**
00025  * This function is called when the ble initialization process has failed
00026  */
00027 void onBleInitError(BLE &ble, ble_error_t error)
00028 {
00029     /* Initialization error handling should go here */
00030 }
00031 
00032 /**
00033  * Callback triggered when the ble initialization process has finished
00034  */
00035 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00036 {
00037     BLE&        ble   = params->ble;
00038     ble_error_t error = params->error;
00039 
00040     if (error != BLE_ERROR_NONE) {
00041         /* In case of error, forward the error handling to onBleInitError */
00042         onBleInitError(ble, error);
00043         return;
00044     }
00045 
00046     /* Ensure that it is the default instance of BLE */
00047     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00048         return;
00049     }
00050  
00051     
00052 
00053     /* setup advertising */
00054     ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); // advertising type
00055     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
00056     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00057     ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)SERVICE_DEVICE_DESC, sizeof(SERVICE_DEVICE_DESC));
00058     ble.gap().setAdvertisingInterval(4000); 
00059     ble.gap().startAdvertising();
00060 }
00061 
00062 int main(void)
00063 {
00064     
00065     BLE &ble = BLE::Instance();
00066     ble.init(bleInitComplete);
00067 
00068     /* SpinWait for initialization to complete. This is necessary because the
00069      * BLE object is used in the main loop below. */
00070     while (ble.hasInitialized()  == false) { /* spin loop */ }
00071 
00072     while (true) {
00073         ble.waitForEvent();
00074     }
00075 }