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:

- Android version

- iOS version

- 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.

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-URIBeacon/raw-file/45a261c84d32/img/app_start.png

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.

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-URIBeacon/raw-file/45a261c84d32/img/open_configuration.png

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.

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-URIBeacon/raw-file/45a261c84d32/img/edit_url.png

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.

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-URIBeacon/raw-file/45a261c84d32/img/save_url.png

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.

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-URIBeacon/raw-file/45a261c84d32/img/result.png

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.

Committer:
mbed_official
Date:
Wed Mar 01 18:15:53 2017 +0000
Revision:
20:aba6206ee5d9
Parent:
11:23f03970d2cd
Merge pull request #55 from adbridge/master

Updating mbed-os to mbed-os-5.3.6
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-ble

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 2:e075005e17d0 1 /* mbed Microcontroller Library
mbed_official 2:e075005e17d0 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 2:e075005e17d0 3 *
mbed_official 2:e075005e17d0 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 2:e075005e17d0 5 * you may not use this file except in compliance with the License.
mbed_official 2:e075005e17d0 6 * You may obtain a copy of the License at
mbed_official 2:e075005e17d0 7 *
mbed_official 2:e075005e17d0 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 2:e075005e17d0 9 *
mbed_official 2:e075005e17d0 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 2:e075005e17d0 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 2:e075005e17d0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 2:e075005e17d0 13 * See the License for the specific language governing permissions and
mbed_official 2:e075005e17d0 14 * limitations under the License.
mbed_official 2:e075005e17d0 15 */
mbed_official 2:e075005e17d0 16
mbed_official 11:23f03970d2cd 17 #include <events/mbed_events.h>
mbed_official 2:e075005e17d0 18 #include "mbed.h"
mbed_official 2:e075005e17d0 19 #include "ble/BLE.h"
mbed_official 2:e075005e17d0 20 #include "ble/services/URIBeaconConfigService.h"
mbed_official 2:e075005e17d0 21 #include "ble/services/DFUService.h"
mbed_official 2:e075005e17d0 22 #include "ble/services/DeviceInformationService.h"
mbed_official 2:e075005e17d0 23 #include "ConfigParamsPersistence.h"
mbed_official 2:e075005e17d0 24
mbed_official 2:e075005e17d0 25 /**
mbed_official 2:e075005e17d0 26 * URIBeaconConfig service can operate in two modes: a configuration mode which
mbed_official 2:e075005e17d0 27 * allows a user to update settings over a connection; and normal URIBeacon mode
mbed_official 2:e075005e17d0 28 * which involves advertising a URI. Constructing an object from URIBeaconConfig
mbed_official 2:e075005e17d0 29 * service sets up advertisements for the configuration mode. It is then up to
mbed_official 2:e075005e17d0 30 * the application to switch to URIBeacon mode based on some timeout.
mbed_official 2:e075005e17d0 31 *
mbed_official 2:e075005e17d0 32 * The following help with this switch.
mbed_official 2:e075005e17d0 33 */
mbed_official 2:e075005e17d0 34 static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 60; // Duration after power-on that config service is available.
mbed_official 2:e075005e17d0 35
mbed_official 2:e075005e17d0 36 static EventQueue eventQueue(
mbed_official 2:e075005e17d0 37 /* event count */ 16 * /* event size */ 32
mbed_official 2:e075005e17d0 38 );
mbed_official 2:e075005e17d0 39
mbed_official 2:e075005e17d0 40 /* global static objects */
mbed_official 2:e075005e17d0 41 BLE ble;
mbed_official 2:e075005e17d0 42 URIBeaconConfigService *uriBeaconConfig;
mbed_official 2:e075005e17d0 43 URIBeaconConfigService::Params_t params;
mbed_official 2:e075005e17d0 44
mbed_official 2:e075005e17d0 45 /**
mbed_official 2:e075005e17d0 46 * Stop advertising the UriBeaconConfig Service after a delay; and switch to normal URIBeacon.
mbed_official 2:e075005e17d0 47 */
mbed_official 2:e075005e17d0 48 void timeout(void)
mbed_official 2:e075005e17d0 49 {
mbed_official 2:e075005e17d0 50 Gap::GapState_t state;
mbed_official 2:e075005e17d0 51 state = ble.getGapState();
mbed_official 2:e075005e17d0 52 if (!state.connected) { /* don't switch if we're in a connected state. */
mbed_official 2:e075005e17d0 53 uriBeaconConfig->setupURIBeaconAdvertisements();
mbed_official 2:e075005e17d0 54 ble.startAdvertising();
mbed_official 2:e075005e17d0 55 } else {
mbed_official 11:23f03970d2cd 56 eventQueue.call_in(CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS * 1000, timeout);
mbed_official 2:e075005e17d0 57 }
mbed_official 2:e075005e17d0 58 }
mbed_official 2:e075005e17d0 59
mbed_official 2:e075005e17d0 60 /**
mbed_official 2:e075005e17d0 61 * Callback triggered upon a disconnection event. Needs to re-enable advertisements.
mbed_official 2:e075005e17d0 62 */
mbed_official 2:e075005e17d0 63 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
mbed_official 2:e075005e17d0 64 {
mbed_official 2:e075005e17d0 65 ble.startAdvertising();
mbed_official 2:e075005e17d0 66 }
mbed_official 2:e075005e17d0 67
mbed_official 2:e075005e17d0 68 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
mbed_official 2:e075005e17d0 69 BLE &ble = BLE::Instance();
mbed_official 11:23f03970d2cd 70 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
mbed_official 2:e075005e17d0 71 }
mbed_official 2:e075005e17d0 72
mbed_official 2:e075005e17d0 73 int main()
mbed_official 2:e075005e17d0 74 {
mbed_official 2:e075005e17d0 75 ble.onEventsToProcess(scheduleBleEventsProcessing);
mbed_official 2:e075005e17d0 76 ble.init();
mbed_official 2:e075005e17d0 77 ble.onDisconnection(disconnectionCallback);
mbed_official 2:e075005e17d0 78
mbed_official 2:e075005e17d0 79 /*
mbed_official 2:e075005e17d0 80 * Load parameters from (platform specific) persistent storage. Parameters
mbed_official 2:e075005e17d0 81 * can be set to non-default values while the URIBeacon is in configuration
mbed_official 2:e075005e17d0 82 * mode (within the first 60 seconds of power-up). Thereafter, parameters
mbed_official 2:e075005e17d0 83 * get copied out to persistent storage before switching to normal URIBeacon
mbed_official 2:e075005e17d0 84 * operation.
mbed_official 2:e075005e17d0 85 */
mbed_official 2:e075005e17d0 86 bool fetchedFromPersistentStorage = loadURIBeaconConfigParams(&params);
mbed_official 2:e075005e17d0 87
mbed_official 2:e075005e17d0 88 /* Initialize a URIBeaconConfig service providing config params, default URI, and power levels. */
mbed_official 2:e075005e17d0 89 static URIBeaconConfigService::PowerLevels_t defaultAdvPowerLevels = {-20, -4, 0, 10}; // Values for ADV packets related to firmware levels
mbed_official 2:e075005e17d0 90 uriBeaconConfig = new URIBeaconConfigService(ble, params, !fetchedFromPersistentStorage, "http://uribeacon.org", defaultAdvPowerLevels);
mbed_official 2:e075005e17d0 91 if (!uriBeaconConfig->configuredSuccessfully()) {
mbed_official 2:e075005e17d0 92 error("failed to accommodate URI");
mbed_official 2:e075005e17d0 93 }
mbed_official 2:e075005e17d0 94
mbed_official 2:e075005e17d0 95 // Setup auxiliary services to allow over-the-air firmware updates, etc
mbed_official 2:e075005e17d0 96 DFUService *dfu = new DFUService(ble);
mbed_official 2:e075005e17d0 97 DeviceInformationService *deviceInfo = new DeviceInformationService(ble, "ARM", "UriBeacon", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
mbed_official 2:e075005e17d0 98
mbed_official 2:e075005e17d0 99 ble.startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the URIBeaconConfig
mbed_official 2:e075005e17d0 100 * service. This can then be switched to the normal URIBeacon functionality after a timeout. */
mbed_official 2:e075005e17d0 101
mbed_official 11:23f03970d2cd 102 eventQueue.call_in(CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS * 1000, timeout);
mbed_official 2:e075005e17d0 103
mbed_official 11:23f03970d2cd 104 eventQueue.dispatch_forever();
mbed_official 2:e075005e17d0 105
mbed_official 2:e075005e17d0 106 return 0;
mbed_official 2:e075005e17d0 107 }