Adding disconnect callback to BT/BLE error system.

This commit is contained in:
Evelyn Eastmond 2018-11-18 10:59:57 -05:00
parent a177b4eeb7
commit fab292889f
5 changed files with 19 additions and 8 deletions

View file

@ -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);
}
/**

View file

@ -212,7 +212,7 @@ class MicroBit {
filters: [
{services: [BLEUUID.service]}
]
}, this._onConnect);
}, this._onConnect, null);
}
/**

View file

@ -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;
}

View file

@ -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`,

View file

@ -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`,