diff --git a/src/extensions/scratch3_ev3/index.js b/src/extensions/scratch3_ev3/index.js index 41d541d81..667aaf5e8 100644 --- a/src/extensions/scratch3_ev3/index.js +++ b/src/extensions/scratch3_ev3/index.js @@ -346,7 +346,7 @@ class EV3Motor { */ coast () { if (this._power === 0) return; - + const cmd = this._parent.generateCommand( Ev3Command.DIRECT_COMMAND_NO_REPLY, [ @@ -555,6 +555,9 @@ class EV3 { * Called by the runtime when user wants to scan for an EV3 peripheral. */ scan () { + if (this._bt) { + this._bt.disconnect(); + } this._bt = new BT(this._runtime, this._extensionId, { majorDeviceClass: 8, minorDeviceClass: 1 @@ -566,17 +569,22 @@ class EV3 { * @param {number} id - the id of the peripheral to connect to. */ connect (id) { - this._bt.connectPeripheral(id); + if (this._bt) { + this._bt.connectPeripheral(id); + } } /** * Called by the runtime when user wants to disconnect from the EV3 peripheral. */ disconnect () { - this._bt.disconnect(); this._clearSensorsAndMotors(); window.clearInterval(this._pollingIntervalID); this._pollingIntervalID = null; + + if (this._bt) { + this._bt.disconnect(); + } } /** diff --git a/src/extensions/scratch3_microbit/index.js b/src/extensions/scratch3_microbit/index.js index 03b78b57f..3e880d8cd 100644 --- a/src/extensions/scratch3_microbit/index.js +++ b/src/extensions/scratch3_microbit/index.js @@ -205,6 +205,9 @@ class MicroBit { * Called by the runtime when user wants to scan for a peripheral. */ scan () { + if (this._ble) { + this._ble.disconnect(); + } this._ble = new BLE(this._runtime, this._extensionId, { filters: [ {services: [BLEUUID.service]} @@ -217,7 +220,9 @@ class MicroBit { * @param {number} id - the id of the peripheral to connect to. */ connect (id) { - this._ble.connectPeripheral(id); + if (this._ble) { + this._ble.connectPeripheral(id); + } } /** @@ -225,7 +230,9 @@ class MicroBit { */ disconnect () { window.clearInterval(this._timeoutID); - this._ble.disconnect(); + if (this._ble) { + this._ble.disconnect(); + } } /** diff --git a/src/extensions/scratch3_wedo2/index.js b/src/extensions/scratch3_wedo2/index.js index 5781781f9..08e54eead 100644 --- a/src/extensions/scratch3_wedo2/index.js +++ b/src/extensions/scratch3_wedo2/index.js @@ -570,6 +570,9 @@ class WeDo2 { * Called by the runtime when user wants to scan for a WeDo 2.0 peripheral. */ scan () { + if (this._ble) { + this._ble.disconnect(); + } this._ble = new BLE(this._runtime, this._extensionId, { filters: [{ services: [BLEService.DEVICE_SERVICE] @@ -583,7 +586,9 @@ class WeDo2 { * @param {number} id - the id of the peripheral to connect to. */ connect (id) { - this._ble.connectPeripheral(id); + if (this._ble) { + this._ble.connectPeripheral(id); + } } /** @@ -598,7 +603,9 @@ class WeDo2 { distance: 0 }; - this._ble.disconnect(); + if (this._ble) { + this._ble.disconnect(); + } } /** diff --git a/src/io/ble.js b/src/io/ble.js index e407a9856..b7b1c460a 100644 --- a/src/io/ble.js +++ b/src/io/ble.js @@ -42,7 +42,7 @@ class BLE extends JSONRPCWebSocket { this.sendRemoteRequest('discover', this._peripheralOptions) .catch(e => { this._sendRequestError(e); - }); // never reached? + }); } // TODO: else? } diff --git a/src/io/bt.js b/src/io/bt.js index 4f2f834eb..ea7ed4557 100644 --- a/src/io/bt.js +++ b/src/io/bt.js @@ -42,7 +42,9 @@ class BT extends JSONRPCWebSocket { this._availablePeripherals = {}; this._discoverTimeoutID = window.setTimeout(this._sendDiscoverTimeout.bind(this), 15000); this.sendRemoteRequest('discover', this._peripheralOptions) - .catch(e => this._sendRequestError(e)); // never reached? + .catch( + e => this._sendRequestError(e) + ); } // TODO: else? }