Constructor
new MicroBit(runtime, extensionId)
Construct a MicroBit communication object.
Parameters:
Name | Type | Description |
---|---|---|
runtime |
Runtime | the Scratch 3.0 runtime |
extensionId |
string | the id of the extension |
Members
(private) _ble :BLE
The BluetoothLowEnergy connection socket for reading/writing peripheral data.
Type:
(private) _busy :boolean
A flag that is true while we are busy sending data to the BLE socket.
Type:
- boolean
_busyTimeoutID
ID for a timeout which is used to clear the busy flag if it has been true for a long time.
_extensionId
The id of the extension this peripheral belongs to.
(private) _gestures :Object.<string, Object>
The most recently received value for each gesture.
Type:
- Object.<string, Object>
(private) _runtime :Runtime
The Scratch 3.0 runtime used to trigger the green flag button.
Type:
(private) _sensors :Object.<string, number>
The most recently received value for each sensor.
Type:
- Object.<string, number>
(private) _timeoutID :number
Interval ID for data reading timeout.
Type:
- number
buttonA
buttonB
gestureState
ledMatrixState
tiltX
tiltY
Methods
(private) _checkPinState(pin) → {number}
Parameters:
Name | Type | Description |
---|---|---|
pin |
number | the pin to check touch state. |
Returns:
- the latest value received for the touch pin states.
- Type
- number
(private) _onConnect()
Starts reading data from peripheral after BLE has connected to it.
(private) _onMessage(base64)
Process the sensor data from the incoming BLE characteristic.
Parameters:
Name | Type | Description |
---|---|---|
base64 |
object | the incoming BLE data. |
connect(id)
Called by the runtime when user wants to connect to a certain peripheral.
Parameters:
Name | Type | Description |
---|---|---|
id |
number | the id of the peripheral to connect to. |
disconnect()
Disconnect from the micro:bit.
displayMatrix(matrix) → {Promise}
Parameters:
Name | Type | Description |
---|---|---|
matrix |
Uint8Array | the matrix to display. |
Returns:
- a Promise that resolves when writing to peripheral.
- Type
- Promise
displayText(text) → {Promise}
Parameters:
Name | Type | Description |
---|---|---|
text |
string | the text to display. |
Returns:
- a Promise that resolves when writing to peripheral.
- Type
- Promise
isConnected() → {boolean}
Return true if connected to the micro:bit.
Returns:
- whether the micro:bit is connected.
- Type
- boolean
reset()
Reset all the state and timeout/interval ids.
scan()
Called by the runtime when user wants to scan for a peripheral.
send(command, message)
Send a message to the peripheral BLE socket.
Parameters:
Name | Type | Description |
---|---|---|
command |
number | the BLE command hex. |
message |
Uint8Array | the message to write |