diff --git a/src/extensions/scratch3_ev3/index.js b/src/extensions/scratch3_ev3/index.js index 667aaf5e8..ed47ffb5c 100644 --- a/src/extensions/scratch3_ev3/index.js +++ b/src/extensions/scratch3_ev3/index.js @@ -476,6 +476,7 @@ class EV3 { this._bt = null; this._runtime.registerPeripheralExtension(extensionId, this); + this.disconnect = this.disconnect.bind(this); this._onConnect = this._onConnect.bind(this); this._onMessage = this._onMessage.bind(this); this._pollValues = this._pollValues.bind(this); @@ -561,7 +562,7 @@ class EV3 { this._bt = new BT(this._runtime, this._extensionId, { majorDeviceClass: 8, minorDeviceClass: 1 - }, this._onConnect, this._onMessage); + }, this._onConnect, this.disconnect, this._onMessage); } /** diff --git a/src/extensions/scratch3_microbit/index.js b/src/extensions/scratch3_microbit/index.js index 3e880d8cd..b8fc23966 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); + }, this._onConnect, null); } /** diff --git a/src/extensions/scratch3_wedo2/index.js b/src/extensions/scratch3_wedo2/index.js index be285dee9..3edb87840 100644 --- a/src/extensions/scratch3_wedo2/index.js +++ b/src/extensions/scratch3_wedo2/index.js @@ -435,6 +435,7 @@ class WeDo2 { */ this._batteryLevelIntervalId = null; + this.disconnect = this.disconnect.bind(this); this._onConnect = this._onConnect.bind(this); this._onMessage = this._onMessage.bind(this); this._checkBatteryLevel = this._checkBatteryLevel.bind(this); @@ -593,7 +594,7 @@ class WeDo2 { services: [BLEService.DEVICE_SERVICE] }], optionalServices: [BLEService.IO_SERVICE] - }, this._onConnect); + }, this._onConnect, this.disconnect); } /** @@ -623,7 +624,6 @@ class WeDo2 { } if (this._batteryLevelIntervalId) { - console.log('clearing batterylevelintervalid'); window.clearInterval(this._batteryLevelIntervalId); this._batteryLevelIntervalId = null; } diff --git a/src/io/ble.js b/src/io/ble.js index 1d9a208a9..afe384a7b 100644 --- a/src/io/ble.js +++ b/src/io/ble.js @@ -11,8 +11,9 @@ class BLE extends JSONRPCWebSocket { * @param {string} extensionId - the id of the extension using this socket. * @param {object} peripheralOptions - the list of options for peripheral discovery. * @param {object} connectCallback - a callback for connection. + * @param {object} disconnectCallback - a callback for disconnection. */ - constructor (runtime, extensionId, peripheralOptions, connectCallback) { + constructor (runtime, extensionId, peripheralOptions, connectCallback, disconnectCallback = null) { const ws = new WebSocket(ScratchLinkWebSocket); super(ws); @@ -25,9 +26,10 @@ class BLE extends JSONRPCWebSocket { this._connectCallback = connectCallback; this._connected = false; this._characteristicDidChangeCallback = null; + this._disconnectCallback = disconnectCallback; + this._discoverTimeoutID = null; this._extensionId = extensionId; this._peripheralOptions = peripheralOptions; - this._discoverTimeoutID = null; this._runtime = runtime; } @@ -190,6 +192,9 @@ class BLE extends JSONRPCWebSocket { if (!this._connected) return; this._connected = false; + if (this._disconnectCallback) { + this._disconnectCallback(); + } this._runtime.emit(this._runtime.constructor.PERIPHERAL_DISCONNECT_ERROR, { message: `Scratch lost connection to`, diff --git a/src/io/bt.js b/src/io/bt.js index 4c901bd87..bf393f76a 100644 --- a/src/io/bt.js +++ b/src/io/bt.js @@ -11,9 +11,10 @@ class BT extends JSONRPCWebSocket { * @param {string} extensionId - the id of the extension using this socket. * @param {object} peripheralOptions - the list of options for peripheral discovery. * @param {object} connectCallback - a callback for connection. + * @param {object} disconnectCallback - a callback for disconnection. * @param {object} messageCallback - a callback for message sending. */ - constructor (runtime, extensionId, peripheralOptions, connectCallback, messageCallback) { + constructor (runtime, extensionId, peripheralOptions, connectCallback, disconnectCallback = null, messageCallback) { const ws = new WebSocket(ScratchLinkWebSocket); super(ws); @@ -26,9 +27,10 @@ class BT extends JSONRPCWebSocket { this._connectCallback = connectCallback; this._connected = false; this._characteristicDidChangeCallback = null; + this._disconnectCallback = disconnectCallback; + this._discoverTimeoutID = null; this._extensionId = extensionId; this._peripheralOptions = peripheralOptions; - this._discoverTimeoutID = null; this._messageCallback = messageCallback; this._runtime = runtime; } @@ -135,6 +137,9 @@ class BT extends JSONRPCWebSocket { if (!this._connected) return; this._connected = false; + if (this._disconnectCallback) { + this._disconnectCallback(); + } this._runtime.emit(this._runtime.constructor.PERIPHERAL_DISCONNECT_ERROR, { message: `Scratch lost connection to`,