Luca Mottola
/
IOTAtelier1819-BLEBeacon
BLE beacon example
source/main.cpp@2:401488de6e22, 2016-07-28 (annotated)
- Committer:
- Vincent Coubard
- Date:
- Thu Jul 28 23:32:47 2016 +0100
- Revision:
- 2:401488de6e22
- Parent:
- 1:2fd54f1254fe
- Child:
- 3:41f6be68aefb
Sync with mbed-os-5.1.0-rc3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vincent Coubard |
2:401488de6e22 | 1 | /* mbed Microcontroller Library |
Vincent Coubard |
2:401488de6e22 | 2 | * Copyright (c) 2006-2015 ARM Limited |
Vincent Coubard |
2:401488de6e22 | 3 | * |
Vincent Coubard |
2:401488de6e22 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Vincent Coubard |
2:401488de6e22 | 5 | * you may not use this file except in compliance with the License. |
Vincent Coubard |
2:401488de6e22 | 6 | * You may obtain a copy of the License at |
Vincent Coubard |
2:401488de6e22 | 7 | * |
Vincent Coubard |
2:401488de6e22 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Vincent Coubard |
2:401488de6e22 | 9 | * |
Vincent Coubard |
2:401488de6e22 | 10 | * Unless required by applicable law or agreed to in writing, software |
Vincent Coubard |
2:401488de6e22 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Vincent Coubard |
2:401488de6e22 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Vincent Coubard |
2:401488de6e22 | 13 | * See the License for the specific language governing permissions and |
Vincent Coubard |
2:401488de6e22 | 14 | * limitations under the License. |
Vincent Coubard |
2:401488de6e22 | 15 | */ |
Vincent Coubard |
2:401488de6e22 | 16 | |
Vincent Coubard |
2:401488de6e22 | 17 | #include <mbed-events/events.h> |
Vincent Coubard |
2:401488de6e22 | 18 | #include <mbed.h> |
Vincent Coubard |
2:401488de6e22 | 19 | #include "ble/BLE.h" |
Vincent Coubard |
2:401488de6e22 | 20 | #include "ble/services/iBeacon.h" |
Vincent Coubard |
2:401488de6e22 | 21 | |
Vincent Coubard |
2:401488de6e22 | 22 | static iBeacon* ibeaconPtr; |
Vincent Coubard |
2:401488de6e22 | 23 | |
Vincent Coubard |
2:401488de6e22 | 24 | static EventQueue eventQueue( |
Vincent Coubard |
2:401488de6e22 | 25 | /* event count */ 4 * /* event size */ 32 |
Vincent Coubard |
2:401488de6e22 | 26 | ); |
Vincent Coubard |
2:401488de6e22 | 27 | |
Vincent Coubard |
2:401488de6e22 | 28 | /** |
Vincent Coubard |
2:401488de6e22 | 29 | * This function is called when the ble initialization process has failled |
Vincent Coubard |
2:401488de6e22 | 30 | */ |
Vincent Coubard |
2:401488de6e22 | 31 | void onBleInitError(BLE &ble, ble_error_t error) |
Vincent Coubard |
2:401488de6e22 | 32 | { |
Vincent Coubard |
2:401488de6e22 | 33 | /* Initialization error handling should go here */ |
Vincent Coubard |
2:401488de6e22 | 34 | } |
Vincent Coubard |
2:401488de6e22 | 35 | |
Vincent Coubard |
2:401488de6e22 | 36 | /** |
Vincent Coubard |
2:401488de6e22 | 37 | * Callback triggered when the ble initialization process has finished |
Vincent Coubard |
2:401488de6e22 | 38 | */ |
Vincent Coubard |
2:401488de6e22 | 39 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
Vincent Coubard |
2:401488de6e22 | 40 | { |
Vincent Coubard |
2:401488de6e22 | 41 | BLE& ble = params->ble; |
Vincent Coubard |
2:401488de6e22 | 42 | ble_error_t error = params->error; |
Vincent Coubard |
2:401488de6e22 | 43 | |
Vincent Coubard |
2:401488de6e22 | 44 | if (error != BLE_ERROR_NONE) { |
Vincent Coubard |
2:401488de6e22 | 45 | /* In case of error, forward the error handling to onBleInitError */ |
Vincent Coubard |
2:401488de6e22 | 46 | onBleInitError(ble, error); |
Vincent Coubard |
2:401488de6e22 | 47 | return; |
Vincent Coubard |
2:401488de6e22 | 48 | } |
Vincent Coubard |
2:401488de6e22 | 49 | |
Vincent Coubard |
2:401488de6e22 | 50 | /* Ensure that it is the default instance of BLE */ |
Vincent Coubard |
2:401488de6e22 | 51 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
Vincent Coubard |
2:401488de6e22 | 52 | return; |
Vincent Coubard |
2:401488de6e22 | 53 | } |
Vincent Coubard |
2:401488de6e22 | 54 | |
Vincent Coubard |
2:401488de6e22 | 55 | /** |
Vincent Coubard |
2:401488de6e22 | 56 | * The Beacon payload has the following composition: |
Vincent Coubard |
2:401488de6e22 | 57 | * 128-Bit / 16byte UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 |
Vincent Coubard |
2:401488de6e22 | 58 | * Major/Minor = 0x1122 / 0x3344 |
Vincent Coubard |
2:401488de6e22 | 59 | * Tx Power = 0xC8 = 200, 2's compliment is 256-200 = (-56dB) |
Vincent Coubard |
2:401488de6e22 | 60 | * |
Vincent Coubard |
2:401488de6e22 | 61 | * Note: please remember to calibrate your beacons TX Power for more accurate results. |
Vincent Coubard |
2:401488de6e22 | 62 | */ |
Vincent Coubard |
2:401488de6e22 | 63 | static const uint8_t uuid[] = {0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, |
Vincent Coubard |
2:401488de6e22 | 64 | 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61}; |
Vincent Coubard |
2:401488de6e22 | 65 | uint16_t majorNumber = 1122; |
Vincent Coubard |
2:401488de6e22 | 66 | uint16_t minorNumber = 3344; |
Vincent Coubard |
2:401488de6e22 | 67 | uint16_t txPower = 0xC8; |
Vincent Coubard |
2:401488de6e22 | 68 | ibeaconPtr = new iBeacon(ble, uuid, majorNumber, minorNumber, txPower); |
Vincent Coubard |
2:401488de6e22 | 69 | |
Vincent Coubard |
2:401488de6e22 | 70 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
Vincent Coubard |
2:401488de6e22 | 71 | ble.gap().startAdvertising(); |
Vincent Coubard |
2:401488de6e22 | 72 | } |
Vincent Coubard |
2:401488de6e22 | 73 | |
Vincent Coubard |
2:401488de6e22 | 74 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
Vincent Coubard |
2:401488de6e22 | 75 | BLE &ble = BLE::Instance(); |
Vincent Coubard |
2:401488de6e22 | 76 | eventQueue.post(Callback<void()>(&ble, &BLE::processEvents)); |
Vincent Coubard |
2:401488de6e22 | 77 | } |
Vincent Coubard |
2:401488de6e22 | 78 | |
Vincent Coubard |
2:401488de6e22 | 79 | int main() |
Vincent Coubard |
2:401488de6e22 | 80 | { |
Vincent Coubard |
2:401488de6e22 | 81 | BLE &ble = BLE::Instance(); |
Vincent Coubard |
2:401488de6e22 | 82 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
Vincent Coubard |
2:401488de6e22 | 83 | ble.init(bleInitComplete); |
Vincent Coubard |
2:401488de6e22 | 84 | |
Vincent Coubard |
2:401488de6e22 | 85 | while (true) { |
Vincent Coubard |
2:401488de6e22 | 86 | eventQueue.dispatch(); |
Vincent Coubard |
2:401488de6e22 | 87 | } |
Vincent Coubard |
2:401488de6e22 | 88 | |
Vincent Coubard |
2:401488de6e22 | 89 | return 0; |
Vincent Coubard |
2:401488de6e22 | 90 | } |