Open this page at https://nix0n.github.io/microbit-radio-send-object/
Micro:bit Radio Send Object Extension
This extension extends the built-in radio namespace by providing new functions like radio.sendObject()
and radio.onReceivedObject()
.
The radio.sendObject()
function depends on the built-in radio.sendString()
function to send a JSON encoding of the passed value. Micro:bit’s built-in radio.sendString() has a limit of 19 characters. To work around this, radio.sendObject() uses a combination of special control characters (STX, ETX) and maximally sized chunks sent by radio.sendString(); which allows for near limitless object size.
Notice: It’s recommended that you enable serial number transmission, using radio.setTransmitSerialNumber(true)
. This allows for multiple devices simultaneously sending streams. It also provides loopback detection in the rare instance the receive event is on the same device it was transmitted.
Use as Extension
This repository can be added as an extension in MakeCode.
- open https://makecode.microbit.org/
- click on New Project
- click on Extensions under the gearwheel menu
- search for https://github.com/nix0n/microbit-radio-send-object and import
Import this project
To Import this repository in MakeCode.
- open https://makecode.microbit.org/
- click on Import then click on Import URL
- paste https://github.com/nix0n/microbit-radio-send-object and click import
Usage
radio.sendObject(value)
Send object over radio
PARAMETERS
value
:any
- Object to be sent
RETURNS
void
EXAMPLE
// Notice: JSON encoding longer than 19 characters
radio.sendObject({ foo: 'bar', baz: true })
radio.onReceivedObject(cb)
Register event handler to listen for received object
PARAMETERS
cb
:(receivedObject, props) => void
- CallbackreceivedObject
:any
- Object sent over radioprops
:number[]
- List of packet properties accessible using RadioPacketProperty.* enumeration
RETURNS
void
EXAMPLE
radio.onReceivedObject(function (receivedObject: any, props: number[]) {
console.log(`Object was sent by: ${props[RadioPacketProperty.SerialNumber]}`)
console.log(`Object received: ${JSON.stringify(receivedObject)}`)
})
Other
License
This code is made available under the the MIT License.
Metadata (used for search, rendering)
- for PXT/microbit