This repository is for Bluetooth Asia 2018 developer training, beacon session.

Dependencies:   microbit-dal

Fork of BluetoothAsiaBeaconClass by Kaiser Ren

Committer:
krenbluetoothsig
Date:
Fri Apr 20 05:43:19 2018 +0000
Revision:
22:4f919611ff42
Parent:
21:6512562f058d
Child:
24:bfc7acc13cb4
1. CHG: remove code comment symbol.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
krenbluetoothsig 21:6512562f058d 1
krenbluetoothsig 21:6512562f058d 2 #include "MicroBit.h"
krenbluetoothsig 21:6512562f058d 3
krenbluetoothsig 21:6512562f058d 4 MicroBit uBit;
krenbluetoothsig 21:6512562f058d 5
krenbluetoothsig 21:6512562f058d 6 char URL[] = "http://www.bluetooth.com";
krenbluetoothsig 21:6512562f058d 7
krenbluetoothsig 21:6512562f058d 8 // lvl : Pwr@ 1m : Pwr@ 0m
krenbluetoothsig 21:6512562f058d 9 // 0 : -90 : -49
krenbluetoothsig 21:6512562f058d 10 // 1 : -78 : -37
krenbluetoothsig 21:6512562f058d 11 // 2 : -74 : -33
krenbluetoothsig 21:6512562f058d 12 // 3 : -69 : -28
krenbluetoothsig 21:6512562f058d 13 // 4 : -66 : -25
krenbluetoothsig 21:6512562f058d 14 // 5 : -61 : -20
krenbluetoothsig 21:6512562f058d 15 // 6 : -56 : -15
krenbluetoothsig 21:6512562f058d 16 // 7 : -51 : -10
krenbluetoothsig 21:6512562f058d 17
krenbluetoothsig 21:6512562f058d 18 const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10};
krenbluetoothsig 21:6512562f058d 19
krenbluetoothsig 21:6512562f058d 20 uint8_t advertising = 0;
krenbluetoothsig 21:6512562f058d 21 uint8_t tx_power_level = 6;
krenbluetoothsig 21:6512562f058d 22
krenbluetoothsig 21:6512562f058d 23 void startAdvertising() {
krenbluetoothsig 21:6512562f058d 24 uBit.bleManager.advertiseEddystoneUrl(URL, CALIBRATED_POWERS[tx_power_level-1], false);
krenbluetoothsig 21:6512562f058d 25 uBit.bleManager.setTransmitPower(tx_power_level);
krenbluetoothsig 21:6512562f058d 26 uBit.display.scroll("ADV");
krenbluetoothsig 21:6512562f058d 27 advertising = 1;
krenbluetoothsig 21:6512562f058d 28 }
krenbluetoothsig 21:6512562f058d 29
krenbluetoothsig 21:6512562f058d 30 void stopAdvertising() {
krenbluetoothsig 21:6512562f058d 31 uBit.bleManager.stopAdvertising();
krenbluetoothsig 21:6512562f058d 32 uBit.display.scroll("OFF");
krenbluetoothsig 21:6512562f058d 33 advertising = 0;
krenbluetoothsig 21:6512562f058d 34 }
krenbluetoothsig 21:6512562f058d 35
krenbluetoothsig 21:6512562f058d 36 void onButtonA(MicroBitEvent)
krenbluetoothsig 21:6512562f058d 37 {
krenbluetoothsig 21:6512562f058d 38 if (advertising == 1) {
krenbluetoothsig 21:6512562f058d 39 return;
krenbluetoothsig 21:6512562f058d 40 }
krenbluetoothsig 21:6512562f058d 41 startAdvertising();
krenbluetoothsig 21:6512562f058d 42 }
krenbluetoothsig 21:6512562f058d 43
krenbluetoothsig 21:6512562f058d 44 void onButtonB(MicroBitEvent)
krenbluetoothsig 21:6512562f058d 45 {
krenbluetoothsig 21:6512562f058d 46 if (advertising == 0) {
krenbluetoothsig 21:6512562f058d 47 return;
krenbluetoothsig 21:6512562f058d 48 }
krenbluetoothsig 21:6512562f058d 49 stopAdvertising();
krenbluetoothsig 21:6512562f058d 50 }
krenbluetoothsig 21:6512562f058d 51
krenbluetoothsig 21:6512562f058d 52 int main()
krenbluetoothsig 21:6512562f058d 53 {
krenbluetoothsig 21:6512562f058d 54 // Initialise the micro:bit runtime.
krenbluetoothsig 21:6512562f058d 55 uBit.init();
krenbluetoothsig 21:6512562f058d 56
krenbluetoothsig 21:6512562f058d 57 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
krenbluetoothsig 21:6512562f058d 58 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
krenbluetoothsig 21:6512562f058d 59
krenbluetoothsig 21:6512562f058d 60 startAdvertising();
krenbluetoothsig 21:6512562f058d 61
krenbluetoothsig 21:6512562f058d 62 release_fiber();
krenbluetoothsig 21:6512562f058d 63 }
krenbluetoothsig 21:6512562f058d 64