URI-Beacons are handy when there is a need to advertise a small amount of information (usually a URL) to any nearby device. They’re really easy to set up: the code is fully available on the mbed website, so all you’ll need to do is tell the beacon what to broadcast. The canonical source for this example lives at https://github.com/ARMmbed/mbed-os-example-ble/tree/master/BLE_URIBeacon
URI-Beacons are handy when there is a need to advertise a small amount of information (usually a URL) to any nearby device. They’re really easy to set up: the code is fully available on the mbed website, so all you’ll need to do is tell the beacon what to broadcast.
Technical details are better presented here, which happens to be the mbed-classic equivalent of this example. Please also refer to Google's URIBeacon project.
What You’ll Need
To get this going, you’ll need:
- To see URIBeacons get the *Physical Web* app installed on your phone:
- One of the BLE platforms listed in the README.md of this repository, for example a Nordic DK board.
Build 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
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
- Build the application and install it on your board as explained in the building instructions.
- Open the *Physical Web* application on your phone. It will start to search for nearby beacons.
figure 1 Start of the *Physical Web* application version 0.1.856 on Android
- When the beacon starts up, the Configuration Service runs for 60 seconds. During this time it is possible to change the URL advertised by the beacon. It is also important to note that during these 60 seconds, your device will not advertise any URL.
figure 2 How to open the beacon configuration view using the *Physical Web* application version 0.1.856 on Android
- Edit the URL advertised by your beacon.
figure 3 How to edit the URL advertised by your beacon using the *Physical Web* application version 0.1.856 on Android
- Save the URL which will be advertised by your beacon.
figure 4 How to save your beacon configuration and start advertising URL using the *Physical Web* application version 0.1.856 on Android.
- Find your device; it should advertise the URL you have set.
figure 5 Display of URL advertised by your beacon using the *Physical Web* application version 0.1.856 on Android.
Please note that the URIBeacon spec requires the URIBeacon app to remain in config mode for the first 60 seconds before switching to being a beacon. So if you're using a physical-web app, you'll only see the beacon after this period; if you're using one of the generic apps for BLE scanning, you should see a configurable beacon being advertised for the first 60 seconds.
You'll find links on Google's project page to client apps to test URIBeacon. Here's a link that should get you an Android App; please browse to `uribeacon-sample-release.apk`. But you should begin with the links to android apps mentioned above.
source/main.cpp@1:7b3d36c9808f, 2016-07-28 (annotated)
- Committer:
- Vincent Coubard
- Date:
- Thu Jul 28 23:08:59 2016 +0100
- Revision:
- 1:7b3d36c9808f
- Parent:
- 0:45a261c84d32
- Child:
- 2:e075005e17d0
Sync with mbed-os-5.1.0-rc3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vincent Coubard |
0:45a261c84d32 | 1 | /* mbed Microcontroller Library |
Vincent Coubard |
0:45a261c84d32 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Vincent Coubard |
0:45a261c84d32 | 3 | * |
Vincent Coubard |
0:45a261c84d32 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Vincent Coubard |
0:45a261c84d32 | 5 | * you may not use this file except in compliance with the License. |
Vincent Coubard |
0:45a261c84d32 | 6 | * You may obtain a copy of the License at |
Vincent Coubard |
0:45a261c84d32 | 7 | * |
Vincent Coubard |
0:45a261c84d32 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Vincent Coubard |
0:45a261c84d32 | 9 | * |
Vincent Coubard |
0:45a261c84d32 | 10 | * Unless required by applicable law or agreed to in writing, software |
Vincent Coubard |
0:45a261c84d32 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Vincent Coubard |
0:45a261c84d32 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Vincent Coubard |
0:45a261c84d32 | 13 | * See the License for the specific language governing permissions and |
Vincent Coubard |
0:45a261c84d32 | 14 | * limitations under the License. |
Vincent Coubard |
0:45a261c84d32 | 15 | */ |
Vincent Coubard |
0:45a261c84d32 | 16 | |
Vincent Coubard |
0:45a261c84d32 | 17 | #include <mbed-events/events.h> |
Vincent Coubard |
0:45a261c84d32 | 18 | #include "mbed.h" |
Vincent Coubard |
0:45a261c84d32 | 19 | #include "ble/BLE.h" |
Vincent Coubard |
0:45a261c84d32 | 20 | #include "ble/services/URIBeaconConfigService.h" |
Vincent Coubard |
0:45a261c84d32 | 21 | #include "ble/services/DFUService.h" |
Vincent Coubard |
0:45a261c84d32 | 22 | #include "ble/services/DeviceInformationService.h" |
Vincent Coubard |
0:45a261c84d32 | 23 | #include "ConfigParamsPersistence.h" |
Vincent Coubard |
0:45a261c84d32 | 24 | |
Vincent Coubard |
0:45a261c84d32 | 25 | /** |
Vincent Coubard |
0:45a261c84d32 | 26 | * URIBeaconConfig service can operate in two modes: a configuration mode which |
Vincent Coubard |
0:45a261c84d32 | 27 | * allows a user to update settings over a connection; and normal URIBeacon mode |
Vincent Coubard |
0:45a261c84d32 | 28 | * which involves advertising a URI. Constructing an object from URIBeaconConfig |
Vincent Coubard |
0:45a261c84d32 | 29 | * service sets up advertisements for the configuration mode. It is then up to |
Vincent Coubard |
0:45a261c84d32 | 30 | * the application to switch to URIBeacon mode based on some timeout. |
Vincent Coubard |
0:45a261c84d32 | 31 | * |
Vincent Coubard |
0:45a261c84d32 | 32 | * The following help with this switch. |
Vincent Coubard |
0:45a261c84d32 | 33 | */ |
Vincent Coubard |
0:45a261c84d32 | 34 | static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 60; // Duration after power-on that config service is available. |
Vincent Coubard |
0:45a261c84d32 | 35 | |
Vincent Coubard |
0:45a261c84d32 | 36 | static EventQueue eventQueue( |
Vincent Coubard |
1:7b3d36c9808f | 37 | /* event count */ 16 * /* event size */ 32 |
Vincent Coubard |
0:45a261c84d32 | 38 | ); |
Vincent Coubard |
0:45a261c84d32 | 39 | |
Vincent Coubard |
0:45a261c84d32 | 40 | /* global static objects */ |
Vincent Coubard |
0:45a261c84d32 | 41 | BLE ble; |
Vincent Coubard |
0:45a261c84d32 | 42 | URIBeaconConfigService *uriBeaconConfig; |
Vincent Coubard |
0:45a261c84d32 | 43 | URIBeaconConfigService::Params_t params; |
Vincent Coubard |
0:45a261c84d32 | 44 | |
Vincent Coubard |
0:45a261c84d32 | 45 | /** |
Vincent Coubard |
0:45a261c84d32 | 46 | * Stop advertising the UriBeaconConfig Service after a delay; and switch to normal URIBeacon. |
Vincent Coubard |
0:45a261c84d32 | 47 | */ |
Vincent Coubard |
0:45a261c84d32 | 48 | void timeout(void) |
Vincent Coubard |
0:45a261c84d32 | 49 | { |
Vincent Coubard |
0:45a261c84d32 | 50 | Gap::GapState_t state; |
Vincent Coubard |
0:45a261c84d32 | 51 | state = ble.getGapState(); |
Vincent Coubard |
0:45a261c84d32 | 52 | if (!state.connected) { /* don't switch if we're in a connected state. */ |
Vincent Coubard |
0:45a261c84d32 | 53 | uriBeaconConfig->setupURIBeaconAdvertisements(); |
Vincent Coubard |
0:45a261c84d32 | 54 | ble.startAdvertising(); |
Vincent Coubard |
0:45a261c84d32 | 55 | } else { |
Vincent Coubard |
1:7b3d36c9808f | 56 | eventQueue.post_in(CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS * 1000, timeout); |
Vincent Coubard |
0:45a261c84d32 | 57 | } |
Vincent Coubard |
0:45a261c84d32 | 58 | } |
Vincent Coubard |
0:45a261c84d32 | 59 | |
Vincent Coubard |
0:45a261c84d32 | 60 | /** |
Vincent Coubard |
0:45a261c84d32 | 61 | * Callback triggered upon a disconnection event. Needs to re-enable advertisements. |
Vincent Coubard |
0:45a261c84d32 | 62 | */ |
Vincent Coubard |
0:45a261c84d32 | 63 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) |
Vincent Coubard |
0:45a261c84d32 | 64 | { |
Vincent Coubard |
0:45a261c84d32 | 65 | ble.startAdvertising(); |
Vincent Coubard |
0:45a261c84d32 | 66 | } |
Vincent Coubard |
0:45a261c84d32 | 67 | |
Vincent Coubard |
0:45a261c84d32 | 68 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
Vincent Coubard |
0:45a261c84d32 | 69 | BLE &ble = BLE::Instance(); |
Vincent Coubard |
0:45a261c84d32 | 70 | eventQueue.post(Callback<void()>(&ble, &BLE::processEvents)); |
Vincent Coubard |
0:45a261c84d32 | 71 | } |
Vincent Coubard |
0:45a261c84d32 | 72 | |
Vincent Coubard |
0:45a261c84d32 | 73 | int main() |
Vincent Coubard |
0:45a261c84d32 | 74 | { |
Vincent Coubard |
0:45a261c84d32 | 75 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
Vincent Coubard |
0:45a261c84d32 | 76 | ble.init(); |
Vincent Coubard |
0:45a261c84d32 | 77 | ble.onDisconnection(disconnectionCallback); |
Vincent Coubard |
0:45a261c84d32 | 78 | |
Vincent Coubard |
0:45a261c84d32 | 79 | /* |
Vincent Coubard |
0:45a261c84d32 | 80 | * Load parameters from (platform specific) persistent storage. Parameters |
Vincent Coubard |
0:45a261c84d32 | 81 | * can be set to non-default values while the URIBeacon is in configuration |
Vincent Coubard |
0:45a261c84d32 | 82 | * mode (within the first 60 seconds of power-up). Thereafter, parameters |
Vincent Coubard |
0:45a261c84d32 | 83 | * get copied out to persistent storage before switching to normal URIBeacon |
Vincent Coubard |
0:45a261c84d32 | 84 | * operation. |
Vincent Coubard |
0:45a261c84d32 | 85 | */ |
Vincent Coubard |
0:45a261c84d32 | 86 | bool fetchedFromPersistentStorage = loadURIBeaconConfigParams(¶ms); |
Vincent Coubard |
0:45a261c84d32 | 87 | |
Vincent Coubard |
0:45a261c84d32 | 88 | /* Initialize a URIBeaconConfig service providing config params, default URI, and power levels. */ |
Vincent Coubard |
0:45a261c84d32 | 89 | static URIBeaconConfigService::PowerLevels_t defaultAdvPowerLevels = {-20, -4, 0, 10}; // Values for ADV packets related to firmware levels |
Vincent Coubard |
0:45a261c84d32 | 90 | uriBeaconConfig = new URIBeaconConfigService(ble, params, !fetchedFromPersistentStorage, "http://uribeacon.org", defaultAdvPowerLevels); |
Vincent Coubard |
0:45a261c84d32 | 91 | if (!uriBeaconConfig->configuredSuccessfully()) { |
Vincent Coubard |
0:45a261c84d32 | 92 | error("failed to accommodate URI"); |
Vincent Coubard |
0:45a261c84d32 | 93 | } |
Vincent Coubard |
0:45a261c84d32 | 94 | |
Vincent Coubard |
0:45a261c84d32 | 95 | // Setup auxiliary services to allow over-the-air firmware updates, etc |
Vincent Coubard |
0:45a261c84d32 | 96 | DFUService *dfu = new DFUService(ble); |
Vincent Coubard |
0:45a261c84d32 | 97 | DeviceInformationService *deviceInfo = new DeviceInformationService(ble, "ARM", "UriBeacon", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
Vincent Coubard |
0:45a261c84d32 | 98 | |
Vincent Coubard |
0:45a261c84d32 | 99 | ble.startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the URIBeaconConfig |
Vincent Coubard |
0:45a261c84d32 | 100 | * service. This can then be switched to the normal URIBeacon functionality after a timeout. */ |
Vincent Coubard |
0:45a261c84d32 | 101 | |
Vincent Coubard |
1:7b3d36c9808f | 102 | eventQueue.post_in(CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS * 1000, timeout); |
Vincent Coubard |
0:45a261c84d32 | 103 | |
Vincent Coubard |
0:45a261c84d32 | 104 | while (true) { |
Vincent Coubard |
0:45a261c84d32 | 105 | eventQueue.dispatch(); |
Vincent Coubard |
0:45a261c84d32 | 106 | } |
Vincent Coubard |
0:45a261c84d32 | 107 | |
Vincent Coubard |
0:45a261c84d32 | 108 | return 0; |
Vincent Coubard |
0:45a261c84d32 | 109 | } |