diff --git a/src/extensions/scratch3_ev3/index.js b/src/extensions/scratch3_ev3/index.js index ed47ffb5..7fa7d449 100644 --- a/src/extensions/scratch3_ev3/index.js +++ b/src/extensions/scratch3_ev3/index.js @@ -579,6 +579,7 @@ class EV3 { * Called by the runtime when user wants to disconnect from the EV3 peripheral. */ disconnect () { + console.log('EV3 DISCONNECT'); this._clearSensorsAndMotors(); window.clearInterval(this._pollingIntervalID); this._pollingIntervalID = null; diff --git a/src/extensions/scratch3_microbit/index.js b/src/extensions/scratch3_microbit/index.js index b8fc2396..9392bff8 100644 --- a/src/extensions/scratch3_microbit/index.js +++ b/src/extensions/scratch3_microbit/index.js @@ -212,7 +212,7 @@ class MicroBit { filters: [ {services: [BLEUUID.service]} ] - }, this._onConnect, null); + }, this._onConnect, this.disconnect); } /** @@ -229,6 +229,7 @@ class MicroBit { * Disconnect from the micro:bit. */ disconnect () { + console.log('MICROBIT DISCONNECT'); window.clearInterval(this._timeoutID); if (this._ble) { this._ble.disconnect(); @@ -317,7 +318,11 @@ class MicroBit { // cancel disconnect timeout and start a new one window.clearInterval(this._timeoutID); - this._timeoutID = window.setInterval(this.disconnect, BLETimeout); + this._timeoutID = window.setInterval( + // send an error to the BLE socket + this._ble._sendDisconnectError.bind(this._ble, 'micro:bit stopped receiving data'), + BLETimeout + ); } /** diff --git a/src/extensions/scratch3_wedo2/index.js b/src/extensions/scratch3_wedo2/index.js index 3edb8784..8ea69c8b 100644 --- a/src/extensions/scratch3_wedo2/index.js +++ b/src/extensions/scratch3_wedo2/index.js @@ -611,6 +611,7 @@ class WeDo2 { * Disconnects from the current BLE socket. */ disconnect () { + console.log('WEDO2 DISCONNECT'); this._ports = ['none', 'none']; this._motors = [null, null]; this._sensors = { diff --git a/src/io/ble.js b/src/io/ble.js index afe384a7..ce493020 100644 --- a/src/io/ble.js +++ b/src/io/ble.js @@ -73,7 +73,10 @@ class BLE extends JSONRPCWebSocket { * Close the websocket. */ disconnect () { + if (!this._connected) return; + this._ws.close(); + this._connected = false; if (this._discoverTimeoutID) { window.clearTimeout(this._discoverTimeoutID); } @@ -191,9 +194,10 @@ class BLE extends JSONRPCWebSocket { if (!this._connected) return; - this._connected = false; if (this._disconnectCallback) { - this._disconnectCallback(); + this._disconnectCallback(); // will trigger a disconnect() + } else { + this.disconnect(); } this._runtime.emit(this._runtime.constructor.PERIPHERAL_DISCONNECT_ERROR, { diff --git a/src/io/bt.js b/src/io/bt.js index bf393f76..356e04ee 100644 --- a/src/io/bt.js +++ b/src/io/bt.js @@ -75,7 +75,10 @@ class BT extends JSONRPCWebSocket { * Close the websocket. */ disconnect () { + if (!this._connected) return; + this._ws.close(); + this._connected = false; if (this._discoverTimeoutID) { window.clearTimeout(this._discoverTimeoutID); } @@ -136,9 +139,10 @@ class BT extends JSONRPCWebSocket { if (!this._connected) return; - this._connected = false; if (this._disconnectCallback) { - this._disconnectCallback(); + this._disconnectCallback(); // will trigger a disconnect() + } else { + this.disconnect(); } this._runtime.emit(this._runtime.constructor.PERIPHERAL_DISCONNECT_ERROR, {