KEN OGAMI / BTW_ComboBeacon_Solution

Dependencies:   microbit-iBeacon

Fork of BTW_Eddystone_Solution1 by Ken Ogami

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "MicroBit.h"
00002 
00003 MicroBit uBit;
00004 
00005 Serial pcCom(USBTX, USBRX);
00006 
00007 // TODO: global data and functions
00008 char URL[] = "https://www.bluetooth.com";
00009 
00010 // lvl : Pwr@ 1m : Pwr@ 0m
00011 //  0  :   -90   :   -49
00012 //  1  :   -78   :   -37
00013 //  2  :   -74   :   -33
00014 //  3  :   -69   :   -28 
00015 //  4  :   -66   :   -25 
00016 //  5  :   -61   :   -20 
00017 //  6  :   -56   :   -15 
00018 //  7  :   -51   :   -10
00019 
00020 const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10};
00021 
00022 uint8_t advertising = 0;
00023 uint8_t tx_power_level = 6;
00024 
00025 BLE_BEACON_TYPE beacon_type = BEACON_TYPE_EDDYSTONE; // BEACON_TYPE_EDDYSTONE, BEACON_TYPE_IBEACON, BEACON_TYPE_NONE
00026 // proximity UUID MSB first
00027 const uint8_t  iBeaconProximityUUIDraw[] = {
00028     0x01, 0xD9, 0xC8, 0x00, 0x45, 0xF3, 0x40, 0x24, 0x93, 0xB0, 0xB0, 0xCD, 0x4A, 0x50, 0x2A, 0xF7
00029 }; // 01D9C800-45F3-4024-93B0-B0CD4A502AF7 use this UUID including dashes in mobile application
00030 const UUID iBeaconProximityUUID(iBeaconProximityUUIDraw);
00031 const int16_t iBeaconMajor = 1234;
00032 const int16_t iBeaconMinor = 5678;
00033 
00034 void startAdvertising() {
00035     switch(beacon_type)
00036     {
00037         case BEACON_TYPE_EDDYSTONE:
00038             uBit.bleManager.advertiseEddystoneUrl(URL, CALIBRATED_POWERS[tx_power_level-1], false);
00039             uBit.display.scroll("ADV Eddystone");
00040             break;
00041         case BEACON_TYPE_IBEACON:
00042             uBit.bleManager.advertiseIBeacon(iBeaconProximityUUID, iBeaconMajor, iBeaconMinor, CALIBRATED_POWERS[tx_power_level-1]);
00043             uBit.display.scroll("ADV iBeacon");
00044             break;
00045         case BEACON_TYPE_NONE:
00046         default:
00047             break;
00048     }
00049     uBit.bleManager.setTransmitPower(tx_power_level);
00050     advertising = 1;
00051 }
00052 
00053 void stopAdvertising() {
00054     uBit.bleManager.stopAdvertising();
00055     uBit.display.scroll("OFF");
00056     advertising = 0;
00057 }
00058 
00059 void onButtonA(MicroBitEvent)
00060 {
00061     // toggle advertising
00062     switch(advertising)
00063     {
00064         case 0:
00065             startAdvertising();
00066             break;
00067         case 1:
00068             stopAdvertising();
00069             break;
00070         default:
00071             break;
00072     }
00073 }
00074 
00075 void onButtonB(MicroBitEvent)
00076 {
00077     // toggle advertising type
00078     stopAdvertising();
00079     switch(beacon_type)
00080     {
00081         case BEACON_TYPE_EDDYSTONE:
00082             beacon_type = BEACON_TYPE_IBEACON;
00083             break;
00084         case BEACON_TYPE_IBEACON:
00085             beacon_type = BEACON_TYPE_EDDYSTONE;
00086             break;
00087         case BEACON_TYPE_NONE:
00088         default:
00089             beacon_type = BEACON_TYPE_EDDYSTONE;
00090             break;
00091    }
00092    startAdvertising();
00093 }
00094 
00095 
00096 
00097 int main()
00098 {
00099     pcCom.printf("mBit Beacon.\n\r");
00100 
00101     // TODO: main processing code    
00102     // Initialize the micro:bit runtime.
00103     uBit.init();
00104 
00105     uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
00106     uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
00107     
00108     uBit.bleManager.advertise();  //First Advertisment, adv name only
00109     
00110     release_fiber();
00111 }