Add disconnectExtensionSession and getPeripheralIsConnected

This commit is contained in:
Eric Rosenbaum 2018-06-27 14:21:11 -04:00 committed by Ray Schamp
parent 613a9f96c3
commit 6f5ff31eb3
5 changed files with 72 additions and 0 deletions

View file

@ -909,6 +909,20 @@ class Runtime extends EventEmitter {
}
}
disconnectExtensionSession (extensionId) {
if (this.extensionDevices[extensionId]) {
this.extensionDevices[extensionId].disconnectSession();
}
}
getPeripheralIsConnected (extensionId) {
let isConnected = false;
if (this.extensionDevices[extensionId]) {
isConnected = this.extensionDevices[extensionId].getPeripheralIsConnected();
}
return isConnected;
}
/**
* Retrieve the function associated with the given opcode.
* @param {!string} opcode The opcode to look up.

View file

@ -128,6 +128,18 @@ class MicroBit {
this._ble.connectDevice(id);
}
disconnectSession () {
this._ble.disconnectSession();
}
getPeripheralIsConnected () {
let connected = false;
if (this._ble) {
connected = this._ble.getPeripheralIsConnected();
}
return connected;
}
/**
* @param {string} text - the text to display.
* @return {Promise} - a Promise that resolves when writing to device.

View file

@ -25,6 +25,8 @@ class BLESession extends JSONRPCWebSocket {
this._characteristicDidChangeCallback = null;
this._deviceOptions = deviceOptions;
this._runtime = runtime;
this._connected = false;
}
/**
@ -50,6 +52,7 @@ class BLESession extends JSONRPCWebSocket {
.then(() => {
log.info('should have connected');
this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTED);
this._connected = true;
this._connectCallback();
})
.catch(e => {
@ -57,6 +60,21 @@ class BLESession extends JSONRPCWebSocket {
});
}
/**
* Close the websocket.
*/
disconnectSession () {
this._ws.close();
this._connected = false;
}
/**
* @return {bool} whether the peripheral is connected.
*/
getPeripheralIsConnected () {
return this._connected;
}
/**
* Handle a received call from the socket.
* @param {string} method - a received method label.
@ -119,6 +137,7 @@ class BLESession extends JSONRPCWebSocket {
}
_sendError (e) {
this._connected = false;
log.error(`BLESession error:`);
log.error(e);
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);

View file

@ -26,6 +26,8 @@ class BTSession extends JSONRPCWebSocket {
this._deviceOptions = deviceOptions;
this._messageCallback = messageCallback;
this._runtime = runtime;
this._connected = false;
}
/**
@ -51,6 +53,7 @@ class BTSession extends JSONRPCWebSocket {
.then(() => {
log.info('should have connected');
this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTED);
this._connected = true;
this._connectCallback();
})
.catch(e => {
@ -58,6 +61,22 @@ class BTSession extends JSONRPCWebSocket {
});
}
/**
* Close the websocket.
*/
disconnectSession () {
this._ws.close();
this._connected = false;
}
/**
* @return {bool} whether the peripheral is connected.
*/
getPeripheralIsConnected () {
return this._connected;
}
sendMessage (options) {
return this.sendRemoteRequest('send', options);
}

View file

@ -213,6 +213,14 @@ class VirtualMachine extends EventEmitter {
this.runtime.connectToPeripheral(extensionId, peripheralId);
}
disconnectExtensionSession (extensionId) {
this.runtime.disconnectExtensionSession(extensionId);
}
getPeripheralIsConnected (extensionId) {
return this.runtime.getPeripheralIsConnected(extensionId);
}
/**
* Load a Scratch project from a .sb, .sb2, .sb3 or json string.
* @param {string | object} input A json string, object, or ArrayBuffer representing the project to load.