add iBeacon functionality

Dependencies:   BLE_API mbed-dev-bin nRF51822

Fork of microbit-dal by Ken Ogami

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitIBeacon.cpp Source File

MicroBitIBeacon.cpp

00001 /*
00002 The MIT License (MIT)
00003 
00004 Copyright (c) 2016 British Broadcasting Corporation.
00005 This software is provided by Lancaster University by arrangement with the BBC.
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to whom the
00012 Software is furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 DEALINGS IN THE SOFTWARE.
00024 */
00025 
00026 #include "MicroBitConfig.h"
00027 #include "MicroBitIBeacon.h"
00028 
00029 MicroBitIBeacon *MicroBitIBeacon::_instance = NULL;
00030 
00031 /* The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ.
00032  * If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL)
00033  * The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this
00034  * as a compatability option, but does not support the options used...
00035  */
00036 #if !defined(__arm)
00037 #pragma GCC diagnostic ignored "-Wunused-function"
00038 #pragma GCC diagnostic push
00039 #pragma GCC diagnostic ignored "-Wunused-parameter"
00040 #endif
00041 
00042 /*
00043  * Return to our predefined compiler settings.
00044  */
00045 #if !defined(__arm)
00046 #pragma GCC diagnostic pop
00047 #endif
00048 
00049 const int IBEACON_PAYLOAD_LENGTH = 26;
00050 const uint8_t IBEACON_MANUFACTURER_DATA = 0xff;
00051 const uint8_t IBEACON_COMPANY_ID[] = {0x4c, 0x00};
00052 const uint8_t IBEACON_BEACON_TYPE[] = {0x02, 0x15};
00053 
00054 MicroBitIBeacon::MicroBitIBeacon() 
00055 {
00056 }
00057 
00058 MicroBitIBeacon * MicroBitIBeacon::getInstance()
00059 {
00060     if (_instance == 0)
00061     _instance = new MicroBitIBeacon;
00062 
00063     return _instance;
00064 }
00065 
00066 /**
00067   * Set the content of iBeacon frames
00068   *
00069   * @param proximityUUID 16-byte proximity UUID
00070   *
00071   * @param major 2-byte major value
00072   *
00073   * @param minor 2-byte minor value
00074   *
00075   * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m).
00076   *
00077   * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded.
00078   * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power
00079   */
00080 int MicroBitIBeacon::setParams(BLEDevice* ble, const UUID &proximityUUID, int16_t major, int16_t minor, int8_t calibratedPower)
00081 {
00082     uint8_t rawData[IBEACON_PAYLOAD_LENGTH + 1];
00083     
00084     // populate iBeacon payload
00085     int index = 0;
00086     rawData[index++] = IBEACON_COMPANY_ID[0];
00087     rawData[index++] = IBEACON_COMPANY_ID[1];
00088     rawData[index++] = IBEACON_BEACON_TYPE[0];
00089     rawData[index++] = IBEACON_BEACON_TYPE[1];
00090     // copy UUID reverse byte order since UUID class is little endian and packet 
00091     uint8_t * proxUUID = const_cast<uint8_t*>(proximityUUID.getBaseUUID());
00092     for(int ii=proximityUUID.getLen()-1;ii>=0;ii--) {
00093         rawData[index++] = *(proxUUID + ii);
00094     }
00095     rawData[index++] = (major >> 8) & 0xff;
00096     rawData[index++] = major & 0xff;
00097     rawData[index++] = (minor >> 8) & 0xff;
00098     rawData[index++] = minor & 0xff;
00099     rawData[index++] = calibratedPower & 0xff;
00100     
00101     ble_error_t err = ble->gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, rawData, index);
00102     
00103     return MICROBIT_OK;
00104 }