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:
Vincent Coubard
Date:
Tue Jul 26 14:52:13 2016 +0100
Revision:
0:45a261c84d32
Child:
2:e075005e17d0
Update example at tag mbed-os-5.0.1-rc1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vincent Coubard 0:45a261c84d32 1 /* mbed Microcontroller Library
Vincent Coubard 0:45a261c84d32 2 * Copyright (c) 2006-2015 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 extern "C" {
Vincent Coubard 0:45a261c84d32 18 #include "pstorage.h"
Vincent Coubard 0:45a261c84d32 19 }
Vincent Coubard 0:45a261c84d32 20
Vincent Coubard 0:45a261c84d32 21 #include "nrf_error.h"
Vincent Coubard 0:45a261c84d32 22 #include "ConfigParamsPersistence.h"
Vincent Coubard 0:45a261c84d32 23
Vincent Coubard 0:45a261c84d32 24 /**
Vincent Coubard 0:45a261c84d32 25 * Nordic specific structure used to store params persistently.
Vincent Coubard 0:45a261c84d32 26 * It extends URIBeaconConfigService::Params_t with a persistence signature.
Vincent Coubard 0:45a261c84d32 27 */
Vincent Coubard 0:45a261c84d32 28 struct PersistentParams_t {
Vincent Coubard 0:45a261c84d32 29 URIBeaconConfigService::Params_t params;
Vincent Coubard 0:45a261c84d32 30 uint32_t persistenceSignature; /* This isn't really a parameter, but having the expected
Vincent Coubard 0:45a261c84d32 31 * magic value in this field indicates persistence. */
Vincent Coubard 0:45a261c84d32 32
Vincent Coubard 0:45a261c84d32 33 static const uint32_t MAGIC = 0x1BEAC000; /* Magic that identifies persistence */
Vincent Coubard 0:45a261c84d32 34 };
Vincent Coubard 0:45a261c84d32 35
Vincent Coubard 0:45a261c84d32 36 /**
Vincent Coubard 0:45a261c84d32 37 * The following is a module-local variable to hold configuration parameters for
Vincent Coubard 0:45a261c84d32 38 * short periods during flash access. This is necessary because the pstorage
Vincent Coubard 0:45a261c84d32 39 * APIs don't copy in the memory provided as data source. The memory cannot be
Vincent Coubard 0:45a261c84d32 40 * freed or reused by the application until this flash access is complete. The
Vincent Coubard 0:45a261c84d32 41 * load and store operations in this module initialize persistentParams and then
Vincent Coubard 0:45a261c84d32 42 * pass it on to the 'pstorage' APIs.
Vincent Coubard 0:45a261c84d32 43 */
Vincent Coubard 0:45a261c84d32 44 static PersistentParams_t persistentParams;
Vincent Coubard 0:45a261c84d32 45
Vincent Coubard 0:45a261c84d32 46 static pstorage_handle_t pstorageHandle;
Vincent Coubard 0:45a261c84d32 47
Vincent Coubard 0:45a261c84d32 48 /**
Vincent Coubard 0:45a261c84d32 49 * Dummy callback handler needed by Nordic's pstorage module. This is called
Vincent Coubard 0:45a261c84d32 50 * after every flash access.
Vincent Coubard 0:45a261c84d32 51 */
Vincent Coubard 0:45a261c84d32 52 static void pstorageNotificationCallback(pstorage_handle_t *p_handle,
Vincent Coubard 0:45a261c84d32 53 uint8_t op_code,
Vincent Coubard 0:45a261c84d32 54 uint32_t result,
Vincent Coubard 0:45a261c84d32 55 uint8_t *p_data,
Vincent Coubard 0:45a261c84d32 56 uint32_t data_len)
Vincent Coubard 0:45a261c84d32 57 {
Vincent Coubard 0:45a261c84d32 58 /* APP_ERROR_CHECK(result); */
Vincent Coubard 0:45a261c84d32 59 }
Vincent Coubard 0:45a261c84d32 60
Vincent Coubard 0:45a261c84d32 61 /* Platform-specific implementation for persistence on the nRF5x. Based on the
Vincent Coubard 0:45a261c84d32 62 * pstorage module provided by the Nordic SDK. */
Vincent Coubard 0:45a261c84d32 63 bool loadURIBeaconConfigParams(URIBeaconConfigService::Params_t *paramsP)
Vincent Coubard 0:45a261c84d32 64 {
Vincent Coubard 0:45a261c84d32 65 static bool pstorageInitied = false;
Vincent Coubard 0:45a261c84d32 66 if (!pstorageInitied) {
Vincent Coubard 0:45a261c84d32 67 pstorage_init();
Vincent Coubard 0:45a261c84d32 68
Vincent Coubard 0:45a261c84d32 69 static pstorage_module_param_t pstorageParams = {
Vincent Coubard 0:45a261c84d32 70 .cb = pstorageNotificationCallback,
Vincent Coubard 0:45a261c84d32 71 .block_size = sizeof(PersistentParams_t),
Vincent Coubard 0:45a261c84d32 72 .block_count = 1
Vincent Coubard 0:45a261c84d32 73 };
Vincent Coubard 0:45a261c84d32 74 pstorage_register(&pstorageParams, &pstorageHandle);
Vincent Coubard 0:45a261c84d32 75 pstorageInitied = true;
Vincent Coubard 0:45a261c84d32 76 }
Vincent Coubard 0:45a261c84d32 77
Vincent Coubard 0:45a261c84d32 78 if ((pstorage_load(reinterpret_cast<uint8_t *>(&persistentParams), &pstorageHandle, sizeof(PersistentParams_t), 0) != NRF_SUCCESS) ||
Vincent Coubard 0:45a261c84d32 79 (persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) {
Vincent Coubard 0:45a261c84d32 80 // On failure zero out and let the service reset to defaults
Vincent Coubard 0:45a261c84d32 81 memset(paramsP, 0, sizeof(URIBeaconConfigService::Params_t));
Vincent Coubard 0:45a261c84d32 82 return false;
Vincent Coubard 0:45a261c84d32 83 }
Vincent Coubard 0:45a261c84d32 84
Vincent Coubard 0:45a261c84d32 85 memcpy(paramsP, &persistentParams.params, sizeof(URIBeaconConfigService::Params_t));
Vincent Coubard 0:45a261c84d32 86 return true;
Vincent Coubard 0:45a261c84d32 87 }
Vincent Coubard 0:45a261c84d32 88
Vincent Coubard 0:45a261c84d32 89 /* Platform-specific implementation for persistence on the nRF5x. Based on the
Vincent Coubard 0:45a261c84d32 90 * pstorage module provided by the Nordic SDK. */
Vincent Coubard 0:45a261c84d32 91 void saveURIBeaconConfigParams(const URIBeaconConfigService::Params_t *paramsP)
Vincent Coubard 0:45a261c84d32 92 {
Vincent Coubard 0:45a261c84d32 93 memcpy(&persistentParams.params, paramsP, sizeof(URIBeaconConfigService::Params_t));
Vincent Coubard 0:45a261c84d32 94 if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) {
Vincent Coubard 0:45a261c84d32 95 persistentParams.persistenceSignature = PersistentParams_t::MAGIC;
Vincent Coubard 0:45a261c84d32 96 pstorage_store(&pstorageHandle,
Vincent Coubard 0:45a261c84d32 97 reinterpret_cast<uint8_t *>(&persistentParams),
Vincent Coubard 0:45a261c84d32 98 sizeof(PersistentParams_t),
Vincent Coubard 0:45a261c84d32 99 0 /* offset */);
Vincent Coubard 0:45a261c84d32 100 } else {
Vincent Coubard 0:45a261c84d32 101 pstorage_update(&pstorageHandle,
Vincent Coubard 0:45a261c84d32 102 reinterpret_cast<uint8_t *>(&persistentParams),
Vincent Coubard 0:45a261c84d32 103 sizeof(PersistentParams_t),
Vincent Coubard 0:45a261c84d32 104 0 /* offset */);
Vincent Coubard 0:45a261c84d32 105 }
Vincent Coubard 0:45a261c84d32 106 }