Beacon demo for the BLE API using the nRF51822 native mode drivers

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

Fork of BLE_iBeacon by Bluetooth Low Energy

Committer:
Rohit Grover
Date:
Thu May 22 11:09:28 2014 +0100
Revision:
11:6774f4827024
Parent:
10:391c1acf4b9d
Child:
12:00545c957af4
rename "nrf" to "ble"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 0:7613d21e5974 1 /* mbed Microcontroller Library
ktownsend 0:7613d21e5974 2 * Copyright (c) 2006-2013 ARM Limited
ktownsend 0:7613d21e5974 3 *
ktownsend 0:7613d21e5974 4 * Licensed under the Apache License, Version 2.0 (the "License");
ktownsend 0:7613d21e5974 5 * you may not use this file except in compliance with the License.
ktownsend 0:7613d21e5974 6 * You may obtain a copy of the License at
ktownsend 0:7613d21e5974 7 *
ktownsend 0:7613d21e5974 8 * http://www.apache.org/licenses/LICENSE-2.0
ktownsend 0:7613d21e5974 9 *
ktownsend 0:7613d21e5974 10 * Unless required by applicable law or agreed to in writing, software
ktownsend 0:7613d21e5974 11 * distributed under the License is distributed on an "AS IS" BASIS,
ktownsend 0:7613d21e5974 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ktownsend 0:7613d21e5974 13 * See the License for the specific language governing permissions and
ktownsend 0:7613d21e5974 14 * limitations under the License.
ktownsend 0:7613d21e5974 15 */
ktownsend 0:7613d21e5974 16
ktownsend 0:7613d21e5974 17 #include "mbed.h"
ktownsend 0:7613d21e5974 18 #include "nRF51822n.h"
ktownsend 0:7613d21e5974 19
Rohit Grover 11:6774f4827024 20 nRF51822n ble; /* BLE radio driver */
ktownsend 0:7613d21e5974 21
Rohit Grover 9:438f44012039 22 DigitalOut mainloopLED(LED1);
Rohit Grover 9:438f44012039 23 DigitalOut tickerLED(LED2);
ktownsend 0:7613d21e5974 24 Ticker flipper;
ktownsend 0:7613d21e5974 25 Serial pc(USBTX,USBRX);
ktownsend 0:7613d21e5974 26
Rohit Grover 10:391c1acf4b9d 27 /*
Rohit Grover 10:391c1acf4b9d 28 * For this demo application, populate the beacon advertisement payload
Rohit Grover 10:391c1acf4b9d 29 * with 2 AD structures: FLAG and MSD
Rohit Grover 10:391c1acf4b9d 30 *
Rohit Grover 10:391c1acf4b9d 31 * Reference:
Rohit Grover 10:391c1acf4b9d 32 * Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 11, 18
Rohit Grover 10:391c1acf4b9d 33 */
ktownsend 0:7613d21e5974 34
Rohit Grover 10:391c1acf4b9d 35 /* Define an Beacon payload.
Rohit Grover 10:391c1acf4b9d 36 *
Rohit Grover 10:391c1acf4b9d 37 * This is the data part of the MSD AdvertisingData structure to be added to
Rohit Grover 10:391c1acf4b9d 38 * the advertising payload.
Rohit Grover 10:391c1acf4b9d 39 * --------------------------------------------------------------
Rohit Grover 10:391c1acf4b9d 40 * 128-Bit UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61
Rohit Grover 10:391c1acf4b9d 41 * Major/Minor = 0000 / 0000
Rohit Grover 10:391c1acf4b9d 42 * Tx Power = C8
Rohit Grover 10:391c1acf4b9d 43 */
Rohit Grover 10:391c1acf4b9d 44 uint8_t beaconPayload[] = {
Rohit Grover 10:391c1acf4b9d 45 0x4C, 0x00, // Company identifier code (0x004C == Apple)
Rohit Grover 10:391c1acf4b9d 46 0x02, // ID
Rohit Grover 10:391c1acf4b9d 47 0x15, // length of the remaining payload
Rohit Grover 10:391c1acf4b9d 48 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, // UUID
Rohit Grover 10:391c1acf4b9d 49 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61,
Rohit Grover 10:391c1acf4b9d 50 0x00, 0x00, // the major value to differenciate a location
Rohit Grover 10:391c1acf4b9d 51 0x00, 0x00, // the minor value to differenciate a location
Rohit Grover 10:391c1acf4b9d 52 0xC8 // 2's complement of the Tx power (-56dB)
Rohit Grover 10:391c1acf4b9d 53 };
Rohit Grover 10:391c1acf4b9d 54
Rohit Grover 10:391c1acf4b9d 55 static void setupAppHardware(void);
Rohit Grover 10:391c1acf4b9d 56 static void tickerCallback(void);
Rohit Grover 10:391c1acf4b9d 57
ktownsend 0:7613d21e5974 58 int main(void)
ktownsend 0:7613d21e5974 59 {
Rohit Grover 10:391c1acf4b9d 60 setupAppHardware();
ktownsend 0:7613d21e5974 61
ktownsend 0:7613d21e5974 62 /* Initialise the nRF51822 */
ktownsend 0:7613d21e5974 63 pc.printf("Initialising the nRF51822\n\r");
Rohit Grover 11:6774f4827024 64 ble.init();
Rohit Grover 11:6774f4827024 65 ble.reset();
ktownsend 0:7613d21e5974 66
Rohit Grover 10:391c1acf4b9d 67 /* Setup advertising data. This includes AD structures in the payload of
Rohit Grover 10:391c1acf4b9d 68 * advertising packets; and scan-response data. */
Rohit Grover 10:391c1acf4b9d 69 {
Rohit Grover 10:391c1acf4b9d 70 GapAdvertisingData advData;
Rohit Grover 10:391c1acf4b9d 71 advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Rohit Grover 10:391c1acf4b9d 72 advData.addData(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
Rohit Grover 10:391c1acf4b9d 73 beaconPayload,
Rohit Grover 10:391c1acf4b9d 74 sizeof(beaconPayload));
Rohit Grover 10:391c1acf4b9d 75 GapAdvertisingData scanResponse;
Rohit Grover 11:6774f4827024 76 ble.getGap().setAdvertisingData(advData, scanResponse);
Rohit Grover 10:391c1acf4b9d 77 }
ktownsend 0:7613d21e5974 78
ktownsend 0:7613d21e5974 79 /* Start advertising! */
Rohit Grover 10:391c1acf4b9d 80 GapAdvertisingParams advParams(
Rohit Grover 10:391c1acf4b9d 81 GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
Rohit Grover 11:6774f4827024 82 ble.getGap().startAdvertising(advParams);
ktownsend 0:7613d21e5974 83
Rohit Grover 9:438f44012039 84 /* Do blinky on mainloopLED while we're waiting for BLE events */
rohit.grover 5:97ce285ff00a 85 for (;; ) {
Rohit Grover 9:438f44012039 86 mainloopLED = !mainloopLED;
rohit.grover 6:26eab6ee6df4 87 wait(1);
ktownsend 0:7613d21e5974 88 }
Rohit Grover 10:391c1acf4b9d 89
Rohit Grover 10:391c1acf4b9d 90 /* unreachable. */
Rohit Grover 10:391c1acf4b9d 91 }
Rohit Grover 10:391c1acf4b9d 92
Rohit Grover 10:391c1acf4b9d 93 void setupAppHardware(void)
Rohit Grover 10:391c1acf4b9d 94 {
Rohit Grover 10:391c1acf4b9d 95 /* Setup blinkies: mainloopLED is toggled in main, tickerLED is
Rohit Grover 10:391c1acf4b9d 96 * toggled via Ticker */
Rohit Grover 10:391c1acf4b9d 97 mainloopLED = 1;
Rohit Grover 10:391c1acf4b9d 98 tickerLED = 1;
Rohit Grover 10:391c1acf4b9d 99 flipper.attach(&tickerCallback, 1.0);
ktownsend 0:7613d21e5974 100 }
ktownsend 0:7613d21e5974 101
ktownsend 0:7613d21e5974 102 /**************************************************************************/
ktownsend 0:7613d21e5974 103 /*!
Rohit Grover 9:438f44012039 104 @brief Ticker callback to switch tickerLED state
ktownsend 0:7613d21e5974 105 */
ktownsend 0:7613d21e5974 106 /**************************************************************************/
ktownsend 0:7613d21e5974 107 void tickerCallback(void)
ktownsend 0:7613d21e5974 108 {
Rohit Grover 9:438f44012039 109 tickerLED = !tickerLED;
ktownsend 0:7613d21e5974 110 }