test
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Beacon by
main.cpp@0:fca875b40c8a, 2014-09-09 (annotated)
- Committer:
- lukaszonak
- Date:
- Tue Sep 09 18:14:03 2014 +0000
- Revision:
- 0:fca875b40c8a
- Child:
- 1:d9a3dcb8c87c
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lukaszonak | 0:fca875b40c8a | 1 | /* mbed Microcontroller Library |
lukaszonak | 0:fca875b40c8a | 2 | * Copyright (c) 2006-2013 ARM Limited |
lukaszonak | 0:fca875b40c8a | 3 | * |
lukaszonak | 0:fca875b40c8a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
lukaszonak | 0:fca875b40c8a | 5 | * you may not use this file except in compliance with the License. |
lukaszonak | 0:fca875b40c8a | 6 | * You may obtain a copy of the License at |
lukaszonak | 0:fca875b40c8a | 7 | * |
lukaszonak | 0:fca875b40c8a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
lukaszonak | 0:fca875b40c8a | 9 | * |
lukaszonak | 0:fca875b40c8a | 10 | * Unless required by applicable law or agreed to in writing, software |
lukaszonak | 0:fca875b40c8a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
lukaszonak | 0:fca875b40c8a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
lukaszonak | 0:fca875b40c8a | 13 | * See the License for the specific language governing permissions and |
lukaszonak | 0:fca875b40c8a | 14 | * limitations under the License. |
lukaszonak | 0:fca875b40c8a | 15 | */ |
lukaszonak | 0:fca875b40c8a | 16 | |
lukaszonak | 0:fca875b40c8a | 17 | #include "mbed.h" |
lukaszonak | 0:fca875b40c8a | 18 | #include "BLEDevice.h" |
lukaszonak | 0:fca875b40c8a | 19 | |
lukaszonak | 0:fca875b40c8a | 20 | BLEDevice ble; |
lukaszonak | 0:fca875b40c8a | 21 | |
lukaszonak | 0:fca875b40c8a | 22 | #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; |
lukaszonak | 0:fca875b40c8a | 23 | * it will have an impact on code-size and power consumption. */ |
lukaszonak | 0:fca875b40c8a | 24 | |
lukaszonak | 0:fca875b40c8a | 25 | #if NEED_CONSOLE_OUTPUT |
lukaszonak | 0:fca875b40c8a | 26 | Serial pc(USBTX, USBRX); |
lukaszonak | 0:fca875b40c8a | 27 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
lukaszonak | 0:fca875b40c8a | 28 | #else |
lukaszonak | 0:fca875b40c8a | 29 | #define DEBUG(...) /* nothing */ |
lukaszonak | 0:fca875b40c8a | 30 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
lukaszonak | 0:fca875b40c8a | 31 | |
lukaszonak | 0:fca875b40c8a | 32 | /* |
lukaszonak | 0:fca875b40c8a | 33 | * For this demo application, populate the beacon advertisement payload |
lukaszonak | 0:fca875b40c8a | 34 | * with 2 AD structures: FLAG and MSD (manufacturer specific data). |
lukaszonak | 0:fca875b40c8a | 35 | * |
lukaszonak | 0:fca875b40c8a | 36 | * Reference: |
lukaszonak | 0:fca875b40c8a | 37 | * Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 11, 18 |
lukaszonak | 0:fca875b40c8a | 38 | */ |
lukaszonak | 0:fca875b40c8a | 39 | |
lukaszonak | 0:fca875b40c8a | 40 | /* |
lukaszonak | 0:fca875b40c8a | 41 | * The Beacon payload (encapsulated within the MSD advertising data structure) |
lukaszonak | 0:fca875b40c8a | 42 | * has the following composition: |
lukaszonak | 0:fca875b40c8a | 43 | * 128-Bit UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 |
lukaszonak | 0:fca875b40c8a | 44 | * Major/Minor = 0000 / 0000 |
lukaszonak | 0:fca875b40c8a | 45 | * Tx Power = C8 |
lukaszonak | 0:fca875b40c8a | 46 | */ |
lukaszonak | 0:fca875b40c8a | 47 | const uint8_t beaconPayload[] = { |
lukaszonak | 0:fca875b40c8a | 48 | 0x4C, 0x00, // Company identifier code (0x004C == Apple) |
lukaszonak | 0:fca875b40c8a | 49 | 0x02, // ID |
lukaszonak | 0:fca875b40c8a | 50 | 0x15, // length of the remaining payload |
lukaszonak | 0:fca875b40c8a | 51 | 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, // location UUID |
lukaszonak | 0:fca875b40c8a | 52 | 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61, |
lukaszonak | 0:fca875b40c8a | 53 | 0x00, 0x00, // the major value to differentiate a location |
lukaszonak | 0:fca875b40c8a | 54 | 0x00, 0x00, // the minor value to differentiate a location |
lukaszonak | 0:fca875b40c8a | 55 | 0xC8 // 2's complement of the Tx power (-56dB) |
lukaszonak | 0:fca875b40c8a | 56 | }; |
lukaszonak | 0:fca875b40c8a | 57 | |
lukaszonak | 0:fca875b40c8a | 58 | int main(void) |
lukaszonak | 0:fca875b40c8a | 59 | { |
lukaszonak | 0:fca875b40c8a | 60 | DEBUG("Initialising BTLE transport\n\r"); |
lukaszonak | 0:fca875b40c8a | 61 | ble.init(); |
lukaszonak | 0:fca875b40c8a | 62 | |
lukaszonak | 0:fca875b40c8a | 63 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
lukaszonak | 0:fca875b40c8a | 64 | ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, beaconPayload, sizeof(beaconPayload)); |
lukaszonak | 0:fca875b40c8a | 65 | |
lukaszonak | 0:fca875b40c8a | 66 | ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
lukaszonak | 0:fca875b40c8a | 67 | ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */ |
lukaszonak | 0:fca875b40c8a | 68 | ble.startAdvertising(); |
lukaszonak | 0:fca875b40c8a | 69 | |
lukaszonak | 0:fca875b40c8a | 70 | for (;;) { |
lukaszonak | 0:fca875b40c8a | 71 | ble.waitForEvent(); |
lukaszonak | 0:fca875b40c8a | 72 | } |
lukaszonak | 0:fca875b40c8a | 73 | |
lukaszonak | 0:fca875b40c8a | 74 | // An alternative to the above: |
lukaszonak | 0:fca875b40c8a | 75 | // |
lukaszonak | 0:fca875b40c8a | 76 | // DigitalOut mainloopLED(LED1); |
lukaszonak | 0:fca875b40c8a | 77 | // for (;;) { |
lukaszonak | 0:fca875b40c8a | 78 | // mainloopLED = !mainloopLED; |
lukaszonak | 0:fca875b40c8a | 79 | // ble.waitForEvent(); |
lukaszonak | 0:fca875b40c8a | 80 | // } |
lukaszonak | 0:fca875b40c8a | 81 | } |