UNPKG

@iotize/device-com-wifi.cordova

Version:
94 lines 3.67 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; // // Copyright 2019 IoTize SAS Inc. Licensed under the MIT license. // import { promiseTimeout } from "@iotize/common/promise"; import { BehaviorSubject, Subject } from "rxjs"; import { debug } from "./logger"; const TAG = "WifiScannerCordova"; const DEFAULT_STOP_TIMEOUT = 10 * 1000; /** * */ export class WifiScanner { constructor() { this._isScanning$ = new BehaviorSubject(false); this._devices$ = new Subject(); } get scanning() { return this._isScanning$.asObservable(); } get isScanning() { return this._isScanning$.value; } get results() { return this._devices$.asObservable(); } isAvailable() { return WifiWizard2.isWifiEnabled(); } /** * Launches the scan for BLE devices */ start(options = {}) { return __awaiter(this, void 0, void 0, function* () { // if (!await this.isAvailable()){ // throw new Error(`WiFi is not available. Please enabled Wi-Fi to start scan`); // } if (this.isScanning) { debug(TAG, "Already scanning"); return; } debug(TAG, "Start Scanning ..."); this._isScanning$.next(true); this._scanPromise = WifiWizard2.scan() // .then(() => { // const timeout = options.timeout || 5000; // debug(TAG, `Waiting for ${timeout}ms...`); // return WifiWizard2.timeout(timeout); // }) // .then(() => { // debug(TAG, 'Getting scan results...'); // return WifiWizard2.getScanResults(); // }) .then((scanResults) => { this._devices$.next(scanResults); debug(TAG, "Scanner stopped", scanResults); this._isScanning$.next(false); }) .catch((err) => { this._isScanning$.next(false); debug(TAG, "Scan result error", err); }); }); } stop(options) { return __awaiter(this, void 0, void 0, function* () { debug(TAG, "Stop Scanning ..."); return promiseTimeout((options === null || options === void 0 ? void 0 : options.timeout) || DEFAULT_STOP_TIMEOUT, this._cancelScan()); }); } _cancelScan() { return __awaiter(this, void 0, void 0, function* () { this._isScanning$.next(false); try { const scanResults = yield WifiWizard2.getScanResults(); if (this._devices$) { this._devices$.next(scanResults); } } catch (err) { // we can ignore if getScanResults has errors } }); } } //# sourceMappingURL=wifi-scanner.js.map