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

Dependencies:   microbit-iBeacon

Fork of BTW_ComboBeacon_Solution by KEN OGAMI

Revision:
17:6715daf92c78
Parent:
16:2a71c1b5fd71
Child:
18:eeea3e01140c
--- a/main.cpp	Tue Jul 31 13:25:48 2018 +0000
+++ b/main.cpp	Wed Aug 01 02:48:13 2018 +0000
@@ -0,0 +1,143 @@
+#include "MicroBit.h"
+
+/******************************************************
+ *          Macros
+ ******************************************************/
+ // TODO: add ADV on and off flag
+ #define ADV_ON 1
+ #define ADV_OFF 0
+ 
+/******************************************************
+ *          Constants
+ ******************************************************/
+ // TODO: constants, used for iBeacon UUID(MSB first), Major, Minor and txpower setting
+const uint8_t  iBeaconProximityUUIDraw[] = {
+    0x01, 0xD9, 0xC8, 0x00, 0x45, 0xF3, 0x40, 0x24, 0x93, 0xB0, 0xB0, 0xCD, 0x4A, 0x50, 0x2A, 0xF7
+}; // 01D9C800-45F3-4024-93B0-B0CD4A502AF7 use this UUID including dashes in mobile application
+const UUID iBeaconProximityUUID(iBeaconProximityUUIDraw);
+const int16_t iBeaconMajor = 1234;
+const int16_t iBeaconMinor = 5678;
+
+// lvl : Pwr@ 1m : Pwr@ 0m
+//  0  :   -90   :   -49 
+//  1  :   -78   :   -37
+//  2  :   -74   :   -33
+//  3  :   -69   :   -28 
+//  4  :   -66   :   -25 
+//  5  :   -61   :   -20 
+//  6  :   -56   :   -15 
+//  7  :   -51   :   -10
+
+const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10};
+ 
+ /******************************************************
+ *          Variables
+ ******************************************************/
+//microbit object
+MicroBit uBit;
+//Initial USB-to-Serial, baud rate is 9600-8-N-1
+Serial pcCom(USBTX, USBRX);
+
+// TODO: global data and functions
+char eddystoneURL[] = "https://www.bluetooth.com";
+uint8_t advertising = 0;
+uint8_t tx_power_level = 6;
+BLE_BEACON_TYPE beacon_type = BEACON_TYPE_EDDYSTONE; // BEACON_TYPE_EDDYSTONE, BEACON_TYPE_IBEACON, BEACON_TYPE_NONE
+
+
+
+/******************************************************
+ *          Functions
+ ******************************************************/
+// TODO: functions, startAdvertising and stopAdvertising 
+void startAdvertising() {
+    switch(beacon_type)
+    {
+        case BEACON_TYPE_EDDYSTONE:
+            uBit.bleManager.advertiseEddystoneUrl(eddystoneURL, CALIBRATED_POWERS[tx_power_level - 1], false);
+            uBit.display.print("E");
+            pcCom.printf("eddystone\r\n");
+            break;
+            //
+        case BEACON_TYPE_IBEACON:
+            uBit.bleManager.advertiseIBeacon(iBeaconProximityUUID, iBeaconMajor, iBeaconMinor, CALIBRATED_POWERS[0]);
+            uBit.display.print("i");
+            pcCom.printf("iBeacon\r\n");
+            break;
+            //
+        case BEACON_TYPE_NONE:
+        default:
+            pcCom.printf("unknow\r\n");
+            break;
+    }
+    uBit.bleManager.setTransmitPower(tx_power_level);
+    advertising = 1;
+}
+
+void stopAdvertising() {
+    pcCom.printf("ADV OFF\r\n");
+    uBit.bleManager.stopAdvertising();
+    advertising = 0;
+}
+
+// TODO: functions, used for ButtonA and ButtonB interrupt callbacks
+void onButtonA(MicroBitEvent)
+{
+    // toggle advertising
+    switch(advertising)
+    {
+        case 0:
+            pcCom.printf("ADV ON ");
+            uBit.display.scroll("ON");
+            startAdvertising();
+            break;
+        case 1:
+            pcCom.printf("ADV OFF");
+            stopAdvertising();
+            uBit.display.scroll("OFF");
+            break;
+        default:
+            break;
+    }
+}
+ 
+void onButtonB(MicroBitEvent)
+{
+    // ButtonB is used to toggle beacon types, switch beacons types 
+    // between iBeacon and Eddystone. 
+    stopAdvertising();
+    switch(beacon_type)
+    {
+        case BEACON_TYPE_EDDYSTONE:
+            beacon_type = BEACON_TYPE_IBEACON;
+            break;
+        case BEACON_TYPE_IBEACON:
+            beacon_type = BEACON_TYPE_EDDYSTONE;
+            break;
+        case BEACON_TYPE_NONE:
+        default:
+            beacon_type = BEACON_TYPE_EDDYSTONE;
+            break;
+   }
+   startAdvertising();
+}
+
+
+
+int main()
+{
+    // Serial output after a reset
+    pcCom.printf("mBit Beacon.\n\r");
+
+    // Initialize the micro:bit runtime.
+    uBit.init();
+
+    // TODO: Register the interrupt and callback for ButtonA and ButtonB
+    uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
+    uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
+    
+    //TODO: First Advertisment, adv name only
+    uBit.bleManager.advertise();  
+    
+    release_fiber();
+}