BLE beacon example

Committer:
Vincent Coubard
Date:
Tue Jul 26 14:35:32 2016 +0100
Revision:
0:66b59f6860ed
Child:
1:2fd54f1254fe
Update example at tag mbed-os-5.0.1-rc1

Who changed what in which revision?

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