HoT with iBeacon

Dependencies:   microbit-iBeacon

Fork of BTW_Eddystone_Solution1 by Ken Ogami

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.

Committer:
bluetooth_kyo
Date:
Tue Oct 17 21:11:43 2017 +0000
Revision:
11:6505399afe76
Parent:
10:0b4b38de17b3
Child:
12:3c732fa25692
Add todos.; Change iBeacon UUID to one generated by Apple

Who changed what in which revision?

UserRevisionLine numberNew 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 11:6505399afe76 7 // TODO: global data and functions
bluetooth_kyo 8:fde95b9f958a 8 char URL[] = "https://www.bluetooth.com";
wwbluetooth 2:511941b33870 9
wwbluetooth 2:511941b33870 10 // lvl : Pwr@ 1m : Pwr@ 0m
wwbluetooth 2:511941b33870 11 // 0 : -90 : -49
wwbluetooth 2:511941b33870 12 // 1 : -78 : -37
wwbluetooth 2:511941b33870 13 // 2 : -74 : -33
wwbluetooth 2:511941b33870 14 // 3 : -69 : -28
wwbluetooth 2:511941b33870 15 // 4 : -66 : -25
wwbluetooth 2:511941b33870 16 // 5 : -61 : -20
wwbluetooth 2:511941b33870 17 // 6 : -56 : -15
bluetooth_kyo 8:fde95b9f958a 18 // 7 : -51 : -10
wwbluetooth 2:511941b33870 19
bluetooth_mdw 0:580a61fd96a1 20 const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10};
bluetooth_mdw 0:580a61fd96a1 21
bluetooth_mdw 0:580a61fd96a1 22 uint8_t advertising = 0;
bluetooth_mdw 0:580a61fd96a1 23 uint8_t tx_power_level = 6;
bluetooth_mdw 0:580a61fd96a1 24
bluetooth_kyo 8:fde95b9f958a 25 BLE_BEACON_TYPE beacon_type = BEACON_TYPE_EDDYSTONE; // BEACON_TYPE_EDDYSTONE, BEACON_TYPE_IBEACON, BEACON_TYPE_NONE
bluetooth_kyo 8:fde95b9f958a 26 // proximity UUID MSB first
bluetooth_kyo 8:fde95b9f958a 27 const uint8_t iBeaconProximityUUIDraw[] = {
bluetooth_kyo 11:6505399afe76 28 0x01, 0xD9, 0xC8, 0x00, 0x45, 0xF3, 0x40, 0x24, 0x93, 0xB0, 0xB0, 0xCD, 0x4A, 0x50, 0x2A, 0xF7
bluetooth_kyo 11:6505399afe76 29 }; // 01D9C800-45F3-4024-93B0-B0CD4A502AF7 use this UUID including spaces in mobile application
bluetooth_kyo 8:fde95b9f958a 30 const UUID iBeaconProximityUUID(iBeaconProximityUUIDraw);
bluetooth_kyo 10:0b4b38de17b3 31 const int16_t iBeaconMajor = 1234;
bluetooth_kyo 10:0b4b38de17b3 32 const int16_t iBeaconMinor = 5678;
bluetooth_kyo 8:fde95b9f958a 33
bluetooth_mdw 0:580a61fd96a1 34 void startAdvertising() {
bluetooth_kyo 8:fde95b9f958a 35 switch(beacon_type)
bluetooth_kyo 8:fde95b9f958a 36 {
bluetooth_kyo 8:fde95b9f958a 37 case BEACON_TYPE_EDDYSTONE:
bluetooth_kyo 8:fde95b9f958a 38 uBit.bleManager.advertiseEddystoneUrl(URL, CALIBRATED_POWERS[tx_power_level-1], false);
bluetooth_kyo 8:fde95b9f958a 39 uBit.display.scroll("ADV Eddystone");
bluetooth_kyo 8:fde95b9f958a 40 break;
bluetooth_kyo 8:fde95b9f958a 41 case BEACON_TYPE_IBEACON:
bluetooth_kyo 8:fde95b9f958a 42 uBit.bleManager.advertiseIBeacon(iBeaconProximityUUID, iBeaconMajor, iBeaconMinor, CALIBRATED_POWERS[tx_power_level-1]);
bluetooth_kyo 8:fde95b9f958a 43 uBit.display.scroll("ADV iBeacon");
bluetooth_kyo 8:fde95b9f958a 44 break;
bluetooth_kyo 8:fde95b9f958a 45 case BEACON_TYPE_NONE:
bluetooth_kyo 8:fde95b9f958a 46 default:
bluetooth_kyo 8:fde95b9f958a 47 break;
bluetooth_kyo 8:fde95b9f958a 48 }
bluetooth_mdw 0:580a61fd96a1 49 uBit.bleManager.setTransmitPower(tx_power_level);
bluetooth_mdw 0:580a61fd96a1 50 advertising = 1;
bluetooth_mdw 0:580a61fd96a1 51 }
bluetooth_mdw 0:580a61fd96a1 52
bluetooth_mdw 0:580a61fd96a1 53 void stopAdvertising() {
bluetooth_mdw 0:580a61fd96a1 54 uBit.bleManager.stopAdvertising();
bluetooth_mdw 0:580a61fd96a1 55 uBit.display.scroll("OFF");
bluetooth_mdw 0:580a61fd96a1 56 advertising = 0;
bluetooth_mdw 0:580a61fd96a1 57 }
bluetooth_mdw 0:580a61fd96a1 58
bluetooth_mdw 0:580a61fd96a1 59 void onButtonA(MicroBitEvent)
bluetooth_mdw 0:580a61fd96a1 60 {
bluetooth_kyo 8:fde95b9f958a 61 // toggle advertising
bluetooth_kyo 8:fde95b9f958a 62 switch(advertising)
bluetooth_kyo 8:fde95b9f958a 63 {
bluetooth_kyo 8:fde95b9f958a 64 case 0:
bluetooth_kyo 8:fde95b9f958a 65 startAdvertising();
bluetooth_kyo 8:fde95b9f958a 66 break;
bluetooth_kyo 8:fde95b9f958a 67 case 1:
bluetooth_kyo 8:fde95b9f958a 68 stopAdvertising();
bluetooth_kyo 8:fde95b9f958a 69 break;
bluetooth_kyo 8:fde95b9f958a 70 default:
bluetooth_kyo 8:fde95b9f958a 71 break;
bluetooth_mdw 0:580a61fd96a1 72 }
bluetooth_mdw 0:580a61fd96a1 73 }
bluetooth_mdw 0:580a61fd96a1 74
bluetooth_mdw 0:580a61fd96a1 75 void onButtonB(MicroBitEvent)
bluetooth_mdw 0:580a61fd96a1 76 {
bluetooth_kyo 8:fde95b9f958a 77 // toggle advertising type
bluetooth_kyo 11:6505399afe76 78 stopAdvertising();
bluetooth_kyo 8:fde95b9f958a 79 switch(beacon_type)
bluetooth_kyo 8:fde95b9f958a 80 {
bluetooth_kyo 8:fde95b9f958a 81 case BEACON_TYPE_EDDYSTONE:
bluetooth_kyo 8:fde95b9f958a 82 beacon_type = BEACON_TYPE_IBEACON;
bluetooth_kyo 8:fde95b9f958a 83 break;
bluetooth_kyo 8:fde95b9f958a 84 case BEACON_TYPE_IBEACON:
bluetooth_kyo 8:fde95b9f958a 85 beacon_type = BEACON_TYPE_EDDYSTONE;
bluetooth_kyo 8:fde95b9f958a 86 break;
bluetooth_kyo 8:fde95b9f958a 87 case BEACON_TYPE_NONE:
bluetooth_kyo 8:fde95b9f958a 88 default:
bluetooth_kyo 8:fde95b9f958a 89 beacon_type = BEACON_TYPE_EDDYSTONE;
bluetooth_kyo 8:fde95b9f958a 90 break;
bluetooth_kyo 8:fde95b9f958a 91 }
bluetooth_kyo 8:fde95b9f958a 92 startAdvertising();
bluetooth_mdw 0:580a61fd96a1 93 }
bluetooth_mdw 0:580a61fd96a1 94
bluetooth_kyo 8:fde95b9f958a 95
bluetooth_kyo 8:fde95b9f958a 96
bluetooth_mdw 0:580a61fd96a1 97 int main()
bluetooth_mdw 0:580a61fd96a1 98 {
bluetooth_kyo 8:fde95b9f958a 99 pcCom.printf("mBit Beacon.\n\r");
bluetooth_kyo 11:6505399afe76 100
bluetooth_kyo 11:6505399afe76 101 // TODO: main processing code
bluetooth_kyo 8:fde95b9f958a 102 // Initialize the micro:bit runtime.
bluetooth_mdw 0:580a61fd96a1 103 uBit.init();
bluetooth_mdw 0:580a61fd96a1 104
bluetooth_mdw 0:580a61fd96a1 105 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
bluetooth_mdw 0:580a61fd96a1 106 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
bluetooth_mdw 0:580a61fd96a1 107
bluetooth_kyo 8:fde95b9f958a 108 uBit.bleManager.advertise(); //First Advertisment, adv name only
bluetooth_mdw 0:580a61fd96a1 109
bluetooth_mdw 0:580a61fd96a1 110 release_fiber();
bluetooth_mdw 0:580a61fd96a1 111 }