| | const JSONRPC = require('../util/jsonrpc'); |
| |
|
| | class BLE extends JSONRPC { |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | constructor (runtime, extensionId, peripheralOptions, connectCallback, resetCallback = null) { |
| | super(); |
| |
|
| | this._socket = runtime.getScratchLinkSocket('BLE'); |
| | this._socket.setOnOpen(this.requestPeripheral.bind(this)); |
| | this._socket.setOnClose(this.handleDisconnectError.bind(this)); |
| | this._socket.setOnError(this._handleRequestError.bind(this)); |
| | this._socket.setHandleMessage(this._handleMessage.bind(this)); |
| |
|
| | this._sendMessage = this._socket.sendMessage.bind(this._socket); |
| |
|
| | this._availablePeripherals = {}; |
| | this._connectCallback = connectCallback; |
| | this._connected = false; |
| | this._characteristicDidChangeCallback = null; |
| | this._resetCallback = resetCallback; |
| | this._discoverTimeoutID = null; |
| | this._extensionId = extensionId; |
| | this._peripheralOptions = peripheralOptions; |
| | this._runtime = runtime; |
| |
|
| | this._socket.open(); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | requestPeripheral () { |
| | this._availablePeripherals = {}; |
| | if (this._discoverTimeoutID) { |
| | window.clearTimeout(this._discoverTimeoutID); |
| | } |
| | this._discoverTimeoutID = window.setTimeout(this._handleDiscoverTimeout.bind(this), 15000); |
| | this.sendRemoteRequest('discover', this._peripheralOptions) |
| | .catch(e => { |
| | this._handleRequestError(e); |
| | }); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | connectPeripheral (id) { |
| | this.sendRemoteRequest('connect', {peripheralId: id}) |
| | .then(() => { |
| | this._connected = true; |
| | this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTED); |
| | this._connectCallback(); |
| | }) |
| | .catch(e => { |
| | this._handleRequestError(e); |
| | }); |
| | } |
| |
|
| | |
| | |
| | |
| | disconnect () { |
| | if (this._connected) { |
| | this._connected = false; |
| | } |
| |
|
| | if (this._socket.isOpen()) { |
| | this._socket.close(); |
| | } |
| |
|
| | if (this._discoverTimeoutID) { |
| | window.clearTimeout(this._discoverTimeoutID); |
| | } |
| |
|
| | |
| | this._runtime.emit(this._runtime.constructor.PERIPHERAL_DISCONNECTED); |
| | } |
| |
|
| | |
| | |
| | |
| | isConnected () { |
| | return this._connected; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | startNotifications (serviceId, characteristicId, onCharacteristicChanged = null) { |
| | const params = { |
| | serviceId, |
| | characteristicId |
| | }; |
| | this._characteristicDidChangeCallback = onCharacteristicChanged; |
| | return this.sendRemoteRequest('startNotifications', params) |
| | .catch(e => { |
| | this.handleDisconnectError(e); |
| | }); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | read (serviceId, characteristicId, optStartNotifications = false, onCharacteristicChanged = null) { |
| | const params = { |
| | serviceId, |
| | characteristicId |
| | }; |
| | if (optStartNotifications) { |
| | params.startNotifications = true; |
| | } |
| | if (onCharacteristicChanged) { |
| | this._characteristicDidChangeCallback = onCharacteristicChanged; |
| | } |
| | return this.sendRemoteRequest('read', params) |
| | .catch(e => { |
| | this.handleDisconnectError(e); |
| | }); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | write (serviceId, characteristicId, message, encoding = null, withResponse = null) { |
| | const params = {serviceId, characteristicId, message}; |
| | if (encoding) { |
| | params.encoding = encoding; |
| | } |
| | if (withResponse !== null) { |
| | params.withResponse = withResponse; |
| | } |
| | return this.sendRemoteRequest('write', params) |
| | .catch(e => { |
| | this.handleDisconnectError(e); |
| | }); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | didReceiveCall (method, params) { |
| | switch (method) { |
| | case 'didDiscoverPeripheral': |
| | this._availablePeripherals[params.peripheralId] = params; |
| | this._runtime.emit( |
| | this._runtime.constructor.PERIPHERAL_LIST_UPDATE, |
| | this._availablePeripherals |
| | ); |
| | if (this._discoverTimeoutID) { |
| | window.clearTimeout(this._discoverTimeoutID); |
| | } |
| | break; |
| | case 'userDidPickPeripheral': |
| | this._availablePeripherals[params.peripheralId] = params; |
| | this._runtime.emit( |
| | this._runtime.constructor.USER_PICKED_PERIPHERAL, |
| | this._availablePeripherals |
| | ); |
| | if (this._discoverTimeoutID) { |
| | window.clearTimeout(this._discoverTimeoutID); |
| | } |
| | break; |
| | case 'userDidNotPickPeripheral': |
| | this._runtime.emit( |
| | this._runtime.constructor.PERIPHERAL_SCAN_TIMEOUT |
| | ); |
| | if (this._discoverTimeoutID) { |
| | window.clearTimeout(this._discoverTimeoutID); |
| | } |
| | break; |
| | case 'characteristicDidChange': |
| | if (this._characteristicDidChangeCallback) { |
| | this._characteristicDidChangeCallback(params.message); |
| | } |
| | break; |
| | case 'ping': |
| | return 42; |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | handleDisconnectError () { |
| | |
| |
|
| | if (!this._connected) return; |
| |
|
| | this.disconnect(); |
| |
|
| | if (this._resetCallback) { |
| | this._resetCallback(); |
| | } |
| |
|
| | this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTION_LOST_ERROR, { |
| | message: `Scratch lost connection to`, |
| | extensionId: this._extensionId |
| | }); |
| | } |
| |
|
| | _handleRequestError () { |
| | |
| |
|
| | this._runtime.emit(this._runtime.constructor.PERIPHERAL_REQUEST_ERROR, { |
| | message: `Scratch lost connection to`, |
| | extensionId: this._extensionId |
| | }); |
| | } |
| |
|
| | _handleDiscoverTimeout () { |
| | if (this._discoverTimeoutID) { |
| | window.clearTimeout(this._discoverTimeoutID); |
| | } |
| | this._runtime.emit(this._runtime.constructor.PERIPHERAL_SCAN_TIMEOUT); |
| | } |
| | } |
| |
|
| | module.exports = BLE; |
| |
|