diff --git a/docs/reference.md b/docs/reference.md index c5bdd08e..257425d3 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -30,7 +30,7 @@ control.inBackground(() => { ``` ## Bluetooth - + ```namespaces devices.tellCameraTo(MesCameraEvent.TakePhoto); bluetooth.onBluetoothConnected(() => {}); diff --git a/docs/reference/bluetooth.md b/docs/reference/bluetooth.md index d2fa66b2..21e850bb 100644 --- a/docs/reference/bluetooth.md +++ b/docs/reference/bluetooth.md @@ -31,6 +31,13 @@ bluetooth.uartWriteNumber(0); bluetooth.uartWriteValue("", 0); ``` +## Eddystone + +```cards +bluetooth.advertiseUrl("https://pxt.microbit.org/", 7); +bluetooth.stopAdvertising(); +``` + ```package bluetooth ``` @@ -47,4 +54,7 @@ For more advanced information on the @boardname@ Bluetooth UART service includin [uartWriteString](/reference/bluetooth/uart-write-string), [uartWriteNumber](/reference/bluetooth/uart-write-number), [uartWriteValue](/reference/bluetooth/uart-write-value), -[onBluetoothConnected](/reference/bluetooth/on-bluetooth-connected), [onBluetoothDisconnected](/reference/bluetooth/on-bluetooth-disconnected) +[onBluetoothConnected](/reference/bluetooth/on-bluetooth-connected), +[onBluetoothDisconnected](/reference/bluetooth/on-bluetooth-disconnected), +[advertiseUrl](/reference/bluetooth/advertise-url), +[stopAdvertising](/reference/bluetooth/stop-advertising) diff --git a/docs/reference/bluetooth/advertise-url.md b/docs/reference/bluetooth/advertise-url.md new file mode 100644 index 00000000..9a0a808d --- /dev/null +++ b/docs/reference/bluetooth/advertise-url.md @@ -0,0 +1,41 @@ +# Avertise Url + +Advertises a URL via the Eddystone protocol over Bluetooth. + +## ~hint + +### Eddystone + +Bluetooth beacons are used to indicate proximity to a place or object of interest. +Beacons use Bluetooth advertising to broadcast a small amount of data, +which can be received and acted upon by anyone in range with a suitable device and software, typically a smartphone and application. + +There are various beacon message formats, which define the way Bluetooth advertising packets are used as containers for beacon data. +iBeacon is Apple's beacon message format. Eddystone comes from Google. + +Read more at https://lancaster-university.github.io/microbit-docs/ble/eddystone/ . + +## ~ + +```sig +bluetooth.advertiseUrl("https://pxt.microbit.org/", 7); +``` + +### Parameters + +* ``url`` - a [string](/reference/types/string) containing the URL to broadcast, at most 18 characters long +* ``power`` - a [number](/reference/types/number) representing the power level between 0 (short) and 7 (maximum range). + +### Example: Broadcast a secret code + +```blocks +bluetooth.advertiseUrl("https://pxt.io?secret=42", 7); +``` + +## See Also + +[stop-advertising](/reference/bluetooth/stop-advertising) + +```package +bluetooth +``` \ No newline at end of file diff --git a/docs/reference/bluetooth/stop-advertising.md b/docs/reference/bluetooth/stop-advertising.md new file mode 100644 index 00000000..82ce25ba --- /dev/null +++ b/docs/reference/bluetooth/stop-advertising.md @@ -0,0 +1,38 @@ +# Stop Advertising + +Stops advertising URL via the Eddystone protocol over Bluetooth. + +## ~hint + +### Eddystone + +Bluetooth beacons are used to indicate proximity to a place or object of interest. +Beacons use Bluetooth advertising to broadcast a small amount of data, +which can be received and acted upon by anyone in range with a suitable device and software, typically a smartphone and application. + +There are various beacon message formats, which define the way Bluetooth advertising packets are used as containers for beacon data. +iBeacon is Apple's beacon message format. Eddystone comes from Google. + +Read more at https://lancaster-university.github.io/microbit-docs/ble/eddystone/ . + +## ~ + +```sig +bluetooth.stopAdvertising(); +``` + +### Example: stop advertising on button pressed + +```blocks +input.onButtonPressed(Button.A, () => { + bluetooth.stopAdvertising(); +}) +``` + +## See Also + +[advertise-url](/reference/bluetooth/advertise-url) + +```package +bluetooth +``` \ No newline at end of file diff --git a/docs/reference/eddystone.md b/docs/reference/eddystone.md new file mode 100644 index 00000000..d1a6f267 --- /dev/null +++ b/docs/reference/eddystone.md @@ -0,0 +1,28 @@ +# Eddystone + +Support for Eddystone beacons. + +```cards +eddystone.advertiseUrl("https://pxt.io/", 6); +eddystone.stopAdvertising(); +``` + +### Advanced + +Bluetooth beacons are used to indicate proximity to a place or object of interest. +Beacons use Bluetooth advertising to broadcast a small amount of data, which can be received and acted upon by anyone in range +with a suitable device and software, typically a smartphone and application. + +There are various beacon message formats, which define the way Bluetooth advertising packets are used as containers for beacon data. +iBeacon is Apple's beacon message format. Eddystone comes from Google. + +More information at https://lancaster-university.github.io/microbit-docs/ble/eddystone/ + +### See Also + +[eddystone.advertiseUrl](/reference/eddystone/advertise-url), +[eddystone.stopAdvertising](/reference/eddystone/stop-advertising), + +```package +eddystone +``` diff --git a/libs/bluetooth/_locales/bluetooth-jsdoc-strings.json b/libs/bluetooth/_locales/bluetooth-jsdoc-strings.json index ca3ba8ef..b22f5b9c 100644 --- a/libs/bluetooth/_locales/bluetooth-jsdoc-strings.json +++ b/libs/bluetooth/_locales/bluetooth-jsdoc-strings.json @@ -1,5 +1,8 @@ { "bluetooth": "Support for additional Bluetooth services.", + "bluetooth.advertiseUrl": "Advertise an Eddystone URL", + "bluetooth.advertiseUrl|param|power": "power level between 0 and 7, e.g.: 7", + "bluetooth.advertiseUrl|param|url": "the url to transmit. Must be no longer than the supported eddystone url length", "bluetooth.onBluetoothConnected": "Register code to run when the micro:bit is connected to over Bluetooth", "bluetooth.onBluetoothConnected|param|body": "Code to run when a Bluetooth connection is established", "bluetooth.onBluetoothDisconnected": "Register code to run when a bluetooth connection to the micro:bit is lost", @@ -11,6 +14,7 @@ "bluetooth.startMagnetometerService": "Starts the Bluetooth magnetometer service", "bluetooth.startTemperatureService": "Starts the Bluetooth temperature service", "bluetooth.startUartService": "Starts the Bluetooth UART service", + "bluetooth.stopAdvertising": "Stops advertising Eddystone end points", "bluetooth.uartReadUntil": "Reads from the Bluetooth UART service buffer, returning its contents when the specified delimiter character is encountered.", "bluetooth.uartWriteNumber": "Prints a numeric value to the serial", "bluetooth.uartWriteString": "Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device.", diff --git a/libs/bluetooth/_locales/bluetooth-strings.json b/libs/bluetooth/_locales/bluetooth-strings.json index ad894e0c..f4ac4022 100644 --- a/libs/bluetooth/_locales/bluetooth-strings.json +++ b/libs/bluetooth/_locales/bluetooth-strings.json @@ -1,4 +1,5 @@ { + "bluetooth.advertiseUrl|block": "bluetooth advertise url %url|with power %power", "bluetooth.onBluetoothConnected|block": "on bluetooth connected", "bluetooth.onBluetoothDisconnected|block": "on bluetooth disconnected", "bluetooth.startAccelerometerService|block": "bluetooth accelerometer service", @@ -8,6 +9,7 @@ "bluetooth.startMagnetometerService|block": "bluetooth magnetometer service", "bluetooth.startTemperatureService|block": "bluetooth temperature service", "bluetooth.startUartService|block": "bluetooth uart service", + "bluetooth.stopAdvertising|block": "bluetooth stop advertising", "bluetooth.uartReadUntil|block": "bluetooth uart|read until %del=serial_delimiter_conv", "bluetooth.uartWriteNumber|block": "bluetooth uart|write number %value", "bluetooth.uartWriteString|block": "bluetooth uart|write string %data", diff --git a/libs/bluetooth/bluetooth.cpp b/libs/bluetooth/bluetooth.cpp index 007f5518..37d217ac 100644 --- a/libs/bluetooth/bluetooth.cpp +++ b/libs/bluetooth/bluetooth.cpp @@ -11,7 +11,6 @@ using namespace pxt; namespace bluetooth { MicroBitUARTService *uart = NULL; - /** * Starts the Bluetooth accelerometer service */ @@ -119,5 +118,30 @@ namespace bluetooth { //% parts="bluetooth" void onBluetoothDisconnected(Action body) { registerWithDal(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, body); - } + } + + const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10}; + + /** + * Advertise an Eddystone URL + * @param url the url to transmit. Must be no longer than the supported eddystone url length + * @param power power level between 0 and 7, e.g.: 7 + */ + //% blockId=eddystone_advertise_url block="bluetooth advertise url %url|with power %power" + //% parts=bluetooth weight=11 blockGap=8 + //% help=bluetooth/advertise-url + void advertiseUrl(StringData* url, int power) { + int8_t level = CALIBRATED_POWERS[min(7, max(0, power))]; + uBit.bleManager.advertiseEddystoneUrl(ManagedString(url), level); + } + + /** + * Stops advertising Eddystone end points + */ + //% blockId=eddystone_stop_advertising block="bluetooth stop advertising" + //% parts=bluetooth weight=10 + //% help=bluetooth/stop-advertising + void stopAdvertising() { + uBit.bleManager.stopAdvertising(); + } } \ No newline at end of file diff --git a/libs/bluetooth/pxt.json b/libs/bluetooth/pxt.json index 51f109d8..43f75849 100644 --- a/libs/bluetooth/pxt.json +++ b/libs/bluetooth/pxt.json @@ -16,19 +16,28 @@ "config": { "microbit-dal": { "bluetooth": { - "enabled": 1, - "pairing_mode": 1, + "enabled": 1 + } + } + }, + "optionalConfig": { + "microbit-dal": { + "bluetooth": { "private_addressing": 0, - "open": 0, "whitelist": 1, "advertising_timeout": 0, "tx_power": 6, "dfu_service": 1, "event_service": 1, - "device_info_service": 1 - }, - "gatt_table_size": "0x700" - } + "device_info_service": 1, + "eddystone_url": 1, + "eddystone_uid": 0, + "pairing_mode": 1, + "open": 0, + "security_level": "SECURITY_MODE_ENCRYPTION_WITH_MITM" + } + }, + "gatt_table_size": "0x700" } }, "installedVersion": "vzlhfd" diff --git a/libs/bluetooth/shims.d.ts b/libs/bluetooth/shims.d.ts index 58c52fa9..57d9e45b 100644 --- a/libs/bluetooth/shims.d.ts +++ b/libs/bluetooth/shims.d.ts @@ -80,6 +80,24 @@ declare namespace bluetooth { //% blockId=bluetooth_on_disconnected block="on bluetooth disconnected" //% parts="bluetooth" shim=bluetooth::onBluetoothDisconnected function onBluetoothDisconnected(body: () => void): void; + + /** + * Advertise an Eddystone URL + * @param url the url to transmit. Must be no longer than the supported eddystone url length + * @param power power level between 0 and 7, e.g.: 7 + */ + //% blockId=eddystone_advertise_url block="bluetooth advertise url %url|with power %power" + //% parts=bluetooth weight=11 blockGap=8 + //% help=bluetooth/advertise-url shim=bluetooth::advertiseUrl + function advertiseUrl(url: string, power: number): void; + + /** + * Stops advertising Eddystone end points + */ + //% blockId=eddystone_stop_advertising block="bluetooth stop advertising" + //% parts=bluetooth weight=10 + //% help=bluetooth/stop-advertising shim=bluetooth::stopAdvertising + function stopAdvertising(): void; } // Auto-generated. Do not edit. Really. diff --git a/libs/core/dal.d.ts b/libs/core/dal.d.ts index 7e7f4134..6dae7e1f 100644 --- a/libs/core/dal.d.ts +++ b/libs/core/dal.d.ts @@ -80,13 +80,13 @@ declare const enum DAL { MICROBIT_BLE_POWER_LEVELS = 8, MICROBIT_BLE_MAXIMUM_BONDS = 4, MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL = 400, + MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER = 0xF0, // built/yt/yotta_modules/microbit-dal/inc/bluetooth/MicroBitButtonService.h // built/yt/yotta_modules/microbit-dal/inc/bluetooth/MicroBitDFUService.h MICROBIT_DFU_OPCODE_START_DFU = 1, MICROBIT_DFU_HISTOGRAM_WIDTH = 5, MICROBIT_DFU_HISTOGRAM_HEIGHT = 5, // built/yt/yotta_modules/microbit-dal/inc/bluetooth/MicroBitEddystone.h - MICROBIT_BLE_EDDYSTONE_URL_ADV_INTERVAL = 400, // built/yt/yotta_modules/microbit-dal/inc/bluetooth/MicroBitEventService.h // built/yt/yotta_modules/microbit-dal/inc/bluetooth/MicroBitIOPinService.h MICROBIT_IO_PIN_SERVICE_PINCOUNT = 19, diff --git a/libs/core/pxt.json b/libs/core/pxt.json index b79d7634..bca70b9e 100644 --- a/libs/core/pxt.json +++ b/libs/core/pxt.json @@ -42,8 +42,7 @@ "public": true, "dependencies": {}, "yotta": { - "configIsJustDefaults": true, - "config": { + "optionalConfig": { "microbit-dal": { "bluetooth": { "enabled": 0 diff --git a/pxtarget.json b/pxtarget.json index bf3ed74e..cf55e6ca 100644 --- a/pxtarget.json +++ b/pxtarget.json @@ -162,7 +162,7 @@ "yottaTarget": "bbc-microbit-classic-gcc", "yottaCorePackage": "microbit", "githubCorePackage": "lancaster-university/microbit", - "gittag": "v2.0.0-rc6", + "gittag": "v2.0.0-rc7", "serviceId": "microbit" }, "serial": { diff --git a/sim/state/misc.ts b/sim/state/misc.ts index d2d20061..521e9a05 100644 --- a/sim/state/misc.ts +++ b/sim/state/misc.ts @@ -134,7 +134,7 @@ namespace pxsim.devices { export function onSignalStrengthChanged(action: number) { // TODO } - export function signalStrength() : number { + export function signalStrength(): number { // TODO return 0; } @@ -165,18 +165,19 @@ namespace pxsim.bluetooth { export function startUartService(): void { // TODO } - export function uartWrite(s : string): void { - // TODO + export function uartWrite(s: string): void { + serial.writeString(s) } export function uartReadUntil(del: string): string { - // TODO - return "" + return serial.readUntil(del); } - export function onBluetoothConnected(a : RefAction) { + export function onBluetoothConnected(a: RefAction) { // TODO } - export function onBluetoothDisconnected(a : RefAction) { + export function onBluetoothDisconnected(a: RefAction) { // TODO } + export function advertiseUrl(url: string, power: number) { } + export function stopAdvertising() { } }