James Shirazian / Mbed 2 deprecated BLE_Beacon

Dependencies:   BLE_API mbed-src-nrf51822 mbed nRF51822

Fork of BLE_Beacon by TAIL

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 "BLEDevice.h"
00019 
00020 BLEDevice ble;
00021 
00022 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
00023 * it will have an impact on code-size and power consumption. */
00024 
00025 #if NEED_CONSOLE_OUTPUT
00026 Serial  pc(USBTX, USBRX);
00027 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
00028 #else
00029 #define DEBUG(...) /* nothing */
00030 #endif /* #if NEED_CONSOLE_OUTPUT */
00031 
00032 /*
00033  * For this demo application, populate the beacon advertisement payload
00034  * with 2 AD structures: FLAG and MSD (manufacturer specific data).
00035  *
00036  * Reference:
00037  *  Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 11, 18
00038  */
00039 
00040 /*
00041  * The Beacon payload (encapsulated within the MSD advertising data structure)
00042  * has the following composition:
00043  * 128-Bit UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61
00044  * Major/Minor  = 0000 / 0000
00045  * Tx Power     = C8
00046  */
00047 const uint8_t beaconPayload[] = {
00048     0x4C, 0x00, // Company identifier code (0x004C == Apple)
00049     0x02,       // ID
00050     0x15,       // length of the remaining payload
00051     0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, // location UUID
00052     0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61,
00053     0x00, 0x00, // the major value to differentiate a location
00054     0x00, 0x00, // the minor value to differentiate a location
00055     0xC8        // 2's complement of the Tx power (-56dB)
00056 };
00057 
00058 int main(void)
00059 {
00060     DEBUG("Initialising BTLE transport\n\r");
00061     ble.init();
00062 
00063     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00064     ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, beaconPayload, sizeof(beaconPayload));
00065 
00066     ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
00067     ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */
00068     ble.startAdvertising();
00069 
00070     for (;;) {
00071         ble.waitForEvent();
00072     }
00073 
00074     // An alternative to the above:
00075     //
00076     // DigitalOut mainloopLED(LED1);
00077     // for (;;) {
00078     //     mainloopLED = !mainloopLED;
00079     //     ble.waitForEvent();
00080     // }
00081 }