The Uniform Resource Identifier Beacon (UriBeacon) defines Bluetooth 4.0 Advertisement Data that contain Web URIs. URIBeacon provides a way for Bluetooth Low Energy devices to discover nearby URIs, for example, provides a way for a user to discover a short URL and then download it on their smartphone.
Dependencies: BLE_API mbed nRF51822
Fork of BLE_PhysicalWeb by
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "ble/BLE.h" 00019 #include "ble/services/URIBeaconConfigService.h" 00020 #include "ble/services/DFUService.h" 00021 #include "ble/services/DeviceInformationService.h" 00022 #include "ConfigParamsPersistence.h" 00023 00024 URIBeaconConfigService *uriBeaconConfig; 00025 00026 /** 00027 * URIBeaconConfig service can operate in two modes: a configuration mode which 00028 * allows a user to update settings over a connection; and normal URIBeacon mode 00029 * which involves advertising a URI. Constructing an object from URIBeaconConfig 00030 * service sets up advertisements for the configuration mode. It is then up to 00031 * the application to switch to URIBeacon mode based on some timeout. 00032 * 00033 * The following help with this switch. 00034 */ 00035 static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 60; // Duration after power-on that config service is available. 00036 Ticker configAdvertisementTimeoutTicker; 00037 00038 /** 00039 * Stop advertising the UriBeaconConfig Service after a delay; and switch to normal URIBeacon. 00040 */ 00041 void timeout(void) 00042 { 00043 Gap::GapState_t state; 00044 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); 00045 00046 state = ble.getGapState(); 00047 if (!state.connected) { /* don't switch if we're in a connected state. */ 00048 uriBeaconConfig->setupURIBeaconAdvertisements(); 00049 ble.startAdvertising(); 00050 00051 configAdvertisementTimeoutTicker.detach(); /* disable the callback from the timeout Ticker. */ 00052 } 00053 } 00054 00055 /** 00056 * Callback triggered upon a disconnection event. Needs to re-enable advertisements. 00057 */ 00058 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) 00059 { 00060 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); 00061 ble.startAdvertising(); 00062 } 00063 00064 void bleInitComplete(BLE::InitializationCompleteCallbackContext *context) 00065 { 00066 BLE &ble = context->ble; 00067 00068 if (context->error != BLE_ERROR_NONE) { 00069 return; 00070 } 00071 00072 ble.onDisconnection(disconnectionCallback); 00073 00074 /* 00075 * Load parameters from (platform specific) persistent storage. Parameters 00076 * can be set to non-default values while the URIBeacon is in configuration 00077 * mode (within the first 60 seconds of power-up). Thereafter, parameters 00078 * get copied out to persistent storage before switching to normal URIBeacon 00079 * operation. 00080 */ 00081 URIBeaconConfigService::Params_t params; 00082 bool fetchedFromPersistentStorage = loadURIBeaconConfigParams(¶ms); 00083 00084 /* Initialize a URIBeaconConfig service providing config params, default URI, and power levels. */ 00085 static URIBeaconConfigService::PowerLevels_t defaultAdvPowerLevels = {-20, -4, 0, 10}; // Values for ADV packets related to firmware levels 00086 uriBeaconConfig = new URIBeaconConfigService(ble, params, !fetchedFromPersistentStorage, "http://uribeacon.org", defaultAdvPowerLevels); 00087 if (!uriBeaconConfig->configuredSuccessfully()) { 00088 error("failed to accommodate URI"); 00089 } 00090 configAdvertisementTimeoutTicker.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS); 00091 00092 // Setup auxiliary services to allow over-the-air firmware updates, etc 00093 DFUService dfu(ble); 00094 DeviceInformationService deviceInfo(ble, "ARM", "UriBeacon", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); 00095 00096 ble.startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the URIBeaconConfig 00097 * service. This can then be switched to the normal URIBeacon functionality after a timeout. */ 00098 } 00099 00100 int main(void) 00101 { 00102 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); 00103 ble.init(bleInitComplete); 00104 00105 /* SpinWait for initialization to complete. This is necessary because the 00106 * BLE object is used in the main loop below. */ 00107 while (!ble.hasInitialized()) { /* spin loop */ } 00108 00109 while (true) { 00110 ble.waitForEvent(); 00111 } 00112 }
Generated on Sun Jul 17 2022 17:24:47 by 1.7.2