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@75:b0385b4fdc3e, 2015-11-06 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Nov 06 13:24:28 2015 +0000
- Revision:
- 75:b0385b4fdc3e
- Parent:
- 74:7754bf460f52
- Child:
- 77:7674b63f8aea
oops. had forgotten to put in the spin loop during initialization(). This isn't necessary for nRF, but could be necessary for other controllers.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ktownsend | 0:7613d21e5974 | 1 | /* mbed Microcontroller Library |
rgrover1 | 71:12660a3eb07d | 2 | * Copyright (c) 2006-2015 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" |
rgrover1 | 71:12660a3eb07d | 18 | #include "ble/services/iBeacon.h" |
mbedAustin | 56:56bc0cab3916 | 19 | |
rgrover1 | 69:f121dba6fcd3 | 20 | BLE ble; |
rgrover1 | 74:7754bf460f52 | 21 | |
rgrover1 | 74:7754bf460f52 | 22 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
ktownsend | 0:7613d21e5974 | 23 | { |
rgrover1 | 74:7754bf460f52 | 24 | BLE &ble = params->ble; |
rgrover1 | 74:7754bf460f52 | 25 | ble_error_t error = params->error; |
mbedAustin | 53:f9ec2c7a47f5 | 26 | |
rgrover1 | 74:7754bf460f52 | 27 | if (error != BLE_ERROR_NONE) { |
rgrover1 | 74:7754bf460f52 | 28 | return; |
rgrover1 | 74:7754bf460f52 | 29 | } |
rgrover1 | 74:7754bf460f52 | 30 | |
rgrover1 | 71:12660a3eb07d | 31 | /** |
rgrover1 | 71:12660a3eb07d | 32 | * The Beacon payload has the following composition: |
rgrover1 | 71:12660a3eb07d | 33 | * 128-Bit / 16byte UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 |
rgrover1 | 71:12660a3eb07d | 34 | * Major/Minor = 0x1122 / 0x3344 |
rgrover1 | 71:12660a3eb07d | 35 | * Tx Power = 0xC8 = 200, 2's compliment is 256-200 = (-56dB) |
rgrover1 | 71:12660a3eb07d | 36 | * |
rgrover1 | 71:12660a3eb07d | 37 | * Note: please remember to calibrate your beacons TX Power for more accurate results. |
rgrover1 | 71:12660a3eb07d | 38 | */ |
rgrover1 | 71:12660a3eb07d | 39 | const uint8_t uuid[] = {0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, |
rgrover1 | 71:12660a3eb07d | 40 | 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61}; |
rgrover1 | 71:12660a3eb07d | 41 | uint16_t majorNumber = 1122; |
rgrover1 | 71:12660a3eb07d | 42 | uint16_t minorNumber = 3344; |
rgrover1 | 71:12660a3eb07d | 43 | uint16_t txPower = 0xC8; |
rgrover1 | 74:7754bf460f52 | 44 | iBeacon *ibeacon = new iBeacon(ble, uuid, majorNumber, minorNumber, txPower); |
mbedAustin | 53:f9ec2c7a47f5 | 45 | |
rgrover1 | 69:f121dba6fcd3 | 46 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
rgrover1 | 69:f121dba6fcd3 | 47 | ble.gap().startAdvertising(); |
rgrover1 | 74:7754bf460f52 | 48 | } |
rgrover1 | 74:7754bf460f52 | 49 | |
rgrover1 | 74:7754bf460f52 | 50 | int main(void) |
rgrover1 | 74:7754bf460f52 | 51 | { |
rgrover1 | 74:7754bf460f52 | 52 | ble.init(bleInitComplete); |
rgrover1 | 75:b0385b4fdc3e | 53 | |
rgrover1 | 75:b0385b4fdc3e | 54 | /* SpinWait for initialization to complete. This is necessary because the |
rgrover1 | 75:b0385b4fdc3e | 55 | * BLE object is used in the main loop below. */ |
rgrover1 | 75:b0385b4fdc3e | 56 | while (ble.hasInitialized()) { /* spin loop */ } |
ktownsend | 0:7613d21e5974 | 57 | |
rgrover1 | 72:eb4de3de66b8 | 58 | while (true) { |
mbedAustin | 56:56bc0cab3916 | 59 | ble.waitForEvent(); // allows or low power operation |
ktownsend | 0:7613d21e5974 | 60 | } |
Rohit Grover |
10:391c1acf4b9d | 61 | } |