nativescript-connectivity-manager-plugin
Version:
This plugin provides a connectivity manager of Android and iOS.
102 lines • 3.33 kB
JavaScript
import { Common } from "./connectivity-manager-impl.common";
export class ConnectivityManagerImpl extends Common {
constructor() {
super(...arguments);
this.previousSsid = undefined;
}
getNetworkInfo() {
let interfaceNames = CNCopySupportedInterfaces();
for (let i = 0; i < interfaceNames.count; i++) {
let info = (CNCopyCurrentNetworkInfo(interfaceNames[i]));
if (!info) {
continue;
}
let ssid = info.valueForKey(kCNNetworkInfoKeySSID);
if (!ssid) {
continue;
}
return info;
}
return null;
}
getSSID() {
const info = this.getNetworkInfo();
return info ? info.valueForKey(kCNNetworkInfoKeySSID) : null;
}
getWifiNetworkId() {
const info = this.getNetworkInfo();
return info ? info.valueForKey(kCNNetworkInfoKeyBSSID) : null;
}
isWifiEnabled() {
return undefined;
}
isWifiConnected() {
return undefined;
}
isCellularEnabled() {
return undefined;
}
isCellularConnected() {
return undefined;
}
isGpsEnabled() {
return undefined;
}
isGpsConnected() {
return undefined;
}
hasInternet() {
return undefined;
}
scanWifiNetworks() {
return undefined;
}
connectToWifiNetwork(ssid, password, milliseconds) {
const that = this;
return new Promise((resolve, reject) => {
const hotspot = NEHotspotConfiguration.new();
let configuration = password ? hotspot.initWithSSIDPassphraseIsWEP(ssid, password, false) : hotspot.initWithSSID(ssid);
configuration.joinOnce = true;
let currentTime = 0;
let interval = setInterval(() => {
if (that.getSSID() == ssid) {
that.previousSsid = ssid;
resolve(true);
clearInterval(interval);
return;
}
currentTime += 200;
if (currentTime >= milliseconds) {
resolve(false);
clearInterval(interval);
}
}, 200);
NEHotspotConfigurationManager.sharedManager.applyConfigurationCompletionHandler(configuration, (err) => {
if (err && err instanceof NSError) {
resolve(false);
clearInterval(interval);
}
});
});
}
disconnectWifiNetwork(timeoutMs) {
const that = this;
return new Promise((resolve) => {
let currentTime = 0;
NEHotspotConfigurationManager.sharedManager.removeConfigurationForSSID(this.previousSsid);
let interval = setInterval(() => {
if (that.getSSID() != that.previousSsid) {
resolve(true);
clearInterval(interval);
return;
}
currentTime += 200;
if (currentTime >= timeoutMs) {
resolve(false);
clearInterval(interval);
}
}, 200);
});
}
}
//# sourceMappingURL=connectivity-manager-impl.ios.js.map