add iBeacon functionality
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
source/bluetooth/MicroBitIBeacon.cpp@80:1d4526d816a8, 2017-10-16 (annotated)
- Committer:
- bluetooth_kyo
- Date:
- Mon Oct 16 18:35:40 2017 +0000
- Revision:
- 80:1d4526d816a8
Add iBeacon functionality
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bluetooth_kyo | 80:1d4526d816a8 | 1 | /* |
bluetooth_kyo | 80:1d4526d816a8 | 2 | The MIT License (MIT) |
bluetooth_kyo | 80:1d4526d816a8 | 3 | |
bluetooth_kyo | 80:1d4526d816a8 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
bluetooth_kyo | 80:1d4526d816a8 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
bluetooth_kyo | 80:1d4526d816a8 | 6 | |
bluetooth_kyo | 80:1d4526d816a8 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
bluetooth_kyo | 80:1d4526d816a8 | 8 | copy of this software and associated documentation files (the "Software"), |
bluetooth_kyo | 80:1d4526d816a8 | 9 | to deal in the Software without restriction, including without limitation |
bluetooth_kyo | 80:1d4526d816a8 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
bluetooth_kyo | 80:1d4526d816a8 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
bluetooth_kyo | 80:1d4526d816a8 | 12 | Software is furnished to do so, subject to the following conditions: |
bluetooth_kyo | 80:1d4526d816a8 | 13 | |
bluetooth_kyo | 80:1d4526d816a8 | 14 | The above copyright notice and this permission notice shall be included in |
bluetooth_kyo | 80:1d4526d816a8 | 15 | all copies or substantial portions of the Software. |
bluetooth_kyo | 80:1d4526d816a8 | 16 | |
bluetooth_kyo | 80:1d4526d816a8 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
bluetooth_kyo | 80:1d4526d816a8 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
bluetooth_kyo | 80:1d4526d816a8 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
bluetooth_kyo | 80:1d4526d816a8 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
bluetooth_kyo | 80:1d4526d816a8 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
bluetooth_kyo | 80:1d4526d816a8 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
bluetooth_kyo | 80:1d4526d816a8 | 23 | DEALINGS IN THE SOFTWARE. |
bluetooth_kyo | 80:1d4526d816a8 | 24 | */ |
bluetooth_kyo | 80:1d4526d816a8 | 25 | |
bluetooth_kyo | 80:1d4526d816a8 | 26 | #include "MicroBitConfig.h" |
bluetooth_kyo | 80:1d4526d816a8 | 27 | #include "MicroBitIBeacon.h" |
bluetooth_kyo | 80:1d4526d816a8 | 28 | |
bluetooth_kyo | 80:1d4526d816a8 | 29 | MicroBitIBeacon *MicroBitIBeacon::_instance = NULL; |
bluetooth_kyo | 80:1d4526d816a8 | 30 | |
bluetooth_kyo | 80:1d4526d816a8 | 31 | /* The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ. |
bluetooth_kyo | 80:1d4526d816a8 | 32 | * If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL) |
bluetooth_kyo | 80:1d4526d816a8 | 33 | * The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this |
bluetooth_kyo | 80:1d4526d816a8 | 34 | * as a compatability option, but does not support the options used... |
bluetooth_kyo | 80:1d4526d816a8 | 35 | */ |
bluetooth_kyo | 80:1d4526d816a8 | 36 | #if !defined(__arm) |
bluetooth_kyo | 80:1d4526d816a8 | 37 | #pragma GCC diagnostic ignored "-Wunused-function" |
bluetooth_kyo | 80:1d4526d816a8 | 38 | #pragma GCC diagnostic push |
bluetooth_kyo | 80:1d4526d816a8 | 39 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
bluetooth_kyo | 80:1d4526d816a8 | 40 | #endif |
bluetooth_kyo | 80:1d4526d816a8 | 41 | |
bluetooth_kyo | 80:1d4526d816a8 | 42 | /* |
bluetooth_kyo | 80:1d4526d816a8 | 43 | * Return to our predefined compiler settings. |
bluetooth_kyo | 80:1d4526d816a8 | 44 | */ |
bluetooth_kyo | 80:1d4526d816a8 | 45 | #if !defined(__arm) |
bluetooth_kyo | 80:1d4526d816a8 | 46 | #pragma GCC diagnostic pop |
bluetooth_kyo | 80:1d4526d816a8 | 47 | #endif |
bluetooth_kyo | 80:1d4526d816a8 | 48 | |
bluetooth_kyo | 80:1d4526d816a8 | 49 | const int IBEACON_PAYLOAD_LENGTH = 26; |
bluetooth_kyo | 80:1d4526d816a8 | 50 | const uint8_t IBEACON_MANUFACTURER_DATA = 0xff; |
bluetooth_kyo | 80:1d4526d816a8 | 51 | const uint8_t IBEACON_COMPANY_ID[] = {0x4c, 0x00}; |
bluetooth_kyo | 80:1d4526d816a8 | 52 | const uint8_t IBEACON_BEACON_TYPE[] = {0x02, 0x15}; |
bluetooth_kyo | 80:1d4526d816a8 | 53 | |
bluetooth_kyo | 80:1d4526d816a8 | 54 | MicroBitIBeacon::MicroBitIBeacon() |
bluetooth_kyo | 80:1d4526d816a8 | 55 | { |
bluetooth_kyo | 80:1d4526d816a8 | 56 | } |
bluetooth_kyo | 80:1d4526d816a8 | 57 | |
bluetooth_kyo | 80:1d4526d816a8 | 58 | MicroBitIBeacon * MicroBitIBeacon::getInstance() |
bluetooth_kyo | 80:1d4526d816a8 | 59 | { |
bluetooth_kyo | 80:1d4526d816a8 | 60 | if (_instance == 0) |
bluetooth_kyo | 80:1d4526d816a8 | 61 | _instance = new MicroBitIBeacon; |
bluetooth_kyo | 80:1d4526d816a8 | 62 | |
bluetooth_kyo | 80:1d4526d816a8 | 63 | return _instance; |
bluetooth_kyo | 80:1d4526d816a8 | 64 | } |
bluetooth_kyo | 80:1d4526d816a8 | 65 | |
bluetooth_kyo | 80:1d4526d816a8 | 66 | /** |
bluetooth_kyo | 80:1d4526d816a8 | 67 | * Set the content of iBeacon frames |
bluetooth_kyo | 80:1d4526d816a8 | 68 | * |
bluetooth_kyo | 80:1d4526d816a8 | 69 | * @param proximityUUID 16-byte proximity UUID |
bluetooth_kyo | 80:1d4526d816a8 | 70 | * |
bluetooth_kyo | 80:1d4526d816a8 | 71 | * @param major 2-byte major value |
bluetooth_kyo | 80:1d4526d816a8 | 72 | * |
bluetooth_kyo | 80:1d4526d816a8 | 73 | * @param minor 2-byte minor value |
bluetooth_kyo | 80:1d4526d816a8 | 74 | * |
bluetooth_kyo | 80:1d4526d816a8 | 75 | * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). |
bluetooth_kyo | 80:1d4526d816a8 | 76 | * |
bluetooth_kyo | 80:1d4526d816a8 | 77 | * @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 | 78 | * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power |
bluetooth_kyo | 80:1d4526d816a8 | 79 | */ |
bluetooth_kyo | 80:1d4526d816a8 | 80 | int MicroBitIBeacon::setParams(BLEDevice* ble, const UUID &proximityUUID, int16_t major, int16_t minor, int8_t calibratedPower) |
bluetooth_kyo | 80:1d4526d816a8 | 81 | { |
bluetooth_kyo | 80:1d4526d816a8 | 82 | uint8_t rawData[IBEACON_PAYLOAD_LENGTH + 1]; |
bluetooth_kyo | 80:1d4526d816a8 | 83 | |
bluetooth_kyo | 80:1d4526d816a8 | 84 | // populate iBeacon payload |
bluetooth_kyo | 80:1d4526d816a8 | 85 | int index = 0; |
bluetooth_kyo | 80:1d4526d816a8 | 86 | rawData[index++] = IBEACON_COMPANY_ID[0]; |
bluetooth_kyo | 80:1d4526d816a8 | 87 | rawData[index++] = IBEACON_COMPANY_ID[1]; |
bluetooth_kyo | 80:1d4526d816a8 | 88 | rawData[index++] = IBEACON_BEACON_TYPE[0]; |
bluetooth_kyo | 80:1d4526d816a8 | 89 | rawData[index++] = IBEACON_BEACON_TYPE[1]; |
bluetooth_kyo | 80:1d4526d816a8 | 90 | // copy UUID reverse byte order since UUID class is little endian and packet |
bluetooth_kyo | 80:1d4526d816a8 | 91 | uint8_t * proxUUID = const_cast<uint8_t*>(proximityUUID.getBaseUUID()); |
bluetooth_kyo | 80:1d4526d816a8 | 92 | for(int ii=proximityUUID.getLen()-1;ii>=0;ii--) { |
bluetooth_kyo | 80:1d4526d816a8 | 93 | rawData[index++] = *(proxUUID + ii); |
bluetooth_kyo | 80:1d4526d816a8 | 94 | } |
bluetooth_kyo | 80:1d4526d816a8 | 95 | rawData[index++] = (major >> 8) & 0xff; |
bluetooth_kyo | 80:1d4526d816a8 | 96 | rawData[index++] = major & 0xff; |
bluetooth_kyo | 80:1d4526d816a8 | 97 | rawData[index++] = (minor >> 8) & 0xff; |
bluetooth_kyo | 80:1d4526d816a8 | 98 | rawData[index++] = minor & 0xff; |
bluetooth_kyo | 80:1d4526d816a8 | 99 | rawData[index++] = calibratedPower & 0xff; |
bluetooth_kyo | 80:1d4526d816a8 | 100 | |
bluetooth_kyo | 80:1d4526d816a8 | 101 | ble_error_t err = ble->gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, rawData, index); |
bluetooth_kyo | 80:1d4526d816a8 | 102 | |
bluetooth_kyo | 80:1d4526d816a8 | 103 | return MICROBIT_OK; |
bluetooth_kyo | 80:1d4526d816a8 | 104 | } |