diff --git a/src/extensions/scratch3_microbit/index.js b/src/extensions/scratch3_microbit/index.js index f5f0da524..f0115135a 100644 --- a/src/extensions/scratch3_microbit/index.js +++ b/src/extensions/scratch3_microbit/index.js @@ -73,6 +73,8 @@ class MicroBit { this._ble = null; this._runtime.registerPeripheralExtension(extensionId, this); + this._extensionId = extensionId; + /** * The most recently received value for each sensor. * @type {Object.} @@ -132,6 +134,15 @@ class MicroBit { this.disconnect = this.disconnect.bind(this); this._onConnect = this._onConnect.bind(this); this._onMessage = this._onMessage.bind(this); + + this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, { + message: `Scratch lost connection to peripheral.`, + extensionId: this._extensionId + }); + this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, { + message: `Scratch lost connection to peripheral.`, + extensionId: 2 + }); } /** @@ -200,7 +211,7 @@ class MicroBit { * Called by the runtime when user wants to scan for a peripheral. */ scan () { - this._ble = new BLE(this._runtime, 'micro:bit', { + this._ble = new BLE(this._runtime, this._extensionId, { filters: [ {services: [BLEUUID.service]} ] diff --git a/src/io/ble.js b/src/io/ble.js index 60f5277a8..f5a681ba8 100644 --- a/src/io/ble.js +++ b/src/io/ble.js @@ -8,11 +8,11 @@ class BLE extends JSONRPCWebSocket { * A BLE peripheral socket object. It handles connecting, over web sockets, to * BLE peripherals, and reading and writing data to them. * @param {Runtime} runtime - the Runtime for sending/receiving GUI update events. - * @param {string} peripheralType - the type of peripheral. + * @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. */ - constructor (runtime, peripheralType, peripheralOptions, connectCallback) { + constructor (runtime, extensionId, peripheralOptions, connectCallback) { const ws = new WebSocket(ScratchLinkWebSocket); super(ws); @@ -25,8 +25,8 @@ class BLE extends JSONRPCWebSocket { this._connectCallback = connectCallback; this._connected = false; this._characteristicDidChangeCallback = null; + this._extensionId = extensionId; this._peripheralOptions = peripheralOptions; - this._peripheralType = peripheralType; this._discoverTimeoutID = null; this._runtime = runtime; } @@ -175,7 +175,8 @@ class BLE extends JSONRPCWebSocket { this.disconnect(); // log.error(`BLE error: ${JSON.stringify(e)}`); this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, { - message: `Scratch lost connection to ${this._peripheralType}.` + message: `Scratch lost connection to ${this._peripheralType}.`, + extensionId: this._extensionId }); } diff --git a/src/io/bt.js b/src/io/bt.js index ac8863d80..41438ec00 100644 --- a/src/io/bt.js +++ b/src/io/bt.js @@ -8,12 +8,12 @@ class BT extends JSONRPCWebSocket { * A BT peripheral socket object. It handles connecting, over web sockets, to * BT peripherals, and reading and writing data to them. * @param {Runtime} runtime - the Runtime for sending/receiving GUI update events. - * @param {string} peripheralType - the type of peripheral. + * @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} messageCallback - a callback for message sending. */ - constructor (runtime, peripheralType, peripheralOptions, connectCallback, messageCallback) { + constructor (runtime, extensionId, peripheralOptions, connectCallback, messageCallback) { const ws = new WebSocket(ScratchLinkWebSocket); super(ws); @@ -26,8 +26,8 @@ class BT extends JSONRPCWebSocket { this._connectCallback = connectCallback; this._connected = false; this._characteristicDidChangeCallback = null; + this._extensionId = extensionId; this._peripheralOptions = peripheralOptions; - this._peripheralType = peripheralType; this._discoverTimeoutID = null; this._messageCallback = messageCallback; this._runtime = runtime; diff --git a/src/virtual-machine.js b/src/virtual-machine.js index e61ceaa6d..5be8a553c 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -111,8 +111,8 @@ class VirtualMachine extends EventEmitter { this.runtime.on(Runtime.PERIPHERAL_CONNECTED, () => this.emit(Runtime.PERIPHERAL_CONNECTED) ); - this.runtime.on(Runtime.PERIPHERAL_ERROR, message => - this.emit(Runtime.PERIPHERAL_ERROR, message) + this.runtime.on(Runtime.PERIPHERAL_ERROR, data => + this.emit(Runtime.PERIPHERAL_ERROR, data) ); this.runtime.on(Runtime.PERIPHERAL_SCAN_TIMEOUT, () => this.emit(Runtime.PERIPHERAL_SCAN_TIMEOUT)