This project contains two type of beacons: Eddystone and iBeacon.
Dependencies: microbit-iBeacon
Fork of BTW_ComboBeacon_Solution by
beacon_src.txt@21:8d35c362b325, 2018-08-02 (annotated)
- Committer:
- krenbluetoothsig
- Date:
- Thu Aug 02 14:40:17 2018 +0000
- Revision:
- 21:8d35c362b325
- Parent:
- 20:223f589c3fe9
1. CHG: minor change for code retract.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krenbluetoothsig | 20:223f589c3fe9 | 1 | //DIRECTORY: ./main.cpp |
krenbluetoothsig | 20:223f589c3fe9 | 2 | |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 3 | //**@code **********************// |
krenbluetoothsig | 18:eeea3e01140c | 4 | // TODO: add ADV on and off flag |
krenbluetoothsig | 18:eeea3e01140c | 5 | #define ADV_ON 1 |
krenbluetoothsig | 18:eeea3e01140c | 6 | #define ADV_OFF 0 |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 7 | //**@endcode *******************// |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 8 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 9 | |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 10 | //**@code **********************// |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 11 | // TODO: constants, used for iBeacon UUID(MSB first), Major, Minor and txpower setting |
krenbluetoothsig | 18:eeea3e01140c | 12 | const uint8_t iBeaconProximityUUIDraw[] = { |
krenbluetoothsig | 18:eeea3e01140c | 13 | 0x01, 0xD9, 0xC8, 0x00, 0x45, 0xF3, 0x40, 0x24, 0x93, 0xB0, 0xB0, 0xCD, 0x4A, 0x50, 0x2A, 0xF7 |
krenbluetoothsig | 18:eeea3e01140c | 14 | }; // 01D9C800-45F3-4024-93B0-B0CD4A502AF7 use this UUID including dashes in mobile application |
krenbluetoothsig | 18:eeea3e01140c | 15 | const UUID iBeaconProximityUUID(iBeaconProximityUUIDraw); |
krenbluetoothsig | 18:eeea3e01140c | 16 | const int16_t iBeaconMajor = 1234; |
krenbluetoothsig | 18:eeea3e01140c | 17 | const int16_t iBeaconMinor = 5678; |
krenbluetoothsig | 16:2a71c1b5fd71 | 18 | // lvl : Pwr@ 1m : Pwr@ 0m |
krenbluetoothsig | 16:2a71c1b5fd71 | 19 | // 0 : -90 : -49 |
krenbluetoothsig | 16:2a71c1b5fd71 | 20 | // 1 : -78 : -37 |
krenbluetoothsig | 16:2a71c1b5fd71 | 21 | // 2 : -74 : -33 |
krenbluetoothsig | 16:2a71c1b5fd71 | 22 | // 3 : -69 : -28 |
krenbluetoothsig | 16:2a71c1b5fd71 | 23 | // 4 : -66 : -25 |
krenbluetoothsig | 16:2a71c1b5fd71 | 24 | // 5 : -61 : -20 |
krenbluetoothsig | 16:2a71c1b5fd71 | 25 | // 6 : -56 : -15 |
krenbluetoothsig | 16:2a71c1b5fd71 | 26 | // 7 : -51 : -10 |
krenbluetoothsig | 16:2a71c1b5fd71 | 27 | const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10}; |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 28 | //**@endcode *******************// |
krenbluetoothsig | 16:2a71c1b5fd71 | 29 | |
krenbluetoothsig | 21:8d35c362b325 | 30 | |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 31 | //**@code **********************// |
krenbluetoothsig | 18:eeea3e01140c | 32 | // TODO: global data and functions |
krenbluetoothsig | 18:eeea3e01140c | 33 | char eddystoneURL[] = "https://www.bluetooth.com"; |
krenbluetoothsig | 16:2a71c1b5fd71 | 34 | uint8_t advertising = 0; |
krenbluetoothsig | 16:2a71c1b5fd71 | 35 | uint8_t tx_power_level = 6; |
krenbluetoothsig | 18:eeea3e01140c | 36 | BLE_BEACON_TYPE beacon_type = BEACON_TYPE_EDDYSTONE; // BEACON_TYPE_EDDYSTONE, BEACON_TYPE_IBEACON, BEACON_TYPE_NONE |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 37 | //**@endcode *******************// |
krenbluetoothsig | 16:2a71c1b5fd71 | 38 | |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 39 | |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 40 | //**@code **********************// |
krenbluetoothsig | 18:eeea3e01140c | 41 | // TODO: functions, startAdvertising and stopAdvertising |
krenbluetoothsig | 16:2a71c1b5fd71 | 42 | void startAdvertising() { |
krenbluetoothsig | 16:2a71c1b5fd71 | 43 | switch(beacon_type) |
krenbluetoothsig | 16:2a71c1b5fd71 | 44 | { |
krenbluetoothsig | 16:2a71c1b5fd71 | 45 | case BEACON_TYPE_EDDYSTONE: |
krenbluetoothsig | 18:eeea3e01140c | 46 | uBit.bleManager.advertiseEddystoneUrl(eddystoneURL, CALIBRATED_POWERS[tx_power_level - 1], false); |
krenbluetoothsig | 16:2a71c1b5fd71 | 47 | uBit.display.print("E"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 48 | pcCom.printf("eddystone\r\n"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 49 | break; |
krenbluetoothsig | 18:eeea3e01140c | 50 | // |
krenbluetoothsig | 16:2a71c1b5fd71 | 51 | case BEACON_TYPE_IBEACON: |
krenbluetoothsig | 16:2a71c1b5fd71 | 52 | uBit.bleManager.advertiseIBeacon(iBeaconProximityUUID, iBeaconMajor, iBeaconMinor, CALIBRATED_POWERS[0]); |
krenbluetoothsig | 16:2a71c1b5fd71 | 53 | uBit.display.print("i"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 54 | pcCom.printf("iBeacon\r\n"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 55 | break; |
krenbluetoothsig | 18:eeea3e01140c | 56 | // |
krenbluetoothsig | 16:2a71c1b5fd71 | 57 | case BEACON_TYPE_NONE: |
krenbluetoothsig | 16:2a71c1b5fd71 | 58 | default: |
krenbluetoothsig | 16:2a71c1b5fd71 | 59 | pcCom.printf("unknow\r\n"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 60 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 61 | } |
krenbluetoothsig | 16:2a71c1b5fd71 | 62 | uBit.bleManager.setTransmitPower(tx_power_level); |
krenbluetoothsig | 16:2a71c1b5fd71 | 63 | advertising = 1; |
krenbluetoothsig | 16:2a71c1b5fd71 | 64 | } |
krenbluetoothsig | 16:2a71c1b5fd71 | 65 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 66 | void stopAdvertising() { |
krenbluetoothsig | 16:2a71c1b5fd71 | 67 | pcCom.printf("ADV OFF\r\n"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 68 | uBit.bleManager.stopAdvertising(); |
krenbluetoothsig | 16:2a71c1b5fd71 | 69 | advertising = 0; |
krenbluetoothsig | 16:2a71c1b5fd71 | 70 | } |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 71 | //**@endcode *******************// |
krenbluetoothsig | 16:2a71c1b5fd71 | 72 | |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 73 | |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 74 | |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 75 | //**@code **********************// |
krenbluetoothsig | 18:eeea3e01140c | 76 | // TODO: functions, used for ButtonA and ButtonB interrupt callbacks |
krenbluetoothsig | 16:2a71c1b5fd71 | 77 | void onButtonA(MicroBitEvent) |
krenbluetoothsig | 16:2a71c1b5fd71 | 78 | { |
krenbluetoothsig | 16:2a71c1b5fd71 | 79 | // toggle advertising |
krenbluetoothsig | 16:2a71c1b5fd71 | 80 | switch(advertising) |
krenbluetoothsig | 16:2a71c1b5fd71 | 81 | { |
krenbluetoothsig | 16:2a71c1b5fd71 | 82 | case 0: |
krenbluetoothsig | 16:2a71c1b5fd71 | 83 | pcCom.printf("ADV ON "); |
krenbluetoothsig | 16:2a71c1b5fd71 | 84 | uBit.display.scroll("ON"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 85 | startAdvertising(); |
krenbluetoothsig | 16:2a71c1b5fd71 | 86 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 87 | case 1: |
krenbluetoothsig | 16:2a71c1b5fd71 | 88 | pcCom.printf("ADV OFF"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 89 | stopAdvertising(); |
krenbluetoothsig | 16:2a71c1b5fd71 | 90 | uBit.display.scroll("OFF"); |
krenbluetoothsig | 16:2a71c1b5fd71 | 91 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 92 | default: |
krenbluetoothsig | 16:2a71c1b5fd71 | 93 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 94 | } |
krenbluetoothsig | 16:2a71c1b5fd71 | 95 | } |
krenbluetoothsig | 18:eeea3e01140c | 96 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 97 | void onButtonB(MicroBitEvent) |
krenbluetoothsig | 16:2a71c1b5fd71 | 98 | { |
krenbluetoothsig | 18:eeea3e01140c | 99 | // ButtonB is used to toggle beacon types, switch beacons types |
krenbluetoothsig | 18:eeea3e01140c | 100 | // between iBeacon and Eddystone. |
krenbluetoothsig | 16:2a71c1b5fd71 | 101 | stopAdvertising(); |
krenbluetoothsig | 16:2a71c1b5fd71 | 102 | switch(beacon_type) |
krenbluetoothsig | 16:2a71c1b5fd71 | 103 | { |
krenbluetoothsig | 16:2a71c1b5fd71 | 104 | case BEACON_TYPE_EDDYSTONE: |
krenbluetoothsig | 16:2a71c1b5fd71 | 105 | beacon_type = BEACON_TYPE_IBEACON; |
krenbluetoothsig | 16:2a71c1b5fd71 | 106 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 107 | case BEACON_TYPE_IBEACON: |
krenbluetoothsig | 16:2a71c1b5fd71 | 108 | beacon_type = BEACON_TYPE_EDDYSTONE; |
krenbluetoothsig | 16:2a71c1b5fd71 | 109 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 110 | case BEACON_TYPE_NONE: |
krenbluetoothsig | 16:2a71c1b5fd71 | 111 | default: |
krenbluetoothsig | 16:2a71c1b5fd71 | 112 | beacon_type = BEACON_TYPE_EDDYSTONE; |
krenbluetoothsig | 16:2a71c1b5fd71 | 113 | break; |
krenbluetoothsig | 16:2a71c1b5fd71 | 114 | } |
krenbluetoothsig | 16:2a71c1b5fd71 | 115 | startAdvertising(); |
krenbluetoothsig | 16:2a71c1b5fd71 | 116 | } |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 117 | //**@endcode *******************// |
krenbluetoothsig | 16:2a71c1b5fd71 | 118 | |
krenbluetoothsig | 16:2a71c1b5fd71 | 119 | |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 120 | //**@code **********************// |
krenbluetoothsig | 18:eeea3e01140c | 121 | // TODO: Register the interrupt and callback for ButtonA and ButtonB |
krenbluetoothsig | 18:eeea3e01140c | 122 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA); |
krenbluetoothsig | 18:eeea3e01140c | 123 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB); |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 124 | //**@endcode *******************// |
krenbluetoothsig | 16:2a71c1b5fd71 | 125 | |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 126 | |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 127 | //**@code **********************// |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 128 | // TODO: First Advertisment, adv name only |
krenbluetoothsig | 18:eeea3e01140c | 129 | uBit.bleManager.advertise(); |
krenbluetoothsig | 19:8c3dd0a6a0a1 | 130 | //**@endcode *******************// |