Change microbit extension to send BLE socket error on data lost, instead of calling disconnect directly.

This commit is contained in:
Evelyn Eastmond 2018-11-18 13:31:37 -05:00
parent fab292889f
commit fdda1e53e3
5 changed files with 21 additions and 6 deletions
src
extensions
scratch3_ev3
scratch3_microbit
scratch3_wedo2
io

View file

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

View file

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

View file

@ -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 = {

View file

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

View file

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