diff --git a/src/extensions/scratch3_boost/index.js b/src/extensions/scratch3_boost/index.js index 44b48fec3..217f3d8d1 100644 --- a/src/extensions/scratch3_boost/index.js +++ b/src/extensions/scratch3_boost/index.js @@ -827,6 +827,7 @@ class Boost { */ disconnect () { console.log('BOOST DISCONNECT CALLED'); + if (this._ble) { this._ble.disconnect(); } @@ -839,6 +840,7 @@ class Boost { */ reset () { console.log('BOOST RESET CALLED'); + this._ports = []; this._motors = []; this._sensors = { diff --git a/src/extensions/scratch3_ev3/index.js b/src/extensions/scratch3_ev3/index.js index cf5053f9e..eb8636aa5 100644 --- a/src/extensions/scratch3_ev3/index.js +++ b/src/extensions/scratch3_ev3/index.js @@ -601,6 +601,7 @@ class EV3 { */ disconnect() { console.log('EV3 DISCONNECT CALLED'); + if (this._bt) { this._bt.disconnect(); } @@ -613,6 +614,7 @@ class EV3 { */ reset() { console.log('EV3 RESET CALLED'); + this._sensorPorts = []; this._motorPorts = []; this._sensors = { diff --git a/src/extensions/scratch3_gdx_for/index.js b/src/extensions/scratch3_gdx_for/index.js index e201a7907..e019b00b8 100644 --- a/src/extensions/scratch3_gdx_for/index.js +++ b/src/extensions/scratch3_gdx_for/index.js @@ -172,7 +172,7 @@ class GdxFor { */ this._timeoutID = null; - this.disconnect = this.disconnect.bind(this); + this.reset = this.reset.bind(this); this._onConnect = this._onConnect.bind(this); } @@ -192,7 +192,7 @@ class GdxFor { optionalServices: [ BLEUUID.service ] - }, this._onConnect, this.disconnect); + }, this._onConnect, this.reset); } /** @@ -209,8 +209,22 @@ class GdxFor { * Called by the runtime when a user exits the connection popup. * Disconnect from the GDX FOR. */ - disconnect () { - window.clearInterval(this._timeoutID); + disconnect() { + console.log('GDXFOR DISCONNECT CALLED'); + + if (this._scratchLinkSocket) { + this._scratchLinkSocket.disconnect(); + } + + this.reset(); + } + + /** + * Reset all the state and timeout/interval ids. + */ + reset () { + console.log('GDXFOR RESET CALLED'); + this._sensors = { force: 0, accelerationX: 0, @@ -220,9 +234,8 @@ class GdxFor { spinSpeedY: 0, spinSpeedZ: 0 }; - if (this._scratchLinkSocket) { - this._scratchLinkSocket.disconnect(); - } + + window.clearInterval(this._timeoutID); } /** diff --git a/src/extensions/scratch3_microbit/index.js b/src/extensions/scratch3_microbit/index.js index 29b5d790a..8074e5bf9 100644 --- a/src/extensions/scratch3_microbit/index.js +++ b/src/extensions/scratch3_microbit/index.js @@ -144,7 +144,7 @@ class MicroBit { */ this._busyTimeoutID = null; - this.disconnect = this.disconnect.bind(this); + this.reset = this.reset.bind(this); this._onConnect = this._onConnect.bind(this); this._onMessage = this._onMessage.bind(this); } @@ -222,7 +222,7 @@ class MicroBit { filters: [ {services: [BLEUUID.service]} ] - }, this._onConnect, this.disconnect); + }, this._onConnect, this.reset); } /** @@ -238,11 +238,23 @@ class MicroBit { /** * Disconnect from the micro:bit. */ - disconnect () { - window.clearTimeout(this._timeoutID); + disconnect() { + console.log('MICROBIT DISCONNECT CALLED'); + if (this._ble) { this._ble.disconnect(); } + + this.reset(); + } + + /** + * Reset all the state and timeout/interval ids. + */ + reset() { + console.log('MICROBIT RESET CALLED'); + + window.clearTimeout(this._timeoutID); } /** diff --git a/src/extensions/scratch3_wedo2/index.js b/src/extensions/scratch3_wedo2/index.js index e08c987fc..9bd222b5d 100644 --- a/src/extensions/scratch3_wedo2/index.js +++ b/src/extensions/scratch3_wedo2/index.js @@ -435,7 +435,7 @@ class WeDo2 { */ this._batteryLevelIntervalId = null; - this.disconnect = this.disconnect.bind(this); + this.reset = this.reset.bind(this); this._onConnect = this._onConnect.bind(this); this._onMessage = this._onMessage.bind(this); this._checkBatteryLevel = this._checkBatteryLevel.bind(this); @@ -594,7 +594,7 @@ class WeDo2 { services: [BLEService.DEVICE_SERVICE] }], optionalServices: [BLEService.IO_SERVICE] - }, this._onConnect, this.disconnect); + }, this._onConnect, this.reset); } /** @@ -610,7 +610,22 @@ class WeDo2 { /** * Disconnects from the current BLE socket. */ - disconnect () { + disconnect() { + console.log('WEDO2 DISCONNECT CALLED'); + + if (this._ble) { + this._ble.disconnect(); + } + + this.reset(); + } + + /** + * Reset all the state and timeout/interval ids. + */ + reset() { + console.log('WEDO2 RESET CALLED'); + this._ports = ['none', 'none']; this._motors = [null, null]; this._sensors = { @@ -619,10 +634,6 @@ class WeDo2 { distance: 0 }; - if (this._ble) { - this._ble.disconnect(); - } - if (this._batteryLevelIntervalId) { window.clearInterval(this._batteryLevelIntervalId); this._batteryLevelIntervalId = null; diff --git a/src/io/ble.js b/src/io/ble.js index acadeec7d..9816b4b95 100644 --- a/src/io/ble.js +++ b/src/io/ble.js @@ -205,9 +205,9 @@ class BLE extends JSONRPC { */ handleDisconnectError (/* e */) { // log.error(`BLE error: ${JSON.stringify(e)}`); + console.log('BLE HANDLEDISCONNECTERROR CALLED'); if (!this._connected) return; - console.log('BLE HANDLEDISCONNECTERROR CALLED'); this.disconnect();