This example creates a BLE beacon: a method of advertising a small amount of information to nearby devices. The information doesn't have to be human-readable; it can be in a format that only an application can use. Beacons are very easy to set up: the code for all beacons is the same, and only the information you want to advertise - the beacon payload - needs to change. he canonical source for this example lives at https://github.com/ARMmbed/mbed-os-example-ble/tree/master/BLE_Beacon

This example creates a BLE beacon: a method of advertising a small amount of information to nearby devices. The information doesn't have to be human-readable; it can be in a format that only an application can use.

Beacons are very easy to set up: the code for all beacons is the same, and only the information you want to advertise - the beacon payload - needs to change.

This example advertises a UUID, a major and minor number and the transmission strength. The major and minor numbers are an example of information that is not (normally) meaningful to humans, but that an application can use to identify the beacon and display related information. For example, if the major number is a store ID and the minor number is a location in that store, then a matching application can use these numbers to query a database and display location-specific information.

Running the application

Requirements

The sample application can be seen on any BLE scanner on a smartphone. If you don't have a scanner on your phone, please install :

- nRF Master Control Panel for Android.

- LightBlue for iPhone.

Hardware requirements are in the main readme.

Building instructions

Building with mbed CLI

If you'd like to use mbed CLI to build this, then you should refer to the main readme. The instructions here relate to using the developer.mbed.org Online Compiler

In order to build this example in the mbed Online Compiler, first import the example using the ‘Import’ button on the right hand side.

Next, select a platform to build for. This must either be a platform that supports BLE, for example the NRF51-DK, or one of the following:

List of platforms supporting Bluetooth Low Energy

Or you must also add a piece of hardware and the supporting library that includes a Bluetooth Low Energy driver for that hardware, for example the K64F or NUCLEO_F401RE with the X-NUCLEO-IDB05A1

List of components supporting Bluetooth Low Energy.

Once you have selected your platform, compile the example and drag and drop the resulting binary onto your board.

For general instructions on using the mbed Online Compiler, please see the mbed Handbook

Checking for success

Note: Screens captures depicted below show what is expected from this example if the scanner used is nRF Master Control Panel version 4.0.5. If you encounter any difficulties consider trying another scanner or another version of nRF Master Control Panel. Alternative scanners may require reference to their manuals.

  • Build the application and install it on your board as explained in the building instructions.
  • Open the BLE scanner on your phone.
  • Start a scan.

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-Beacon/raw-file/66b59f6860ed/img/start_scan.png

figure 1 How to start scan using nRF Master Control Panel 4.0.5

1. Find your device; it should be tagged as an `iBeacon` and observe its advertisements (there is no need to connect to the beacon).

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-Beacon/raw-file/66b59f6860ed/img/discovery.png

figure 2 Scan results using nRF Master Control Panel 4.0.5

  • View the beacon's details; the exact steps depend on which scanner you're using.

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-Beacon/raw-file/66b59f6860ed/img/beacon_details.png

figure 3 Beacon details using nRF Master Control Panel 4.0.5

Tip: If you are in an area with many BLE devices, it may be difficult to identify your beacon. The simplest solution is to turn your board off and on, initiate a new scan on your BLE scanner every time, and look for the beacon that appears only when your board is on.

If you can see the beacon and all its information, the application worked properly.

For more information, see the mbed Classic version of this application.

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 }