add iBeacon functionality

Dependencies:   BLE_API mbed-dev-bin nRF51822

Fork of microbit-dal by Ken Ogami

Committer:
bluetooth_kyo
Date:
Mon Oct 16 18:37:57 2017 +0000
Revision:
81:5691aae23431
Parent:
80:1d4526d816a8
Parent:
79:e4e6ab1c9835
merge changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jonathan Austin 1:8aa5cdb4ab67 1 /*
Jonathan Austin 1:8aa5cdb4ab67 2 The MIT License (MIT)
Jonathan Austin 1:8aa5cdb4ab67 3
Jonathan Austin 1:8aa5cdb4ab67 4 Copyright (c) 2016 British Broadcasting Corporation.
Jonathan Austin 1:8aa5cdb4ab67 5 This software is provided by Lancaster University by arrangement with the BBC.
Jonathan Austin 1:8aa5cdb4ab67 6
Jonathan Austin 1:8aa5cdb4ab67 7 Permission is hereby granted, free of charge, to any person obtaining a
Jonathan Austin 1:8aa5cdb4ab67 8 copy of this software and associated documentation files (the "Software"),
Jonathan Austin 1:8aa5cdb4ab67 9 to deal in the Software without restriction, including without limitation
Jonathan Austin 1:8aa5cdb4ab67 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
Jonathan Austin 1:8aa5cdb4ab67 11 and/or sell copies of the Software, and to permit persons to whom the
Jonathan Austin 1:8aa5cdb4ab67 12 Software is furnished to do so, subject to the following conditions:
Jonathan Austin 1:8aa5cdb4ab67 13
Jonathan Austin 1:8aa5cdb4ab67 14 The above copyright notice and this permission notice shall be included in
Jonathan Austin 1:8aa5cdb4ab67 15 all copies or substantial portions of the Software.
Jonathan Austin 1:8aa5cdb4ab67 16
Jonathan Austin 1:8aa5cdb4ab67 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Jonathan Austin 1:8aa5cdb4ab67 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Jonathan Austin 1:8aa5cdb4ab67 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
Jonathan Austin 1:8aa5cdb4ab67 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Jonathan Austin 1:8aa5cdb4ab67 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Jonathan Austin 1:8aa5cdb4ab67 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Jonathan Austin 1:8aa5cdb4ab67 23 DEALINGS IN THE SOFTWARE.
Jonathan Austin 1:8aa5cdb4ab67 24 */
Jonathan Austin 1:8aa5cdb4ab67 25
wwbluetooth 75:739b6a1c1b50 26
wwbluetooth 75:739b6a1c1b50 27
Jonathan Austin 1:8aa5cdb4ab67 28 #include "MicroBitConfig.h"
Jonathan Austin 1:8aa5cdb4ab67 29 #include "MicroBitBLEManager.h"
bluetooth_mdw 74:a8f5674a0079 30 #include "MicroBitEddystone.h"
Jonathan Austin 1:8aa5cdb4ab67 31 #include "MicroBitStorage.h"
Jonathan Austin 1:8aa5cdb4ab67 32 #include "MicroBitFiber.h"
bluetooth_mdw 74:a8f5674a0079 33 #include "MicroBitSystemTimer.h"
bluetooth_kyo 80:1d4526d816a8 34 #include "MicroBitIBeacon.h"
Jonathan Austin 1:8aa5cdb4ab67 35
Jonathan Austin 1:8aa5cdb4ab67 36 /* The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ.
Jonathan Austin 1:8aa5cdb4ab67 37 * If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL)
Jonathan Austin 1:8aa5cdb4ab67 38 * The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this
Jonathan Austin 1:8aa5cdb4ab67 39 * as a compatability option, but does not support the options used...
Jonathan Austin 1:8aa5cdb4ab67 40 */
Jonathan Austin 1:8aa5cdb4ab67 41 #if !defined(__arm)
Jonathan Austin 1:8aa5cdb4ab67 42 #pragma GCC diagnostic ignored "-Wunused-function"
Jonathan Austin 1:8aa5cdb4ab67 43 #pragma GCC diagnostic push
Jonathan Austin 1:8aa5cdb4ab67 44 #pragma GCC diagnostic ignored "-Wunused-parameter"
Jonathan Austin 1:8aa5cdb4ab67 45 #endif
Jonathan Austin 1:8aa5cdb4ab67 46
Jonathan Austin 1:8aa5cdb4ab67 47 #include "ble.h"
Jonathan Austin 1:8aa5cdb4ab67 48
bluetooth_mdw 74:a8f5674a0079 49 extern "C" {
Jonathan Austin 1:8aa5cdb4ab67 50 #include "device_manager.h"
Jonathan Austin 1:8aa5cdb4ab67 51 uint32_t btle_set_gatt_table_size(uint32_t size);
Jonathan Austin 1:8aa5cdb4ab67 52 }
Jonathan Austin 1:8aa5cdb4ab67 53
Jonathan Austin 1:8aa5cdb4ab67 54 /*
Jonathan Austin 1:8aa5cdb4ab67 55 * Return to our predefined compiler settings.
Jonathan Austin 1:8aa5cdb4ab67 56 */
Jonathan Austin 1:8aa5cdb4ab67 57 #if !defined(__arm)
Jonathan Austin 1:8aa5cdb4ab67 58 #pragma GCC diagnostic pop
Jonathan Austin 1:8aa5cdb4ab67 59 #endif
Jonathan Austin 1:8aa5cdb4ab67 60
bluetooth_mdw 74:a8f5674a0079 61 #define MICROBIT_PAIRING_FADE_SPEED 4
Jonathan Austin 1:8aa5cdb4ab67 62
bluetooth_mdw 74:a8f5674a0079 63 // Some Black Magic to compare the definition of our security mode in MicroBitConfig with a given parameter.
bluetooth_mdw 74:a8f5674a0079 64 // Required as the MicroBitConfig option is actually an mbed enum, that is not normally comparable at compile time.
bluetooth_mdw 74:a8f5674a0079 65 //
bluetooth_mdw 74:a8f5674a0079 66
bluetooth_mdw 74:a8f5674a0079 67 #define __CAT(a, ...) a##__VA_ARGS__
wwbluetooth 75:739b6a1c1b50 68
bluetooth_mdw 74:a8f5674a0079 69
bluetooth_mdw 74:a8f5674a0079 70 const char *MICROBIT_BLE_MANUFACTURER = NULL;
bluetooth_mdw 74:a8f5674a0079 71 const char *MICROBIT_BLE_MODEL = "BBC micro:bit";
bluetooth_mdw 74:a8f5674a0079 72 const char *MICROBIT_BLE_HARDWARE_VERSION = NULL;
bluetooth_mdw 74:a8f5674a0079 73 const char *MICROBIT_BLE_FIRMWARE_VERSION = MICROBIT_DAL_VERSION;
bluetooth_mdw 74:a8f5674a0079 74 const char *MICROBIT_BLE_SOFTWARE_VERSION = NULL;
Jonathan Austin 1:8aa5cdb4ab67 75 const int8_t MICROBIT_BLE_POWER_LEVEL[] = {-30, -20, -16, -12, -8, -4, 0, 4};
Jonathan Austin 1:8aa5cdb4ab67 76
Jonathan Austin 1:8aa5cdb4ab67 77 /*
Jonathan Austin 1:8aa5cdb4ab67 78 * Many of the mbed interfaces we need to use only support callbacks to plain C functions, rather than C++ methods.
Jonathan Austin 1:8aa5cdb4ab67 79 * So, we maintain a pointer to the MicroBitBLEManager that's in use. Ths way, we can still access resources on the micro:bit
Jonathan Austin 1:8aa5cdb4ab67 80 * whilst keeping the code modular.
Jonathan Austin 1:8aa5cdb4ab67 81 */
bluetooth_mdw 74:a8f5674a0079 82 MicroBitBLEManager *MicroBitBLEManager::manager = NULL; // Singleton reference to the BLE manager. many mbed BLE API callbacks still do not support member funcions yet. :-(
bluetooth_mdw 74:a8f5674a0079 83
Jonathan Austin 1:8aa5cdb4ab67 84
Jonathan Austin 1:8aa5cdb4ab67 85
Jonathan Austin 1:8aa5cdb4ab67 86 /**
Jonathan Austin 1:8aa5cdb4ab67 87 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 88 * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack.
Jonathan Austin 1:8aa5cdb4ab67 89 * @param _storage an instance of MicroBitStorage used to persist sys attribute information. (This is required for compatability with iOS).
Jonathan Austin 1:8aa5cdb4ab67 90 * @note The BLE stack *cannot* be brought up in a static context (the software simply hangs or corrupts itself).
Jonathan Austin 1:8aa5cdb4ab67 91 * Hence, the init() member function should be used to initialise the BLE stack.
Jonathan Austin 1:8aa5cdb4ab67 92 */
bluetooth_mdw 74:a8f5674a0079 93 MicroBitBLEManager::MicroBitBLEManager(MicroBitStorage &_storage) : storage(&_storage)
Jonathan Austin 1:8aa5cdb4ab67 94 {
Jonathan Austin 1:8aa5cdb4ab67 95 manager = this;
bluetooth_mdw 74:a8f5674a0079 96 this->ble = NULL;
bluetooth_mdw 74:a8f5674a0079 97 this->pairingStatus = 0;
Jonathan Austin 1:8aa5cdb4ab67 98 }
Jonathan Austin 1:8aa5cdb4ab67 99
Jonathan Austin 1:8aa5cdb4ab67 100 /**
Jonathan Austin 1:8aa5cdb4ab67 101 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 102 * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack.
Jonathan Austin 1:8aa5cdb4ab67 103 * @note The BLE stack *cannot* be brought up in a static context (the software simply hangs or corrupts itself).
Jonathan Austin 1:8aa5cdb4ab67 104 * Hence, the init() member function should be used to initialise the BLE stack.
Jonathan Austin 1:8aa5cdb4ab67 105 */
bluetooth_mdw 74:a8f5674a0079 106 MicroBitBLEManager::MicroBitBLEManager() : storage(NULL)
Jonathan Austin 1:8aa5cdb4ab67 107 {
Jonathan Austin 1:8aa5cdb4ab67 108 manager = this;
bluetooth_mdw 74:a8f5674a0079 109 this->ble = NULL;
bluetooth_mdw 74:a8f5674a0079 110 this->pairingStatus = 0;
bluetooth_mdw 74:a8f5674a0079 111 }
bluetooth_mdw 74:a8f5674a0079 112
bluetooth_mdw 74:a8f5674a0079 113 /**
bluetooth_mdw 74:a8f5674a0079 114 * When called, the micro:bit will begin advertising for a predefined period,
bluetooth_mdw 74:a8f5674a0079 115 * MICROBIT_BLE_ADVERTISING_TIMEOUT seconds to bonded devices.
bluetooth_mdw 74:a8f5674a0079 116 */
bluetooth_mdw 74:a8f5674a0079 117 MicroBitBLEManager *MicroBitBLEManager::getInstance()
bluetooth_mdw 74:a8f5674a0079 118 {
bluetooth_mdw 74:a8f5674a0079 119 if (manager == 0)
bluetooth_mdw 74:a8f5674a0079 120 {
bluetooth_mdw 74:a8f5674a0079 121 manager = new MicroBitBLEManager;
bluetooth_mdw 74:a8f5674a0079 122 }
bluetooth_mdw 74:a8f5674a0079 123 return manager;
Jonathan Austin 1:8aa5cdb4ab67 124 }
Jonathan Austin 1:8aa5cdb4ab67 125
Jonathan Austin 1:8aa5cdb4ab67 126 /**
Jonathan Austin 1:8aa5cdb4ab67 127 * When called, the micro:bit will begin advertising for a predefined period,
Jonathan Austin 1:8aa5cdb4ab67 128 * MICROBIT_BLE_ADVERTISING_TIMEOUT seconds to bonded devices.
Jonathan Austin 1:8aa5cdb4ab67 129 */
Jonathan Austin 1:8aa5cdb4ab67 130 void MicroBitBLEManager::advertise()
Jonathan Austin 1:8aa5cdb4ab67 131 {
bluetooth_mdw 74:a8f5674a0079 132 if (ble)
Jonathan Austin 1:8aa5cdb4ab67 133 ble->gap().startAdvertising();
Jonathan Austin 1:8aa5cdb4ab67 134 }
Jonathan Austin 1:8aa5cdb4ab67 135
Jonathan Austin 1:8aa5cdb4ab67 136 /**
Jonathan Austin 1:8aa5cdb4ab67 137 * Post constructor initialisation method as the BLE stack cannot be brought
Jonathan Austin 1:8aa5cdb4ab67 138 * up in a static context.
Jonathan Austin 1:8aa5cdb4ab67 139 * @param deviceName The name used when advertising
Jonathan Austin 1:8aa5cdb4ab67 140 * @param serialNumber The serial number exposed by the device information service
Jonathan Austin 1:8aa5cdb4ab67 141 * @param messageBus An instance of an EventModel, used during pairing.
Jonathan Austin 1:8aa5cdb4ab67 142 * @param enableBonding If true, the security manager enabled bonding.
Jonathan Austin 1:8aa5cdb4ab67 143 * @code
Jonathan Austin 1:8aa5cdb4ab67 144 * bleManager.init(uBit.getName(), uBit.getSerial(), uBit.messageBus, true);
Jonathan Austin 1:8aa5cdb4ab67 145 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 146 */
bluetooth_mdw 74:a8f5674a0079 147 void MicroBitBLEManager::init(ManagedString deviceName, ManagedString serialNumber, EventModel &messageBus, bool enableBonding)
Jonathan Austin 1:8aa5cdb4ab67 148 {
bluetooth_mdw 74:a8f5674a0079 149 ManagedString BLEName("BBC micro:bit");
bluetooth_mdw 74:a8f5674a0079 150 this->deviceName = deviceName;
Jonathan Austin 1:8aa5cdb4ab67 151
wwbluetooth 75:739b6a1c1b50 152 //wlw #if !(CONFIG_ENABLED(MICROBIT_BLE_WHITELIST))
bluetooth_mdw 74:a8f5674a0079 153 ManagedString namePrefix(" [");
bluetooth_mdw 74:a8f5674a0079 154 ManagedString namePostfix("]");
bluetooth_mdw 74:a8f5674a0079 155 BLEName = BLEName + namePrefix + deviceName + namePostfix;
wwbluetooth 75:739b6a1c1b50 156 //wlw #endif
Jonathan Austin 1:8aa5cdb4ab67 157
bluetooth_mdw 74:a8f5674a0079 158 // Start the BLE stack.
Jonathan Austin 1:8aa5cdb4ab67 159 #if CONFIG_ENABLED(MICROBIT_HEAP_REUSE_SD)
Jonathan Austin 1:8aa5cdb4ab67 160 btle_set_gatt_table_size(MICROBIT_SD_GATT_TABLE_SIZE);
Jonathan Austin 1:8aa5cdb4ab67 161 #endif
Jonathan Austin 1:8aa5cdb4ab67 162
Jonathan Austin 1:8aa5cdb4ab67 163 ble = new BLEDevice();
Jonathan Austin 1:8aa5cdb4ab67 164 ble->init();
Jonathan Austin 1:8aa5cdb4ab67 165
Jonathan Austin 1:8aa5cdb4ab67 166
Jonathan Austin 1:8aa5cdb4ab67 167 // Configure the stack to hold onto the CPU during critical timing events.
Jonathan Austin 1:8aa5cdb4ab67 168 // mbed-classic performs __disable_irq() calls in its timers that can cause
Jonathan Austin 1:8aa5cdb4ab67 169 // MIC failures on secure BLE channels...
Jonathan Austin 1:8aa5cdb4ab67 170 ble_common_opt_radio_cpu_mutex_t opt;
Jonathan Austin 1:8aa5cdb4ab67 171 opt.enable = 1;
Jonathan Austin 1:8aa5cdb4ab67 172 sd_ble_opt_set(BLE_COMMON_OPT_RADIO_CPU_MUTEX, (const ble_opt_t *)&opt);
Jonathan Austin 1:8aa5cdb4ab67 173
Jonathan Austin 1:8aa5cdb4ab67 174
Jonathan Austin 1:8aa5cdb4ab67 175
Jonathan Austin 1:8aa5cdb4ab67 176 // Configure the radio at our default power level
kenogami 78:e4c9170fd9b9 177 // setTransmitPower(MICROBIT_BLE_DEFAULT_TX_POWER);
kenogami 78:e4c9170fd9b9 178 // use higher power setting for iOS nrf Connect
kenogami 78:e4c9170fd9b9 179 setTransmitPower(6);
Jonathan Austin 1:8aa5cdb4ab67 180
bluetooth_mdw 74:a8f5674a0079 181 // Bring up core BLE services.
LancasterUniversity 29:62f8b007debf 182 #if CONFIG_ENABLED(MICROBIT_BLE_DFU_SERVICE)
Jonathan Austin 1:8aa5cdb4ab67 183 new MicroBitDFUService(*ble);
LancasterUniversity 29:62f8b007debf 184 #endif
LancasterUniversity 29:62f8b007debf 185
LancasterUniversity 29:62f8b007debf 186 #if CONFIG_ENABLED(MICROBIT_BLE_DEVICE_INFORMATION_SERVICE)
bluetooth_mdw 74:a8f5674a0079 187 DeviceInformationService ble_device_information_service(*ble, MICROBIT_BLE_MANUFACTURER, MICROBIT_BLE_MODEL, serialNumber.toCharArray(), MICROBIT_BLE_HARDWARE_VERSION, MICROBIT_BLE_FIRMWARE_VERSION, MICROBIT_BLE_SOFTWARE_VERSION);
LancasterUniversity 66:2fc7d7c2fffc 188 #else
LancasterUniversity 66:2fc7d7c2fffc 189 (void)serialNumber;
LancasterUniversity 29:62f8b007debf 190 #endif
LancasterUniversity 29:62f8b007debf 191
LancasterUniversity 29:62f8b007debf 192 #if CONFIG_ENABLED(MICROBIT_BLE_EVENT_SERVICE)
Jonathan Austin 1:8aa5cdb4ab67 193 new MicroBitEventService(*ble, messageBus);
LancasterUniversity 29:62f8b007debf 194 #else
LancasterUniversity 29:62f8b007debf 195 (void)messageBus;
LancasterUniversity 29:62f8b007debf 196 #endif
Jonathan Austin 1:8aa5cdb4ab67 197
Jonathan Austin 1:8aa5cdb4ab67 198
bluetooth_mdw 74:a8f5674a0079 199 // Setup advertising.
Jonathan Austin 1:8aa5cdb4ab67 200 #if CONFIG_ENABLED(MICROBIT_BLE_WHITELIST)
Jonathan Austin 1:8aa5cdb4ab67 201 ble->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Jonathan Austin 1:8aa5cdb4ab67 202 #else
Jonathan Austin 1:8aa5cdb4ab67 203 ble->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
Jonathan Austin 1:8aa5cdb4ab67 204 #endif
Jonathan Austin 1:8aa5cdb4ab67 205
Jonathan Austin 1:8aa5cdb4ab67 206 ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)BLEName.toCharArray(), BLEName.length());
Jonathan Austin 1:8aa5cdb4ab67 207 ble->setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Jonathan Austin 1:8aa5cdb4ab67 208 ble->setAdvertisingInterval(200);
Jonathan Austin 1:8aa5cdb4ab67 209
Jonathan Austin 1:8aa5cdb4ab67 210 #if (MICROBIT_BLE_ADVERTISING_TIMEOUT > 0)
Jonathan Austin 1:8aa5cdb4ab67 211 ble->gap().setAdvertisingTimeout(MICROBIT_BLE_ADVERTISING_TIMEOUT);
Jonathan Austin 1:8aa5cdb4ab67 212 #endif
kenogami 79:e4e6ab1c9835 213 ble->gap().startAdvertising();
Jonathan Austin 1:8aa5cdb4ab67 214 }
Jonathan Austin 1:8aa5cdb4ab67 215
wwbluetooth 75:739b6a1c1b50 216
Jonathan Austin 1:8aa5cdb4ab67 217 /**
Jonathan Austin 1:8aa5cdb4ab67 218 * Change the output power level of the transmitter to the given value.
Jonathan Austin 1:8aa5cdb4ab67 219 * @param power a value in the range 0..7, where 0 is the lowest power and 7 is the highest.
Jonathan Austin 1:8aa5cdb4ab67 220 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range.
Jonathan Austin 1:8aa5cdb4ab67 221 * @code
Jonathan Austin 1:8aa5cdb4ab67 222 * // maximum transmission power.
Jonathan Austin 1:8aa5cdb4ab67 223 * bleManager.setTransmitPower(7);
Jonathan Austin 1:8aa5cdb4ab67 224 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 225 */
Jonathan Austin 1:8aa5cdb4ab67 226 int MicroBitBLEManager::setTransmitPower(int power)
Jonathan Austin 1:8aa5cdb4ab67 227 {
Jonathan Austin 1:8aa5cdb4ab67 228 if (power < 0 || power >= MICROBIT_BLE_POWER_LEVELS)
Jonathan Austin 1:8aa5cdb4ab67 229 return MICROBIT_INVALID_PARAMETER;
Jonathan Austin 1:8aa5cdb4ab67 230
Jonathan Austin 1:8aa5cdb4ab67 231 if (ble->gap().setTxPower(MICROBIT_BLE_POWER_LEVEL[power]) != NRF_SUCCESS)
Jonathan Austin 1:8aa5cdb4ab67 232 return MICROBIT_NOT_SUPPORTED;
Jonathan Austin 1:8aa5cdb4ab67 233
Jonathan Austin 1:8aa5cdb4ab67 234 return MICROBIT_OK;
Jonathan Austin 1:8aa5cdb4ab67 235 }
Jonathan Austin 1:8aa5cdb4ab67 236
Jonathan Austin 1:8aa5cdb4ab67 237
Jonathan Austin 1:8aa5cdb4ab67 238
Jonathan Austin 1:8aa5cdb4ab67 239
Jonathan Austin 1:8aa5cdb4ab67 240 /**
Jonathan Austin 1:8aa5cdb4ab67 241 * Periodic callback in thread context.
Jonathan Austin 1:8aa5cdb4ab67 242 * We use this here purely to safely issue a disconnect operation after a pairing operation is complete.
Jonathan Austin 1:8aa5cdb4ab67 243 */
Jonathan Austin 1:8aa5cdb4ab67 244 void MicroBitBLEManager::idleTick()
Jonathan Austin 1:8aa5cdb4ab67 245 {
bluetooth_mdw 74:a8f5674a0079 246
wwbluetooth 75:739b6a1c1b50 247 }
bluetooth_mdw 74:a8f5674a0079 248
bluetooth_mdw 74:a8f5674a0079 249
bluetooth_mdw 74:a8f5674a0079 250 /**
bluetooth_mdw 74:a8f5674a0079 251 * Stops any currently running BLE advertisements
bluetooth_mdw 74:a8f5674a0079 252 */
bluetooth_mdw 74:a8f5674a0079 253 void MicroBitBLEManager::stopAdvertising()
bluetooth_mdw 74:a8f5674a0079 254 {
bluetooth_mdw 74:a8f5674a0079 255 ble->gap().stopAdvertising();
bluetooth_mdw 74:a8f5674a0079 256 }
bluetooth_mdw 74:a8f5674a0079 257
bluetooth_mdw 74:a8f5674a0079 258 #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_URL)
bluetooth_mdw 74:a8f5674a0079 259 /**
bluetooth_mdw 74:a8f5674a0079 260 * Set the content of Eddystone URL frames
bluetooth_mdw 74:a8f5674a0079 261 * @param url The url to broadcast
bluetooth_mdw 74:a8f5674a0079 262 * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m).
bluetooth_mdw 74:a8f5674a0079 263 * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true)
bluetooth_mdw 74:a8f5674a0079 264 * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL)
bluetooth_mdw 74:a8f5674a0079 265 * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded.
bluetooth_mdw 74:a8f5674a0079 266 * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power
bluetooth_mdw 74:a8f5674a0079 267 */
bluetooth_mdw 74:a8f5674a0079 268 int MicroBitBLEManager::advertiseEddystoneUrl(const char* url, int8_t calibratedPower, bool connectable, uint16_t interval)
bluetooth_mdw 74:a8f5674a0079 269 {
bluetooth_mdw 74:a8f5674a0079 270 ble->gap().stopAdvertising();
bluetooth_mdw 74:a8f5674a0079 271 ble->clearAdvertisingPayload();
bluetooth_mdw 74:a8f5674a0079 272
bluetooth_mdw 74:a8f5674a0079 273 ble->setAdvertisingType(connectable ? GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED : GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
bluetooth_mdw 74:a8f5674a0079 274 ble->setAdvertisingInterval(interval);
bluetooth_mdw 74:a8f5674a0079 275
bluetooth_mdw 74:a8f5674a0079 276 ble->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
bluetooth_mdw 74:a8f5674a0079 277
bluetooth_mdw 74:a8f5674a0079 278 int ret = MicroBitEddystone::getInstance()->setURL(ble, url, calibratedPower);
bluetooth_mdw 74:a8f5674a0079 279
bluetooth_mdw 74:a8f5674a0079 280 #if (MICROBIT_BLE_ADVERTISING_TIMEOUT > 0)
bluetooth_mdw 74:a8f5674a0079 281 ble->gap().setAdvertisingTimeout(MICROBIT_BLE_ADVERTISING_TIMEOUT);
bluetooth_mdw 74:a8f5674a0079 282 #endif
bluetooth_mdw 74:a8f5674a0079 283 ble->gap().startAdvertising();
bluetooth_kyo 80:1d4526d816a8 284
bluetooth_mdw 74:a8f5674a0079 285 return ret;
bluetooth_mdw 74:a8f5674a0079 286 }
Jonathan Austin 1:8aa5cdb4ab67 287
bluetooth_mdw 74:a8f5674a0079 288 /**
bluetooth_mdw 74:a8f5674a0079 289 * Set the content of Eddystone URL frames, but accepts a ManagedString as a url.
bluetooth_mdw 74:a8f5674a0079 290 * @param url The url to broadcast
bluetooth_mdw 74:a8f5674a0079 291 * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m).
bluetooth_mdw 74:a8f5674a0079 292 * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true)
bluetooth_mdw 74:a8f5674a0079 293 * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL)
bluetooth_mdw 74:a8f5674a0079 294 * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded.
bluetooth_mdw 74:a8f5674a0079 295 * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power
bluetooth_mdw 74:a8f5674a0079 296 */
bluetooth_mdw 74:a8f5674a0079 297 int MicroBitBLEManager::advertiseEddystoneUrl(ManagedString url, int8_t calibratedPower, bool connectable, uint16_t interval)
bluetooth_mdw 74:a8f5674a0079 298 {
bluetooth_mdw 74:a8f5674a0079 299 return advertiseEddystoneUrl((char *)url.toCharArray(), calibratedPower, connectable, interval);
Jonathan Austin 1:8aa5cdb4ab67 300 }
bluetooth_mdw 74:a8f5674a0079 301 #endif
bluetooth_mdw 74:a8f5674a0079 302
bluetooth_kyo 80:1d4526d816a8 303 /**
bluetooth_kyo 80:1d4526d816a8 304 * Set the content of Eddystone URL frames
bluetooth_kyo 80:1d4526d816a8 305 *
bluetooth_kyo 80:1d4526d816a8 306 * @param proximityUUID 16-byte proximity UUID
bluetooth_kyo 80:1d4526d816a8 307 *
bluetooth_kyo 80:1d4526d816a8 308 * @param major 2-byte major value
bluetooth_kyo 80:1d4526d816a8 309 *
bluetooth_kyo 80:1d4526d816a8 310 * @param minor 2-byte minor value
bluetooth_kyo 80:1d4526d816a8 311 *
bluetooth_kyo 80:1d4526d816a8 312 * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m).
bluetooth_kyo 80:1d4526d816a8 313 *
bluetooth_kyo 80:1d4526d816a8 314 * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL)
bluetooth_kyo 80:1d4526d816a8 315 *
bluetooth_kyo 80:1d4526d816a8 316 * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded.
bluetooth_kyo 80:1d4526d816a8 317 * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power
bluetooth_kyo 80:1d4526d816a8 318 */
bluetooth_kyo 80:1d4526d816a8 319 int MicroBitBLEManager::advertiseIBeacon(const UUID &proximityUUID, int16_t major, int16_t minor, int8_t calibratedPower, uint16_t interval)
bluetooth_kyo 80:1d4526d816a8 320 {
bluetooth_kyo 80:1d4526d816a8 321 int retVal = MICROBIT_OK;
bluetooth_kyo 80:1d4526d816a8 322
bluetooth_kyo 80:1d4526d816a8 323 ble->gap().stopAdvertising();
bluetooth_kyo 80:1d4526d816a8 324 ble->clearAdvertisingPayload();
bluetooth_kyo 80:1d4526d816a8 325
bluetooth_kyo 80:1d4526d816a8 326 ble->setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
bluetooth_kyo 80:1d4526d816a8 327 ble->setAdvertisingInterval(interval);
bluetooth_kyo 80:1d4526d816a8 328
bluetooth_kyo 80:1d4526d816a8 329 ble->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
bluetooth_kyo 80:1d4526d816a8 330
bluetooth_kyo 80:1d4526d816a8 331 retVal = MicroBitIBeacon::getInstance()->setParams(ble, proximityUUID, major, minor, calibratedPower);
bluetooth_kyo 80:1d4526d816a8 332
bluetooth_kyo 80:1d4526d816a8 333 #if (MICROBIT_BLE_ADVERTISING_TIMEOUT > 0)
bluetooth_kyo 80:1d4526d816a8 334 ble->gap().setAdvertisingTimeout(MICROBIT_BLE_ADVERTISING_TIMEOUT);
bluetooth_kyo 80:1d4526d816a8 335 #endif
bluetooth_kyo 80:1d4526d816a8 336 ble->gap().startAdvertising();
bluetooth_kyo 80:1d4526d816a8 337
bluetooth_kyo 80:1d4526d816a8 338 return retVal;
bluetooth_kyo 80:1d4526d816a8 339 }
bluetooth_mdw 74:a8f5674a0079 340
wwbluetooth 75:739b6a1c1b50 341 void MicroBitBLEManager::pairingMode(MicroBitDisplay &display, MicroBitButton &authorisationButton)
wwbluetooth 75:739b6a1c1b50 342 {
Jonathan Austin 1:8aa5cdb4ab67 343
wwbluetooth 75:739b6a1c1b50 344
wwbluetooth 75:739b6a1c1b50 345 }
Jonathan Austin 1:8aa5cdb4ab67 346
Jonathan Austin 1:8aa5cdb4ab67 347