nativeScript-connectivity-manager-plugin
此插件提供了 Android 和 iOS 的连接管理器。
npm i --save nativescript-connectivity-manager-plugin

NativeScript ConnectivityManager 插件

用于管理 Android 和 iOS 设备连接的插件。

  • [x] Android
    • [x] WiFi
    • [x] 移动网络
    • [x] GPS
    • [ ] 蓝牙
    • [ ] 授予权限
  • [x] iOS
    • [x] WiFi
      • [x] 连接到 WiFi
      • [x] 获取 SSID
      • [] 其他尚未实现
    • [ ] 移动网络
    • [ ] GPS
    • [ ] 蓝牙

安装

tns plugin add nativescript-connectivity-manager-plugin

演示

查看 Angular 演示应用程序 并在本地运行它

git clone https://github.com/1IoT/nativescript-connectivity-manager-plugin
cd nativescript-connectivity-manager-plugin/src
npm run demo:android

用法

import {ConnectivityManagerImpl} from 'nativescript-connectivity-manager-plugin';

@Component({
selector: "Home",
templateUrl: "./home.component.html"
})
export class HomeComponent implements OnInit {

private static NETWORK_SSID: string = "MY_SSID";
private static NETWORK_PASSPHARSE: string = "MY_KEY";
private static CONNECTION_TIMEOUT_MS: number = 30000;
private static DISCONNECT_TIMEOUT_MS: number = 15000;

constructor(private connectivityManager: ConnectivityManagerImpl, private httpClient: HttpClient) {
}

ngOnInit(): void {
}

public getInfos() {
console.log("Wifi SSID: " + this.connectivityManager.getSSID());
console.log("NetworkId: " + this.connectivityManager.getWifiNetworkId());
console.log("Wifi enabled: " + this.connectivityManager.isWifiEnabled());
console.log("Wifi connected: " + this.connectivityManager.isWifiConnected());
console.log("Cellular enabled: " + this.connectivityManager.isCellularEnabled());
console.log("Cellular connected: " + this.connectivityManager.isCellularConnected());
console.log("GPS enabled: " + this.connectivityManager.isGpsEnabled());
console.log("GPS connected: " + this.connectivityManager.isGpsConnected());
}

public scan(): void {
console.log("Start scan...");
this.connectivityManager.scanWifiNetworks().then((wifiSSIDs: string[]) => {
console.log(wifiSSIDs);
});
}

public async connect(): Promise<boolean> {
console.log("Start connection...");
console.log("Disconnect with the source network...");
return this.connectivityManager.connectToWifiNetwork(HomeComponent.NETWORK_SSID, HomeComponent.NETWORK_PASSPHARSE, HomeComponent.CONNECTION_TIMEOUT_MS);
}

public async disconnect(): Promise<boolean> {
return this.connectivityManager.disconnectWifiNetwork(HomeComponent.DISCONNECT_TIMEOUT_MS);
}
}

API

需要 Android SDK:29

警告:请注意,即使是为了扫描 WiFi 和检索 SSID,也必须授予位置权限并启用 GPS!

方法 返回值 描述
getSSID() string 需要已授予的位置权限和已启用的 GPS
getWifiNetworkId() number
isWifiEnabled() boolean
isWifiConnected() boolean
isCellularEnabled() boolean
isCellularConnected() boolean
isGpsEnabled() boolean
isGpsConnected() boolean
hasInternet() boolean
async scanWifiNetworks() Promise 需要已授予的位置权限和已启用的 GPS
async connectToWifiNetwork(ssid: string, password: string, milliseconds: number) Promise
async disconnectWifiNetwork(timeoutMs: number) Promise

提示

  • 有关 tns-platform-declarations 的文档
  • 如果项目无法构建,可能需要运行 npm run demo:resetnpm run build 来修复它

许可证

Apache 许可证第 2 版,2004 年 1 月