@awarns/wifi
Version:
AwarNS Framework package that allows to scan for nearby Wi-Fi access points
48 lines • 2.01 kB
JavaScript
import { ProviderInterrupter } from '@awarns/core/providers';
import { WifiScan, WifiScanType } from './scan';
import { FingerprintGrouping, getWifiScanProvider as getNativeProvider, } from 'nativescript-context-apis/wifi';
import { firstValueFrom, map, of, Subject, takeUntil, timeout } from 'rxjs';
export class WifiScanProvider {
constructor(ensureIsNew, timeout, nativeProvider = getNativeProvider) {
this.ensureIsNew = ensureIsNew;
this.timeout = timeout;
this.nativeProvider = nativeProvider;
}
get provides() {
return WifiScanType;
}
async checkIfIsReady() {
const isReady = await this.nativeProvider().isReady();
if (!isReady) {
throw wifiScanProviderNotReadyErr;
}
}
async prepare() {
return this.nativeProvider().prepare();
}
next() {
const interrupter = new ProviderInterrupter();
const scanResult = this.obtainWifiScan(interrupter);
return [scanResult, () => interrupter.interrupt()];
}
obtainWifiScan(interrupter) {
const interrupted$ = new Subject();
interrupter.interruption = () => {
interrupted$.next();
interrupted$.complete();
};
return firstValueFrom(this.nativeProvider()
.wifiFingerprintStream({
ensureAlwaysNew: this.ensureIsNew,
grouping: FingerprintGrouping.NONE,
continueOnFailure: false,
})
.pipe(takeUntil(interrupted$), timeout({ each: this.timeout, with: () => of(null) }), map((fingerprint) => scanFromFingerprint(fingerprint))));
}
}
function scanFromFingerprint(fingerprint) {
const { seen, isNew, timestamp } = fingerprint;
return new WifiScan(seen, isNew, timestamp);
}
export const wifiScanProviderNotReadyErr = new Error("Wifi scan provider is not ready. Perhaps permissions haven't been granted, location services have been disabled or wifi is turn off");
//# sourceMappingURL=provider.js.map