mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-29 07:29:12 -04:00
EV3/Microbit critical fixes for code freeze (#1354)
* Resolves - BLESession and BTSession should emit PERIPHERAL_SCAN_TIMEOUT #1348. * Resolves - BLESession should handle 'could not find service' error #1350. * Resolves - BTSession should handle 'no peripheral connected' error #1351. * Fixing a typo that caused device scan timeout bugs. * Resolves - Add casting and clamping throughout the EV3 extension #1352. * Fixing a linting error. * Further fixes for issue #1351.
This commit is contained in:
parent
1dcdfc9548
commit
c4ee7065a2
6 changed files with 117 additions and 46 deletions
src/io
|
@ -22,11 +22,11 @@ class BLESession extends JSONRPCWebSocket {
|
|||
|
||||
this._availablePeripherals = {};
|
||||
this._connectCallback = connectCallback;
|
||||
this._connected = false;
|
||||
this._characteristicDidChangeCallback = null;
|
||||
this._deviceOptions = deviceOptions;
|
||||
this._discoverTimeoutID = null;
|
||||
this._runtime = runtime;
|
||||
|
||||
this._connected = false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -35,7 +35,8 @@ class BLESession extends JSONRPCWebSocket {
|
|||
*/
|
||||
requestDevice () {
|
||||
if (this._ws.readyState === 1) { // is this needed since it's only called on ws.onopen?
|
||||
// TODO: start a 'discover' timeout
|
||||
this._availablePeripherals = {};
|
||||
this._discoverTimeoutID = window.setTimeout(this._sendDiscoverTimeout.bind(this), 15000);
|
||||
this.sendRemoteRequest('discover', this._deviceOptions)
|
||||
.catch(e => this._sendError(e)); // never reached?
|
||||
}
|
||||
|
@ -88,7 +89,10 @@ class BLESession extends JSONRPCWebSocket {
|
|||
this._runtime.constructor.PERIPHERAL_LIST_UPDATE,
|
||||
this._availablePeripherals
|
||||
);
|
||||
// TODO: cancel a discover timeout if one is active
|
||||
if (this._discoverTimeoutID) {
|
||||
// TODO: window?
|
||||
window.clearTimeout(this._discoverTimeoutID);
|
||||
}
|
||||
break;
|
||||
case 'characteristicDidChange':
|
||||
this._characteristicDidChangeCallback(params.message);
|
||||
|
@ -115,8 +119,10 @@ class BLESession extends JSONRPCWebSocket {
|
|||
params.startNotifications = true;
|
||||
}
|
||||
this._characteristicDidChangeCallback = onCharacteristicChanged;
|
||||
return this.sendRemoteRequest('read', params);
|
||||
// TODO: handle error here
|
||||
return this.sendRemoteRequest('read', params)
|
||||
.catch(e => {
|
||||
this._sendError(e);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -132,7 +138,10 @@ class BLESession extends JSONRPCWebSocket {
|
|||
if (encoding) {
|
||||
params.encoding = encoding;
|
||||
}
|
||||
return this.sendRemoteRequest('write', params);
|
||||
return this.sendRemoteRequest('write', params)
|
||||
.catch(e => {
|
||||
this._sendError(e);
|
||||
});
|
||||
}
|
||||
|
||||
_sendError (e) {
|
||||
|
@ -140,6 +149,10 @@ class BLESession extends JSONRPCWebSocket {
|
|||
log.error(`BLESession error: ${JSON.stringify(e)}`);
|
||||
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
|
||||
}
|
||||
|
||||
_sendDiscoverTimeout () {
|
||||
this._runtime.emit(this._runtime.constructor.PERIPHERAL_SCAN_TIMEOUT);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BLESession;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue