HoT with iBeacon
Dependencies: microbit-iBeacon
Fork of BTW_Eddystone_Solution1 by
Beacon Hands-on-Training solution code using Eddystone URL and iBeacon formats.
Libraries microbit-iBeacon -> microbit-dal-iBeacon
Includes classes for Eddystone and iBeacon. Eddystone only supports URL and UID formats, not TLM.
main.cpp@8:fde95b9f958a, 2017-10-16 (annotated)
- Committer:
- bluetooth_kyo
- Date:
- Mon Oct 16 18:36:15 2017 +0000
- Revision:
- 8:fde95b9f958a
- Parent:
- 6:9b09bfc43c69
- Child:
- 10:0b4b38de17b3
Add iBeacon functionality
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bluetooth_mdw | 0:580a61fd96a1 | 1 | #include "MicroBit.h" |
bluetooth_mdw | 0:580a61fd96a1 | 2 | |
bluetooth_mdw | 0:580a61fd96a1 | 3 | MicroBit uBit; |
bluetooth_mdw | 0:580a61fd96a1 | 4 | |
bluetooth_kyo | 8:fde95b9f958a | 5 | Serial pcCom(USBTX, USBRX); |
bluetooth_kyo | 8:fde95b9f958a | 6 | |
bluetooth_kyo | 8:fde95b9f958a | 7 | char URL[] = "https://www.bluetooth.com"; |
wwbluetooth | 2:511941b33870 | 8 | |
wwbluetooth | 2:511941b33870 | 9 | // lvl : Pwr@ 1m : Pwr@ 0m |
wwbluetooth | 2:511941b33870 | 10 | // 0 : -90 : -49 |
wwbluetooth | 2:511941b33870 | 11 | // 1 : -78 : -37 |
wwbluetooth | 2:511941b33870 | 12 | // 2 : -74 : -33 |
wwbluetooth | 2:511941b33870 | 13 | // 3 : -69 : -28 |
wwbluetooth | 2:511941b33870 | 14 | // 4 : -66 : -25 |
wwbluetooth | 2:511941b33870 | 15 | // 5 : -61 : -20 |
wwbluetooth | 2:511941b33870 | 16 | // 6 : -56 : -15 |
bluetooth_kyo | 8:fde95b9f958a | 17 | // 7 : -51 : -10 |
wwbluetooth | 2:511941b33870 | 18 | |
bluetooth_mdw | 0:580a61fd96a1 | 19 | const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10}; |
bluetooth_mdw | 0:580a61fd96a1 | 20 | |
bluetooth_mdw | 0:580a61fd96a1 | 21 | uint8_t advertising = 0; |
bluetooth_mdw | 0:580a61fd96a1 | 22 | uint8_t tx_power_level = 6; |
bluetooth_mdw | 0:580a61fd96a1 | 23 | |
bluetooth_kyo | 8:fde95b9f958a | 24 | BLE_BEACON_TYPE beacon_type = BEACON_TYPE_EDDYSTONE; // BEACON_TYPE_EDDYSTONE, BEACON_TYPE_IBEACON, BEACON_TYPE_NONE |
bluetooth_kyo | 8:fde95b9f958a | 25 | // proximity UUID MSB first |
bluetooth_kyo | 8:fde95b9f958a | 26 | const uint8_t iBeaconProximityUUIDraw[] = { |
bluetooth_kyo | 8:fde95b9f958a | 27 | 0xa1, 0xe6, 0xdf, 0x48, 0xaf, 0x6b, 0x11, 0xe7, 0xab, 0xc4, 0xce, 0xc2, 0x78, 0xb6, 0xb5, 0x0a |
bluetooth_kyo | 8:fde95b9f958a | 28 | }; |
bluetooth_kyo | 8:fde95b9f958a | 29 | const UUID iBeaconProximityUUID(iBeaconProximityUUIDraw); |
bluetooth_kyo | 8:fde95b9f958a | 30 | const int16_t iBeaconMajor = 0x1234; |
bluetooth_kyo | 8:fde95b9f958a | 31 | const int16_t iBeaconMinor = 0x5678; |
bluetooth_kyo | 8:fde95b9f958a | 32 | |
bluetooth_mdw | 0:580a61fd96a1 | 33 | void startAdvertising() { |
bluetooth_kyo | 8:fde95b9f958a | 34 | switch(beacon_type) |
bluetooth_kyo | 8:fde95b9f958a | 35 | { |
bluetooth_kyo | 8:fde95b9f958a | 36 | case BEACON_TYPE_EDDYSTONE: |
bluetooth_kyo | 8:fde95b9f958a | 37 | uBit.bleManager.advertiseEddystoneUrl(URL, CALIBRATED_POWERS[tx_power_level-1], false); |
bluetooth_kyo | 8:fde95b9f958a | 38 | uBit.display.scroll("ADV Eddystone"); |
bluetooth_kyo | 8:fde95b9f958a | 39 | break; |
bluetooth_kyo | 8:fde95b9f958a | 40 | case BEACON_TYPE_IBEACON: |
bluetooth_kyo | 8:fde95b9f958a | 41 | uBit.bleManager.advertiseIBeacon(iBeaconProximityUUID, iBeaconMajor, iBeaconMinor, CALIBRATED_POWERS[tx_power_level-1]); |
bluetooth_kyo | 8:fde95b9f958a | 42 | uBit.display.scroll("ADV iBeacon"); |
bluetooth_kyo | 8:fde95b9f958a | 43 | break; |
bluetooth_kyo | 8:fde95b9f958a | 44 | case BEACON_TYPE_NONE: |
bluetooth_kyo | 8:fde95b9f958a | 45 | default: |
bluetooth_kyo | 8:fde95b9f958a | 46 | break; |
bluetooth_kyo | 8:fde95b9f958a | 47 | } |
bluetooth_mdw | 0:580a61fd96a1 | 48 | uBit.bleManager.setTransmitPower(tx_power_level); |
bluetooth_mdw | 0:580a61fd96a1 | 49 | advertising = 1; |
bluetooth_mdw | 0:580a61fd96a1 | 50 | } |
bluetooth_mdw | 0:580a61fd96a1 | 51 | |
bluetooth_mdw | 0:580a61fd96a1 | 52 | void stopAdvertising() { |
bluetooth_mdw | 0:580a61fd96a1 | 53 | uBit.bleManager.stopAdvertising(); |
bluetooth_mdw | 0:580a61fd96a1 | 54 | uBit.display.scroll("OFF"); |
bluetooth_mdw | 0:580a61fd96a1 | 55 | advertising = 0; |
bluetooth_mdw | 0:580a61fd96a1 | 56 | } |
bluetooth_mdw | 0:580a61fd96a1 | 57 | |
bluetooth_mdw | 0:580a61fd96a1 | 58 | void onButtonA(MicroBitEvent) |
bluetooth_mdw | 0:580a61fd96a1 | 59 | { |
bluetooth_kyo | 8:fde95b9f958a | 60 | // toggle advertising |
bluetooth_kyo | 8:fde95b9f958a | 61 | switch(advertising) |
bluetooth_kyo | 8:fde95b9f958a | 62 | { |
bluetooth_kyo | 8:fde95b9f958a | 63 | case 0: |
bluetooth_kyo | 8:fde95b9f958a | 64 | startAdvertising(); |
bluetooth_kyo | 8:fde95b9f958a | 65 | break; |
bluetooth_kyo | 8:fde95b9f958a | 66 | case 1: |
bluetooth_kyo | 8:fde95b9f958a | 67 | stopAdvertising(); |
bluetooth_kyo | 8:fde95b9f958a | 68 | break; |
bluetooth_kyo | 8:fde95b9f958a | 69 | default: |
bluetooth_kyo | 8:fde95b9f958a | 70 | break; |
bluetooth_mdw | 0:580a61fd96a1 | 71 | } |
bluetooth_mdw | 0:580a61fd96a1 | 72 | } |
bluetooth_mdw | 0:580a61fd96a1 | 73 | |
bluetooth_mdw | 0:580a61fd96a1 | 74 | void onButtonB(MicroBitEvent) |
bluetooth_mdw | 0:580a61fd96a1 | 75 | { |
bluetooth_kyo | 8:fde95b9f958a | 76 | // toggle advertising type |
bluetooth_kyo | 8:fde95b9f958a | 77 | switch(beacon_type) |
bluetooth_kyo | 8:fde95b9f958a | 78 | { |
bluetooth_kyo | 8:fde95b9f958a | 79 | case BEACON_TYPE_EDDYSTONE: |
bluetooth_kyo | 8:fde95b9f958a | 80 | beacon_type = BEACON_TYPE_IBEACON; |
bluetooth_kyo | 8:fde95b9f958a | 81 | break; |
bluetooth_kyo | 8:fde95b9f958a | 82 | case BEACON_TYPE_IBEACON: |
bluetooth_kyo | 8:fde95b9f958a | 83 | beacon_type = BEACON_TYPE_EDDYSTONE; |
bluetooth_kyo | 8:fde95b9f958a | 84 | break; |
bluetooth_kyo | 8:fde95b9f958a | 85 | case BEACON_TYPE_NONE: |
bluetooth_kyo | 8:fde95b9f958a | 86 | default: |
bluetooth_kyo | 8:fde95b9f958a | 87 | beacon_type = BEACON_TYPE_EDDYSTONE; |
bluetooth_kyo | 8:fde95b9f958a | 88 | break; |
bluetooth_kyo | 8:fde95b9f958a | 89 | } |
bluetooth_kyo | 8:fde95b9f958a | 90 | startAdvertising(); |
bluetooth_mdw | 0:580a61fd96a1 | 91 | } |
bluetooth_mdw | 0:580a61fd96a1 | 92 | |
bluetooth_kyo | 8:fde95b9f958a | 93 | |
bluetooth_kyo | 8:fde95b9f958a | 94 | |
bluetooth_mdw | 0:580a61fd96a1 | 95 | int main() |
bluetooth_mdw | 0:580a61fd96a1 | 96 | { |
bluetooth_kyo | 8:fde95b9f958a | 97 | pcCom.printf("mBit Beacon.\n\r"); |
bluetooth_kyo | 8:fde95b9f958a | 98 | |
bluetooth_kyo | 8:fde95b9f958a | 99 | // Initialize the micro:bit runtime. |
bluetooth_mdw | 0:580a61fd96a1 | 100 | uBit.init(); |
bluetooth_mdw | 0:580a61fd96a1 | 101 | |
bluetooth_mdw | 0:580a61fd96a1 | 102 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA); |
bluetooth_mdw | 0:580a61fd96a1 | 103 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB); |
bluetooth_mdw | 0:580a61fd96a1 | 104 | |
bluetooth_kyo | 8:fde95b9f958a | 105 | uBit.bleManager.advertise(); //First Advertisment, adv name only |
bluetooth_mdw | 0:580a61fd96a1 | 106 | |
bluetooth_mdw | 0:580a61fd96a1 | 107 | release_fiber(); |
bluetooth_mdw | 0:580a61fd96a1 | 108 | } |