From 6d51454bc9da34112ebda331fa37f96b7ff39d74 Mon Sep 17 00:00:00 2001 From: Martin Woolley Date: Fri, 28 Oct 2016 11:39:34 +0100 Subject: [PATCH] Examples for Eddystone URL and UID beacons --- .../bluetooth-eddystone-uid/config.json | 19 +++++++ .../examples/bluetooth-eddystone-uid/main.cpp | 52 +++++++++++++++++++ .../bluetooth-eddystone-url/config.json | 19 +++++++ .../examples/bluetooth-eddystone-url/main.cpp | 51 ++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100755 source/examples/bluetooth-eddystone-uid/config.json create mode 100755 source/examples/bluetooth-eddystone-uid/main.cpp create mode 100755 source/examples/bluetooth-eddystone-url/config.json create mode 100755 source/examples/bluetooth-eddystone-url/main.cpp diff --git a/source/examples/bluetooth-eddystone-uid/config.json b/source/examples/bluetooth-eddystone-uid/config.json new file mode 100755 index 0000000..697b3ae --- /dev/null +++ b/source/examples/bluetooth-eddystone-uid/config.json @@ -0,0 +1,19 @@ +{ + "microbit-dal": { + "bluetooth": { + "enabled": 1, + "pairing_mode": 0, + "private_addressing": 0, + "open": 0, + "whitelist": 0, + "advertising_timeout": 0, + "tx_power": 0, + "dfu_service": 0, + "event_service": 0, + "device_info_service": 0, + "eddystone_url": 0, + "eddystone_uid": 1 + }, + "gatt_table_size": "0x600" + } +} diff --git a/source/examples/bluetooth-eddystone-uid/main.cpp b/source/examples/bluetooth-eddystone-uid/main.cpp new file mode 100755 index 0000000..2102304 --- /dev/null +++ b/source/examples/bluetooth-eddystone-uid/main.cpp @@ -0,0 +1,52 @@ +#include "MicroBit.h" + +MicroBit uBit; + +char UID_NAMESPACE[] = {0x0E,0x67,0x47,0x04,0x42,0xD0,0x14,0x06,0xD5,0x83}; // sha-1 hash of "com.bittysoftware" +char UID_INSTANCE[] = {0x00 , 0x00, 0x00, 0x00, 0x00, 0x01}; +const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10}; + +uint8_t advertising = 0; +uint8_t tx_power_level = 6; + +void startAdvertising() { + uBit.bleManager.advertiseEddystoneUid(UID_NAMESPACE, UID_INSTANCE, CALIBRATED_POWERS[tx_power_level-1], false); + uBit.bleManager.setTransmitPower(6); + uBit.display.scroll("ADV"); + advertising = 1; +} + +void stopAdvertising() { + uBit.bleManager.stopAdvertising(); + uBit.display.scroll("OFF"); + advertising = 0; +} + +void onButtonA(MicroBitEvent) +{ + if (advertising == 1) { + return; + } + startAdvertising(); +} + +void onButtonB(MicroBitEvent) +{ + if (advertising == 0) { + return; + } + stopAdvertising(); +} + +int main() +{ + // Initialise the micro:bit runtime. + uBit.init(); + + uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY, onButtonA); + uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_EVT_ANY, onButtonB); + + startAdvertising(); + + release_fiber(); +} diff --git a/source/examples/bluetooth-eddystone-url/config.json b/source/examples/bluetooth-eddystone-url/config.json new file mode 100755 index 0000000..94cc404 --- /dev/null +++ b/source/examples/bluetooth-eddystone-url/config.json @@ -0,0 +1,19 @@ +{ + "microbit-dal": { + "bluetooth": { + "enabled": 1, + "pairing_mode": 0, + "private_addressing": 0, + "open": 0, + "whitelist": 0, + "advertising_timeout": 0, + "tx_power": 0, + "dfu_service": 0, + "event_service": 0, + "device_info_service": 0, + "eddystone_url": 1, + "eddystone_uid": 0 + }, + "gatt_table_size": "0x600" + } +} diff --git a/source/examples/bluetooth-eddystone-url/main.cpp b/source/examples/bluetooth-eddystone-url/main.cpp new file mode 100755 index 0000000..39b0b03 --- /dev/null +++ b/source/examples/bluetooth-eddystone-url/main.cpp @@ -0,0 +1,51 @@ +#include "MicroBit.h" + +MicroBit uBit; + +char URL[] = "https://goo.gl/TlUTF7"; +const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10}; + +uint8_t advertising = 0; +uint8_t tx_power_level = 6; + +void startAdvertising() { + uBit.bleManager.advertiseEddystoneUrl(URL, CALIBRATED_POWERS[tx_power_level-1], false); + uBit.bleManager.setTransmitPower(tx_power_level); + uBit.display.scroll("ADV"); + advertising = 1; +} + +void stopAdvertising() { + uBit.bleManager.stopAdvertising(); + uBit.display.scroll("OFF"); + advertising = 0; +} + +void onButtonA(MicroBitEvent) +{ + if (advertising == 1) { + return; + } + startAdvertising(); +} + +void onButtonB(MicroBitEvent) +{ + if (advertising == 0) { + return; + } + stopAdvertising(); +} + +int main() +{ + // Initialise the micro:bit runtime. + uBit.init(); + + uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY, onButtonA); + uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_EVT_ANY, onButtonB); + + startAdvertising(); + + release_fiber(); +}