mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-08-28 22:30:40 -04:00
Close Scratch Link web socket on every new peripheral scan attempt (#1696)
* Progress towards: #1671: Close web socket before making a new one for hardware extensions. * Always disconnect before scanning for new peripheral. Removing console logs. * Removing unused test code.
This commit is contained in:
parent
fe186913ac
commit
53c165c2f6
5 changed files with 33 additions and 9 deletions
|
@ -555,6 +555,9 @@ class EV3 {
|
|||
* Called by the runtime when user wants to scan for an EV3 peripheral.
|
||||
*/
|
||||
scan () {
|
||||
if (this._bt) {
|
||||
this._bt.disconnect();
|
||||
}
|
||||
this._bt = new BT(this._runtime, this._extensionId, {
|
||||
majorDeviceClass: 8,
|
||||
minorDeviceClass: 1
|
||||
|
@ -566,17 +569,22 @@ class EV3 {
|
|||
* @param {number} id - the id of the peripheral to connect to.
|
||||
*/
|
||||
connect (id) {
|
||||
this._bt.connectPeripheral(id);
|
||||
if (this._bt) {
|
||||
this._bt.connectPeripheral(id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the runtime when user wants to disconnect from the EV3 peripheral.
|
||||
*/
|
||||
disconnect () {
|
||||
this._bt.disconnect();
|
||||
this._clearSensorsAndMotors();
|
||||
window.clearInterval(this._pollingIntervalID);
|
||||
this._pollingIntervalID = null;
|
||||
|
||||
if (this._bt) {
|
||||
this._bt.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -205,6 +205,9 @@ class MicroBit {
|
|||
* Called by the runtime when user wants to scan for a peripheral.
|
||||
*/
|
||||
scan () {
|
||||
if (this._ble) {
|
||||
this._ble.disconnect();
|
||||
}
|
||||
this._ble = new BLE(this._runtime, this._extensionId, {
|
||||
filters: [
|
||||
{services: [BLEUUID.service]}
|
||||
|
@ -217,7 +220,9 @@ class MicroBit {
|
|||
* @param {number} id - the id of the peripheral to connect to.
|
||||
*/
|
||||
connect (id) {
|
||||
this._ble.connectPeripheral(id);
|
||||
if (this._ble) {
|
||||
this._ble.connectPeripheral(id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -225,7 +230,9 @@ class MicroBit {
|
|||
*/
|
||||
disconnect () {
|
||||
window.clearInterval(this._timeoutID);
|
||||
this._ble.disconnect();
|
||||
if (this._ble) {
|
||||
this._ble.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -570,6 +570,9 @@ class WeDo2 {
|
|||
* Called by the runtime when user wants to scan for a WeDo 2.0 peripheral.
|
||||
*/
|
||||
scan () {
|
||||
if (this._ble) {
|
||||
this._ble.disconnect();
|
||||
}
|
||||
this._ble = new BLE(this._runtime, this._extensionId, {
|
||||
filters: [{
|
||||
services: [BLEService.DEVICE_SERVICE]
|
||||
|
@ -583,7 +586,9 @@ class WeDo2 {
|
|||
* @param {number} id - the id of the peripheral to connect to.
|
||||
*/
|
||||
connect (id) {
|
||||
this._ble.connectPeripheral(id);
|
||||
if (this._ble) {
|
||||
this._ble.connectPeripheral(id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -598,7 +603,9 @@ class WeDo2 {
|
|||
distance: 0
|
||||
};
|
||||
|
||||
this._ble.disconnect();
|
||||
if (this._ble) {
|
||||
this._ble.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -42,7 +42,7 @@ class BLE extends JSONRPCWebSocket {
|
|||
this.sendRemoteRequest('discover', this._peripheralOptions)
|
||||
.catch(e => {
|
||||
this._sendRequestError(e);
|
||||
}); // never reached?
|
||||
});
|
||||
}
|
||||
// TODO: else?
|
||||
}
|
||||
|
|
|
@ -42,7 +42,9 @@ class BT extends JSONRPCWebSocket {
|
|||
this._availablePeripherals = {};
|
||||
this._discoverTimeoutID = window.setTimeout(this._sendDiscoverTimeout.bind(this), 15000);
|
||||
this.sendRemoteRequest('discover', this._peripheralOptions)
|
||||
.catch(e => this._sendRequestError(e)); // never reached?
|
||||
.catch(
|
||||
e => this._sendRequestError(e)
|
||||
);
|
||||
}
|
||||
// TODO: else?
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue