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

Dependencies:   microbit-dal

Fork of BluetoothAsiaBeaconClass by Kaiser Ren

Committer:
krenbluetoothsig
Date:
Thu May 17 07:32:41 2018 +0000
Revision:
24:bfc7acc13cb4
Parent:
22:4f919611ff42
1. CHG: when micro:bit power up, it advertises its board name; when button A is pressed, it advertises Eddystone-URL. Both of them use same txpower setting.

Who changed what in which revision?

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