diff --git a/src/extensions/scratch3_ev3/index.js b/src/extensions/scratch3_ev3/index.js index 9b1bcba02..b11c8d526 100644 --- a/src/extensions/scratch3_ev3/index.js +++ b/src/extensions/scratch3_ev3/index.js @@ -682,6 +682,10 @@ class EV3 { const array = Base64Util.base64ToUint8Array(message); // log.info(`received array: ${array}`); + if (array.length < 35) { // TODO: find safer solution + return; // don't parse results that aren't sensor data list or device list + } + if (this._updateDevices) { // READ DEVICE LIST this._sensorPorts[0] = EV_DEVICE_TYPES[array[5]] ? EV_DEVICE_TYPES[array[5]] : 'none'; diff --git a/src/extensions/scratch3_microbit/index.js b/src/extensions/scratch3_microbit/index.js index 1501380d1..c714f1346 100644 --- a/src/extensions/scratch3_microbit/index.js +++ b/src/extensions/scratch3_microbit/index.js @@ -24,6 +24,8 @@ const BLECommand = { CMD_DISPLAY_LED: 0x82 }; +const BLETimeout = 4500; // TODO: might need tweaking based on how long the device takes to start sending data + /** * Enum for micro:bit protocol. * https://github.com/LLK/scratch-microbit-firmware/blob/master/protocol.md @@ -98,6 +100,13 @@ class MicroBit { timeout: false } }; + + /** + * Interval ID for data reading timeout. + * @type {number} + * @private + */ + this._timeoutID = null; } // TODO: keep here? @@ -122,6 +131,7 @@ class MicroBit { } disconnectSession () { + window.clearInterval(this._timeoutID); this._ble.disconnectSession(); } @@ -209,6 +219,7 @@ class MicroBit { _onSessionConnect () { const callback = this._processSessionData.bind(this); this._ble.read(BLEUUID.service, BLEUUID.rxChar, true, callback); + this._timeoutID = window.setInterval(this.disconnectSession.bind(this), BLETimeout); } /** @@ -217,6 +228,7 @@ class MicroBit { * @private */ _processSessionData (base64) { + // parse data const data = Base64Util.base64ToUint8Array(base64); this._sensors.tiltX = data[1] | (data[0] << 8); @@ -232,6 +244,10 @@ class MicroBit { this._sensors.touchPins[2] = data[8]; this._sensors.gestureState = data[9]; + + // cancel disconnect timeout and start a new one + window.clearInterval(this._timeoutID); + this._timeoutID = window.setInterval(this.disconnectSession.bind(this), BLETimeout); } /** diff --git a/src/io/bleSession.js b/src/io/bleSession.js index 5ba95742f..357b80ef6 100644 --- a/src/io/bleSession.js +++ b/src/io/bleSession.js @@ -1,5 +1,5 @@ const JSONRPCWebSocket = require('../util/jsonrpc-web-socket'); -const log = require('../util/log'); +// const log = require('../util/log'); const ScratchLinkWebSocket = 'wss://device-manager.scratch.mit.edu:20110/scratch/ble'; class BLESession extends JSONRPCWebSocket { @@ -144,9 +144,9 @@ class BLESession extends JSONRPCWebSocket { }); } - _sendError (e) { + _sendError (/* e */) { this._connected = false; - log.error(`BLESession error: ${JSON.stringify(e)}`); + // log.error(`BLESession error: ${JSON.stringify(e)}`); this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR); } diff --git a/src/io/btSession.js b/src/io/btSession.js index f2cc54732..849e3032b 100644 --- a/src/io/btSession.js +++ b/src/io/btSession.js @@ -1,5 +1,5 @@ const JSONRPCWebSocket = require('../util/jsonrpc-web-socket'); -const log = require('../util/log'); +// const log = require('../util/log'); const ScratchLinkWebSocket = 'wss://device-manager.scratch.mit.edu:20110/scratch/bt'; class BTSession extends JSONRPCWebSocket { @@ -112,9 +112,9 @@ class BTSession extends JSONRPCWebSocket { } } - _sendError (e) { + _sendError (/* e */) { this._connected = false; - log.error(`BTSession error: ${JSON.stringify(e)}`); + // log.error(`BTSession error: ${JSON.stringify(e)}`); this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR); }