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
main.cpp@43:b5dc3241fc91, 2014-09-02 (annotated)
- Committer:
- rgrover1
- Date:
- Tue Sep 02 16:09:59 2014 +0000
- Revision:
- 43:b5dc3241fc91
- Parent:
- 42:bb46ad5c24dd
- Child:
- 44:71ff94cd9c1d
updating underlying libraries.
Who changed what in which revision?
User | Revision | Line number | New 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" |
Rohit Grover |
32:7b7093b653a8 | 18 | #include "BLEDevice.h" |
ktownsend | 0:7613d21e5974 | 19 | |
Rohit Grover |
32:7b7093b653a8 | 20 | BLEDevice ble; |
Rohit Grover |
31:93e50a3c3dc6 | 21 | |
Rohit Grover |
31:93e50a3c3dc6 | 22 | #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; |
Rohit Grover |
31:93e50a3c3dc6 | 23 | * it will have an impact on code-size and power consumption. */ |
ktownsend | 0:7613d21e5974 | 24 | |
Rohit Grover |
31:93e50a3c3dc6 | 25 | #if NEED_CONSOLE_OUTPUT |
Rohit Grover |
31:93e50a3c3dc6 | 26 | Serial pc(USBTX, USBRX); |
Rohit Grover |
31:93e50a3c3dc6 | 27 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
Rohit Grover |
31:93e50a3c3dc6 | 28 | #else |
Rohit Grover |
31:93e50a3c3dc6 | 29 | #define DEBUG(...) /* nothing */ |
Rohit Grover |
31:93e50a3c3dc6 | 30 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
ktownsend | 0:7613d21e5974 | 31 | |
Rohit Grover |
10:391c1acf4b9d | 32 | /* |
Rohit Grover |
10:391c1acf4b9d | 33 | * For this demo application, populate the beacon advertisement payload |
Rohit Grover |
15:4e1b36b73213 | 34 | * with 2 AD structures: FLAG and MSD (manufacturer specific data). |
Rohit Grover |
10:391c1acf4b9d | 35 | * |
Rohit Grover |
10:391c1acf4b9d | 36 | * Reference: |
Rohit Grover |
10:391c1acf4b9d | 37 | * Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 11, 18 |
Rohit Grover |
10:391c1acf4b9d | 38 | */ |
ktownsend | 0:7613d21e5974 | 39 | |
Rohit Grover |
14:dfdf0c8b1c09 | 40 | /* |
Rohit Grover |
15:4e1b36b73213 | 41 | * The Beacon payload (encapsulated within the MSD advertising data structure) |
Rohit Grover |
15:4e1b36b73213 | 42 | * has the following composition: |
Rohit Grover |
10:391c1acf4b9d | 43 | * 128-Bit UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 |
Rohit Grover |
10:391c1acf4b9d | 44 | * Major/Minor = 0000 / 0000 |
Rohit Grover |
10:391c1acf4b9d | 45 | * Tx Power = C8 |
Rohit Grover |
10:391c1acf4b9d | 46 | */ |
rgrover1 | 41:51f585d14675 | 47 | const uint8_t beaconPayload[] = { |
rgrover1 | 43:b5dc3241fc91 | 48 | 0x4C, 0x00, |
rgrover1 | 43:b5dc3241fc91 | 49 | 0x02, |
rgrover1 | 43:b5dc3241fc91 | 50 | 0x15, |
rgrover1 | 43:b5dc3241fc91 | 51 | 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, |
Rohit Grover |
10:391c1acf4b9d | 52 | 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61, |
rgrover1 | 43:b5dc3241fc91 | 53 | 0x00, 0x00, |
rgrover1 | 43:b5dc3241fc91 | 54 | 0x00, 0x00, |
rgrover1 | 43:b5dc3241fc91 | 55 | 0xC8 |
Rohit Grover |
10:391c1acf4b9d | 56 | }; |
Rohit Grover |
10:391c1acf4b9d | 57 | |
ktownsend | 0:7613d21e5974 | 58 | int main(void) |
ktownsend | 0:7613d21e5974 | 59 | { |
Rohit Grover |
31:93e50a3c3dc6 | 60 | DEBUG("Initialising BTLE transport\n\r"); |
Rohit Grover |
11:6774f4827024 | 61 | ble.init(); |
ktownsend | 0:7613d21e5974 | 62 | |
Rohit Grover |
37:205deeded79d | 63 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Rohit Grover |
27:8d4f5bda1191 | 64 | ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, beaconPayload, sizeof(beaconPayload)); |
ktownsend | 0:7613d21e5974 | 65 | |
Rohit Grover |
20:5e84b5b253a5 | 66 | ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
rgrover1 | 41:51f585d14675 | 67 | ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */ |
Rohit Grover |
19:869d8c7306b4 | 68 | ble.startAdvertising(); |
ktownsend | 0:7613d21e5974 | 69 | |
Rohit Grover |
21:a61af863b273 | 70 | for (;;) { |
Rohit Grover |
31:93e50a3c3dc6 | 71 | ble.waitForEvent(); |
ktownsend | 0:7613d21e5974 | 72 | } |
Rohit Grover |
31:93e50a3c3dc6 | 73 | |
Rohit Grover |
31:93e50a3c3dc6 | 74 | // An alternative to the above: |
Rohit Grover |
31:93e50a3c3dc6 | 75 | // |
Rohit Grover |
31:93e50a3c3dc6 | 76 | // DigitalOut mainloopLED(LED1); |
Rohit Grover |
31:93e50a3c3dc6 | 77 | // for (;;) { |
Rohit Grover |
31:93e50a3c3dc6 | 78 | // mainloopLED = !mainloopLED; |
rgrover1 | 42:bb46ad5c24dd | 79 | // ble.waitForEvent(); |
Rohit Grover |
31:93e50a3c3dc6 | 80 | // } |
Rohit Grover |
10:391c1acf4b9d | 81 | } |