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

Dependencies:   microbit-iBeacon

Fork of BTW_ComboBeacon_Solution by KEN OGAMI

Committer:
krenbluetoothsig
Date:
Wed Aug 01 14:02:32 2018 +0000
Revision:
18:eeea3e01140c
Parent:
16:2a71c1b5fd71
Child:
19:8c3dd0a6a0a1
1. NEW: main.cpp as the template, beacon_src.txt includes code snippet.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
krenbluetoothsig 18:eeea3e01140c 1 // TODO: add ADV on and off flag
krenbluetoothsig 18:eeea3e01140c 2 #define ADV_ON 1
krenbluetoothsig 18:eeea3e01140c 3 #define ADV_OFF 0
krenbluetoothsig 18:eeea3e01140c 4 ctrl+C then ctrl+V
krenbluetoothsig 16:2a71c1b5fd71 5
krenbluetoothsig 18:eeea3e01140c 6 // TODO: constants, used for iBeacon UUID(MSB first), Major, Minor and txpower setting
krenbluetoothsig 18:eeea3e01140c 7 const uint8_t iBeaconProximityUUIDraw[] = {
krenbluetoothsig 18:eeea3e01140c 8 0x01, 0xD9, 0xC8, 0x00, 0x45, 0xF3, 0x40, 0x24, 0x93, 0xB0, 0xB0, 0xCD, 0x4A, 0x50, 0x2A, 0xF7
krenbluetoothsig 18:eeea3e01140c 9 }; // 01D9C800-45F3-4024-93B0-B0CD4A502AF7 use this UUID including dashes in mobile application
krenbluetoothsig 18:eeea3e01140c 10 const UUID iBeaconProximityUUID(iBeaconProximityUUIDraw);
krenbluetoothsig 18:eeea3e01140c 11 const int16_t iBeaconMajor = 1234;
krenbluetoothsig 18:eeea3e01140c 12 const int16_t iBeaconMinor = 5678;
krenbluetoothsig 16:2a71c1b5fd71 13 // lvl : Pwr@ 1m : Pwr@ 0m
krenbluetoothsig 16:2a71c1b5fd71 14 // 0 : -90 : -49
krenbluetoothsig 16:2a71c1b5fd71 15 // 1 : -78 : -37
krenbluetoothsig 16:2a71c1b5fd71 16 // 2 : -74 : -33
krenbluetoothsig 16:2a71c1b5fd71 17 // 3 : -69 : -28
krenbluetoothsig 16:2a71c1b5fd71 18 // 4 : -66 : -25
krenbluetoothsig 16:2a71c1b5fd71 19 // 5 : -61 : -20
krenbluetoothsig 16:2a71c1b5fd71 20 // 6 : -56 : -15
krenbluetoothsig 16:2a71c1b5fd71 21 // 7 : -51 : -10
krenbluetoothsig 16:2a71c1b5fd71 22 const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10};
krenbluetoothsig 16:2a71c1b5fd71 23
krenbluetoothsig 18:eeea3e01140c 24
krenbluetoothsig 18:eeea3e01140c 25 // TODO: global data and functions
krenbluetoothsig 18:eeea3e01140c 26 char eddystoneURL[] = "https://www.bluetooth.com";
krenbluetoothsig 16:2a71c1b5fd71 27 uint8_t advertising = 0;
krenbluetoothsig 16:2a71c1b5fd71 28 uint8_t tx_power_level = 6;
krenbluetoothsig 18:eeea3e01140c 29 BLE_BEACON_TYPE beacon_type = BEACON_TYPE_EDDYSTONE; // BEACON_TYPE_EDDYSTONE, BEACON_TYPE_IBEACON, BEACON_TYPE_NONE
krenbluetoothsig 16:2a71c1b5fd71 30
krenbluetoothsig 18:eeea3e01140c 31 // TODO: functions, startAdvertising and stopAdvertising
krenbluetoothsig 16:2a71c1b5fd71 32 void startAdvertising() {
krenbluetoothsig 16:2a71c1b5fd71 33 switch(beacon_type)
krenbluetoothsig 16:2a71c1b5fd71 34 {
krenbluetoothsig 16:2a71c1b5fd71 35 case BEACON_TYPE_EDDYSTONE:
krenbluetoothsig 18:eeea3e01140c 36 uBit.bleManager.advertiseEddystoneUrl(eddystoneURL, CALIBRATED_POWERS[tx_power_level - 1], false);
krenbluetoothsig 16:2a71c1b5fd71 37 uBit.display.print("E");
krenbluetoothsig 16:2a71c1b5fd71 38 pcCom.printf("eddystone\r\n");
krenbluetoothsig 16:2a71c1b5fd71 39 break;
krenbluetoothsig 18:eeea3e01140c 40 //
krenbluetoothsig 16:2a71c1b5fd71 41 case BEACON_TYPE_IBEACON:
krenbluetoothsig 16:2a71c1b5fd71 42 uBit.bleManager.advertiseIBeacon(iBeaconProximityUUID, iBeaconMajor, iBeaconMinor, CALIBRATED_POWERS[0]);
krenbluetoothsig 16:2a71c1b5fd71 43 uBit.display.print("i");
krenbluetoothsig 16:2a71c1b5fd71 44 pcCom.printf("iBeacon\r\n");
krenbluetoothsig 16:2a71c1b5fd71 45 break;
krenbluetoothsig 18:eeea3e01140c 46 //
krenbluetoothsig 16:2a71c1b5fd71 47 case BEACON_TYPE_NONE:
krenbluetoothsig 16:2a71c1b5fd71 48 default:
krenbluetoothsig 16:2a71c1b5fd71 49 pcCom.printf("unknow\r\n");
krenbluetoothsig 16:2a71c1b5fd71 50 break;
krenbluetoothsig 16:2a71c1b5fd71 51 }
krenbluetoothsig 16:2a71c1b5fd71 52 uBit.bleManager.setTransmitPower(tx_power_level);
krenbluetoothsig 16:2a71c1b5fd71 53 advertising = 1;
krenbluetoothsig 16:2a71c1b5fd71 54 }
krenbluetoothsig 16:2a71c1b5fd71 55
krenbluetoothsig 16:2a71c1b5fd71 56 void stopAdvertising() {
krenbluetoothsig 16:2a71c1b5fd71 57 pcCom.printf("ADV OFF\r\n");
krenbluetoothsig 16:2a71c1b5fd71 58 uBit.bleManager.stopAdvertising();
krenbluetoothsig 16:2a71c1b5fd71 59 advertising = 0;
krenbluetoothsig 16:2a71c1b5fd71 60 }
krenbluetoothsig 16:2a71c1b5fd71 61
krenbluetoothsig 18:eeea3e01140c 62 // TODO: functions, used for ButtonA and ButtonB interrupt callbacks
krenbluetoothsig 16:2a71c1b5fd71 63 void onButtonA(MicroBitEvent)
krenbluetoothsig 16:2a71c1b5fd71 64 {
krenbluetoothsig 16:2a71c1b5fd71 65 // toggle advertising
krenbluetoothsig 16:2a71c1b5fd71 66 switch(advertising)
krenbluetoothsig 16:2a71c1b5fd71 67 {
krenbluetoothsig 16:2a71c1b5fd71 68 case 0:
krenbluetoothsig 16:2a71c1b5fd71 69 pcCom.printf("ADV ON ");
krenbluetoothsig 16:2a71c1b5fd71 70 uBit.display.scroll("ON");
krenbluetoothsig 16:2a71c1b5fd71 71 startAdvertising();
krenbluetoothsig 16:2a71c1b5fd71 72 break;
krenbluetoothsig 16:2a71c1b5fd71 73 case 1:
krenbluetoothsig 16:2a71c1b5fd71 74 pcCom.printf("ADV OFF");
krenbluetoothsig 16:2a71c1b5fd71 75 stopAdvertising();
krenbluetoothsig 16:2a71c1b5fd71 76 uBit.display.scroll("OFF");
krenbluetoothsig 16:2a71c1b5fd71 77 break;
krenbluetoothsig 16:2a71c1b5fd71 78 default:
krenbluetoothsig 16:2a71c1b5fd71 79 break;
krenbluetoothsig 16:2a71c1b5fd71 80 }
krenbluetoothsig 16:2a71c1b5fd71 81 }
krenbluetoothsig 18:eeea3e01140c 82
krenbluetoothsig 16:2a71c1b5fd71 83 void onButtonB(MicroBitEvent)
krenbluetoothsig 16:2a71c1b5fd71 84 {
krenbluetoothsig 18:eeea3e01140c 85 // ButtonB is used to toggle beacon types, switch beacons types
krenbluetoothsig 18:eeea3e01140c 86 // between iBeacon and Eddystone.
krenbluetoothsig 16:2a71c1b5fd71 87 stopAdvertising();
krenbluetoothsig 16:2a71c1b5fd71 88 switch(beacon_type)
krenbluetoothsig 16:2a71c1b5fd71 89 {
krenbluetoothsig 16:2a71c1b5fd71 90 case BEACON_TYPE_EDDYSTONE:
krenbluetoothsig 16:2a71c1b5fd71 91 beacon_type = BEACON_TYPE_IBEACON;
krenbluetoothsig 16:2a71c1b5fd71 92 break;
krenbluetoothsig 16:2a71c1b5fd71 93 case BEACON_TYPE_IBEACON:
krenbluetoothsig 16:2a71c1b5fd71 94 beacon_type = BEACON_TYPE_EDDYSTONE;
krenbluetoothsig 16:2a71c1b5fd71 95 break;
krenbluetoothsig 16:2a71c1b5fd71 96 case BEACON_TYPE_NONE:
krenbluetoothsig 16:2a71c1b5fd71 97 default:
krenbluetoothsig 16:2a71c1b5fd71 98 beacon_type = BEACON_TYPE_EDDYSTONE;
krenbluetoothsig 16:2a71c1b5fd71 99 break;
krenbluetoothsig 16:2a71c1b5fd71 100 }
krenbluetoothsig 16:2a71c1b5fd71 101 startAdvertising();
krenbluetoothsig 16:2a71c1b5fd71 102 }
krenbluetoothsig 16:2a71c1b5fd71 103
krenbluetoothsig 16:2a71c1b5fd71 104
krenbluetoothsig 16:2a71c1b5fd71 105
krenbluetoothsig 18:eeea3e01140c 106 // TODO: Register the interrupt and callback for ButtonA and ButtonB
krenbluetoothsig 18:eeea3e01140c 107 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
krenbluetoothsig 18:eeea3e01140c 108 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
krenbluetoothsig 16:2a71c1b5fd71 109
krenbluetoothsig 18:eeea3e01140c 110 //TODO: First Advertisment, adv name only
krenbluetoothsig 18:eeea3e01140c 111 uBit.bleManager.advertise();
krenbluetoothsig 16:2a71c1b5fd71 112