mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-25 07:22:33 -05:00
Add disconnectExtensionSession and getPeripheralIsConnected
This commit is contained in:
parent
613a9f96c3
commit
6f5ff31eb3
5 changed files with 72 additions and 0 deletions
|
@ -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.
|
* Retrieve the function associated with the given opcode.
|
||||||
* @param {!string} opcode The opcode to look up.
|
* @param {!string} opcode The opcode to look up.
|
||||||
|
|
|
@ -128,6 +128,18 @@ class MicroBit {
|
||||||
this._ble.connectDevice(id);
|
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.
|
* @param {string} text - the text to display.
|
||||||
* @return {Promise} - a Promise that resolves when writing to device.
|
* @return {Promise} - a Promise that resolves when writing to device.
|
||||||
|
|
|
@ -25,6 +25,8 @@ class BLESession extends JSONRPCWebSocket {
|
||||||
this._characteristicDidChangeCallback = null;
|
this._characteristicDidChangeCallback = null;
|
||||||
this._deviceOptions = deviceOptions;
|
this._deviceOptions = deviceOptions;
|
||||||
this._runtime = runtime;
|
this._runtime = runtime;
|
||||||
|
|
||||||
|
this._connected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,6 +52,7 @@ class BLESession extends JSONRPCWebSocket {
|
||||||
.then(() => {
|
.then(() => {
|
||||||
log.info('should have connected');
|
log.info('should have connected');
|
||||||
this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTED);
|
this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTED);
|
||||||
|
this._connected = true;
|
||||||
this._connectCallback();
|
this._connectCallback();
|
||||||
})
|
})
|
||||||
.catch(e => {
|
.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.
|
* Handle a received call from the socket.
|
||||||
* @param {string} method - a received method label.
|
* @param {string} method - a received method label.
|
||||||
|
@ -119,6 +137,7 @@ class BLESession extends JSONRPCWebSocket {
|
||||||
}
|
}
|
||||||
|
|
||||||
_sendError (e) {
|
_sendError (e) {
|
||||||
|
this._connected = false;
|
||||||
log.error(`BLESession error:`);
|
log.error(`BLESession error:`);
|
||||||
log.error(e);
|
log.error(e);
|
||||||
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
|
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
|
||||||
|
|
|
@ -26,6 +26,8 @@ class BTSession extends JSONRPCWebSocket {
|
||||||
this._deviceOptions = deviceOptions;
|
this._deviceOptions = deviceOptions;
|
||||||
this._messageCallback = messageCallback;
|
this._messageCallback = messageCallback;
|
||||||
this._runtime = runtime;
|
this._runtime = runtime;
|
||||||
|
|
||||||
|
this._connected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -51,6 +53,7 @@ class BTSession extends JSONRPCWebSocket {
|
||||||
.then(() => {
|
.then(() => {
|
||||||
log.info('should have connected');
|
log.info('should have connected');
|
||||||
this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTED);
|
this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTED);
|
||||||
|
this._connected = true;
|
||||||
this._connectCallback();
|
this._connectCallback();
|
||||||
})
|
})
|
||||||
.catch(e => {
|
.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) {
|
sendMessage (options) {
|
||||||
return this.sendRemoteRequest('send', options);
|
return this.sendRemoteRequest('send', options);
|
||||||
}
|
}
|
||||||
|
|
|
@ -213,6 +213,14 @@ class VirtualMachine extends EventEmitter {
|
||||||
this.runtime.connectToPeripheral(extensionId, peripheralId);
|
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.
|
* 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.
|
* @param {string | object} input A json string, object, or ArrayBuffer representing the project to load.
|
||||||
|
|
Loading…
Reference in a new issue