UNPKG

@iotize/device-com-ble.cordova

Version:

Bluetooth Low Energy (BLE) for IoTize modules Plugin

151 lines 5.75 kB
// // Copyright 2018 IoTize SAS Inc. Licensed under the MIT license. // // scanner.ts // device-com-ble.cordova BLE Cordova Plugin // 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()); }); }; import { BleComError } from "@iotize/tap/protocol/ble/common"; import { BehaviorSubject } from "rxjs"; import { debug } from "./logger"; import { getIoTizeBleCordovaPlugin } from "./utility"; /** * BLE Tap scanner for cordova apps */ export class BLEScanner { /** * * @param cordovaInterfaceOverwrite overwrite cordova interface. Used for testing */ constructor(cordovaInterfaceOverwrite, /** * Request device options used to filter scan results */ requestDeviceOptions) { this.cordovaInterfaceOverwrite = cordovaInterfaceOverwrite; this.requestDeviceOptions = requestDeviceOptions; this._results = new BehaviorSubject([]); this._scanning$ = new BehaviorSubject(false); } /** * Lazy reference to iotizeBLE. * We don't want to reference iotizeBLE in constructor as it may be referenced * before cordova plugin is loaded */ get cordovaInterface() { return this.cordovaInterfaceOverwrite || getIoTizeBleCordovaPlugin(); } get scanning() { return this._scanning$.asObservable(); } get isScanning() { return this._scanning$.value; } /** * Gets the observable on the devices$ Subject * @return */ get results() { return this._results.asObservable(); } /** * Launches the scan for BLE devices * Throws if BLE is not available */ start(options) { var _a; return __awaiter(this, void 0, void 0, function* () { (_a = this.scanSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe(); this.scanSubscription = undefined; this.clearResults(); const isAvailable = yield this.checkAvailable(); if (!isAvailable) { throw BleComError.bleNotAvailable(`BLE is not available. Make sure that BLE is enabled on your device`); } debug("Start Scanning ..."); this._scanning$.next(true); return new Promise((resolve, reject) => { this.scanSubscription = this.cordovaInterface .startScan(this.requestDeviceOptions) .subscribe((result) => { if (result == "Ok") { resolve(); return; } this.addOrRefreshDevice(result); }, (error) => { debug("Start scan failed with error: ", error); this.cordovaInterface .getLastError() .then((lasterror) => { debug("Last BLE error " + lasterror); }) .catch((err) => { debug("Cannot get last BLE error: ", err); }); reject(error); this._scanning$.next(false); }); }); }); } /** * */ stop() { var _a; return __awaiter(this, void 0, void 0, function* () { debug("Stop Scanning ..."); try { (_a = this.scanSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe(); this.scanSubscription = undefined; yield this.cordovaInterface.stopScan(); this._scanning$.next(false); return; } catch (err) { this._scanning$.next(false); throw err; } }); } /** * Returns true if this scanner is available */ checkAvailable() { return this.cordovaInterface.checkAvailable(); } get devices() { return this._results.value; } clearResults() { this._results.next([]); } addOrRefreshDevice(newDevice) { let storedDeviceIndex = this.devices.findIndex((entry) => entry.address == newDevice.address); if (storedDeviceIndex >= 0) { let storedDevice = this.devices[storedDeviceIndex]; if (storedDevice.name != newDevice.name || storedDevice.rssi != newDevice.rssi) { debug(`Updating device at index ${storedDeviceIndex}, name=${storedDevice.name} with rssi=${storedDevice.rssi}`); this.devices[storedDeviceIndex] = newDevice; // this.devices = [...this.devices]; this._results.next(this.devices); } } else { debug(`Adding new device name=${newDevice.name} with rssi=${newDevice.rssi}`); this.devices.push(newDevice); this._results.next(this.devices); } } } //# sourceMappingURL=scanner.js.map