This project contains two type of beacons: Eddystone and iBeacon.

Dependencies:   microbit-iBeacon

Fork of BTW_ComboBeacon_Solution by KEN OGAMI

Committer:
krenbluetoothsig
Date:
Thu Aug 02 04:10:03 2018 +0000
Revision:
20:223f589c3fe9
Parent:
19:8c3dd0a6a0a1
Child:
21:8d35c362b325
1. NEW: add "DIRECTORY" mark, users can find the file easily.

Who changed what in which revision?

UserRevisionLine numberNew 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 19:8c3dd0a6a0a1 30 //**@code **********************//
krenbluetoothsig 18:eeea3e01140c 31 // TODO: global data and functions
krenbluetoothsig 18:eeea3e01140c 32 char eddystoneURL[] = "https://www.bluetooth.com";
krenbluetoothsig 16:2a71c1b5fd71 33 uint8_t advertising = 0;
krenbluetoothsig 16:2a71c1b5fd71 34 uint8_t tx_power_level = 6;
krenbluetoothsig 18:eeea3e01140c 35 BLE_BEACON_TYPE beacon_type = BEACON_TYPE_EDDYSTONE; // BEACON_TYPE_EDDYSTONE, BEACON_TYPE_IBEACON, BEACON_TYPE_NONE
krenbluetoothsig 19:8c3dd0a6a0a1 36 //**@endcode *******************//
krenbluetoothsig 16:2a71c1b5fd71 37
krenbluetoothsig 19:8c3dd0a6a0a1 38
krenbluetoothsig 19:8c3dd0a6a0a1 39 //**@code **********************//
krenbluetoothsig 18:eeea3e01140c 40 // TODO: functions, startAdvertising and stopAdvertising
krenbluetoothsig 16:2a71c1b5fd71 41 void startAdvertising() {
krenbluetoothsig 16:2a71c1b5fd71 42 switch(beacon_type)
krenbluetoothsig 16:2a71c1b5fd71 43 {
krenbluetoothsig 16:2a71c1b5fd71 44 case BEACON_TYPE_EDDYSTONE:
krenbluetoothsig 18:eeea3e01140c 45 uBit.bleManager.advertiseEddystoneUrl(eddystoneURL, CALIBRATED_POWERS[tx_power_level - 1], false);
krenbluetoothsig 16:2a71c1b5fd71 46 uBit.display.print("E");
krenbluetoothsig 16:2a71c1b5fd71 47 pcCom.printf("eddystone\r\n");
krenbluetoothsig 16:2a71c1b5fd71 48 break;
krenbluetoothsig 18:eeea3e01140c 49 //
krenbluetoothsig 16:2a71c1b5fd71 50 case BEACON_TYPE_IBEACON:
krenbluetoothsig 16:2a71c1b5fd71 51 uBit.bleManager.advertiseIBeacon(iBeaconProximityUUID, iBeaconMajor, iBeaconMinor, CALIBRATED_POWERS[0]);
krenbluetoothsig 16:2a71c1b5fd71 52 uBit.display.print("i");
krenbluetoothsig 16:2a71c1b5fd71 53 pcCom.printf("iBeacon\r\n");
krenbluetoothsig 16:2a71c1b5fd71 54 break;
krenbluetoothsig 18:eeea3e01140c 55 //
krenbluetoothsig 16:2a71c1b5fd71 56 case BEACON_TYPE_NONE:
krenbluetoothsig 16:2a71c1b5fd71 57 default:
krenbluetoothsig 16:2a71c1b5fd71 58 pcCom.printf("unknow\r\n");
krenbluetoothsig 16:2a71c1b5fd71 59 break;
krenbluetoothsig 16:2a71c1b5fd71 60 }
krenbluetoothsig 16:2a71c1b5fd71 61 uBit.bleManager.setTransmitPower(tx_power_level);
krenbluetoothsig 16:2a71c1b5fd71 62 advertising = 1;
krenbluetoothsig 16:2a71c1b5fd71 63 }
krenbluetoothsig 16:2a71c1b5fd71 64
krenbluetoothsig 16:2a71c1b5fd71 65 void stopAdvertising() {
krenbluetoothsig 16:2a71c1b5fd71 66 pcCom.printf("ADV OFF\r\n");
krenbluetoothsig 16:2a71c1b5fd71 67 uBit.bleManager.stopAdvertising();
krenbluetoothsig 16:2a71c1b5fd71 68 advertising = 0;
krenbluetoothsig 16:2a71c1b5fd71 69 }
krenbluetoothsig 19:8c3dd0a6a0a1 70 //**@endcode *******************//
krenbluetoothsig 16:2a71c1b5fd71 71
krenbluetoothsig 19:8c3dd0a6a0a1 72
krenbluetoothsig 19:8c3dd0a6a0a1 73
krenbluetoothsig 19:8c3dd0a6a0a1 74 //**@code **********************//
krenbluetoothsig 18:eeea3e01140c 75 // TODO: functions, used for ButtonA and ButtonB interrupt callbacks
krenbluetoothsig 16:2a71c1b5fd71 76 void onButtonA(MicroBitEvent)
krenbluetoothsig 16:2a71c1b5fd71 77 {
krenbluetoothsig 16:2a71c1b5fd71 78 // toggle advertising
krenbluetoothsig 16:2a71c1b5fd71 79 switch(advertising)
krenbluetoothsig 16:2a71c1b5fd71 80 {
krenbluetoothsig 16:2a71c1b5fd71 81 case 0:
krenbluetoothsig 16:2a71c1b5fd71 82 pcCom.printf("ADV ON ");
krenbluetoothsig 16:2a71c1b5fd71 83 uBit.display.scroll("ON");
krenbluetoothsig 16:2a71c1b5fd71 84 startAdvertising();
krenbluetoothsig 16:2a71c1b5fd71 85 break;
krenbluetoothsig 16:2a71c1b5fd71 86 case 1:
krenbluetoothsig 16:2a71c1b5fd71 87 pcCom.printf("ADV OFF");
krenbluetoothsig 16:2a71c1b5fd71 88 stopAdvertising();
krenbluetoothsig 16:2a71c1b5fd71 89 uBit.display.scroll("OFF");
krenbluetoothsig 16:2a71c1b5fd71 90 break;
krenbluetoothsig 16:2a71c1b5fd71 91 default:
krenbluetoothsig 16:2a71c1b5fd71 92 break;
krenbluetoothsig 16:2a71c1b5fd71 93 }
krenbluetoothsig 16:2a71c1b5fd71 94 }
krenbluetoothsig 18:eeea3e01140c 95
krenbluetoothsig 16:2a71c1b5fd71 96 void onButtonB(MicroBitEvent)
krenbluetoothsig 16:2a71c1b5fd71 97 {
krenbluetoothsig 18:eeea3e01140c 98 // ButtonB is used to toggle beacon types, switch beacons types
krenbluetoothsig 18:eeea3e01140c 99 // between iBeacon and Eddystone.
krenbluetoothsig 16:2a71c1b5fd71 100 stopAdvertising();
krenbluetoothsig 16:2a71c1b5fd71 101 switch(beacon_type)
krenbluetoothsig 16:2a71c1b5fd71 102 {
krenbluetoothsig 16:2a71c1b5fd71 103 case BEACON_TYPE_EDDYSTONE:
krenbluetoothsig 16:2a71c1b5fd71 104 beacon_type = BEACON_TYPE_IBEACON;
krenbluetoothsig 16:2a71c1b5fd71 105 break;
krenbluetoothsig 16:2a71c1b5fd71 106 case BEACON_TYPE_IBEACON:
krenbluetoothsig 16:2a71c1b5fd71 107 beacon_type = BEACON_TYPE_EDDYSTONE;
krenbluetoothsig 16:2a71c1b5fd71 108 break;
krenbluetoothsig 16:2a71c1b5fd71 109 case BEACON_TYPE_NONE:
krenbluetoothsig 16:2a71c1b5fd71 110 default:
krenbluetoothsig 16:2a71c1b5fd71 111 beacon_type = BEACON_TYPE_EDDYSTONE;
krenbluetoothsig 16:2a71c1b5fd71 112 break;
krenbluetoothsig 16:2a71c1b5fd71 113 }
krenbluetoothsig 16:2a71c1b5fd71 114 startAdvertising();
krenbluetoothsig 16:2a71c1b5fd71 115 }
krenbluetoothsig 19:8c3dd0a6a0a1 116 //**@endcode *******************//
krenbluetoothsig 16:2a71c1b5fd71 117
krenbluetoothsig 16:2a71c1b5fd71 118
krenbluetoothsig 19:8c3dd0a6a0a1 119 //**@code **********************//
krenbluetoothsig 18:eeea3e01140c 120 // TODO: Register the interrupt and callback for ButtonA and ButtonB
krenbluetoothsig 18:eeea3e01140c 121 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
krenbluetoothsig 18:eeea3e01140c 122 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
krenbluetoothsig 19:8c3dd0a6a0a1 123 //**@endcode *******************//
krenbluetoothsig 16:2a71c1b5fd71 124
krenbluetoothsig 19:8c3dd0a6a0a1 125
krenbluetoothsig 19:8c3dd0a6a0a1 126 //**@code **********************//
krenbluetoothsig 19:8c3dd0a6a0a1 127 // TODO: First Advertisment, adv name only
krenbluetoothsig 18:eeea3e01140c 128 uBit.bleManager.advertise();
krenbluetoothsig 19:8c3dd0a6a0a1 129 //**@endcode *******************//