连接模块在使用Nativescript时不起作用

时间:2019-02-28 15:32:30

标签: android typescript nativescript multipeer-connectivity

我遵循this教程,并在app.component.ts中编写此代码。

    export class AppComponent implements OnInit {
    isConnection: boolean;
    constructor() {}
    ngOnInit() {
        const myConnectionType = getConnectionType();
        switch (myConnectionType) {
            case connectionType.none:
                this.isConnection= false;
                dialogs.confirm({
                    message: "Please, check Wifi",
                    okButtonText: "OK",
                }).then(result => {
                    console.log("Dialog result: " + result);
                });
                break;
            case connectionType.wifi:
                this.isConnection= true
                break;
            case connectionType.mobile:
                this.isConnection= false;
                dialogs.confirm({
                    message: "Please, check Wifi",
                    okButtonText: "OK",
                }).then(result => {
                    console.log("Dialog result: " + result);
                });
                break;
            case connectionType.ethernet:
                this.isConnection= false;
                dialogs.confirm({
                    message: "Please, check Wifi",
                    okButtonText: "OK",
                }).then(result => {
                    console.log("Dialog result: " + result);
                });
                break;
            default:
                break;
        }
    }
}

我不明白为什么断开WiFi后无法正常工作?

在app.component.html

<page-router-outlet></page-router-outlet>

在AndroidManifest / xml中,我放了<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

请问您有什么主意吗?

1 个答案:

答案 0 :(得分:1)

回答您的问题“当我断开WiFi连接时为什么不起作用?”我们“ You have to monitor the connection”。在您的代码中,您只是在检查应用程序负载时检查连接类型,但是还必须监控,以防您需要检查何时断开Wi-Fi。

ngOnInit() {
connectivity.startMonitoring((newConnectionType: number) => {
      switch (newConnectionType) {
        case connectivity.connectionType.none:
          this._userService.connectionType = AppConstants.INT_CONN_NONE;
          console.log('Connection type changed to none.');
          break;
        case connectivity.connectionType.wifi:
          this._userService.connectionType = AppConstants.INT_CONN_WIFI;
          console.log('Connection type changed to WiFi.');
          break;
        case connectivity.connectionType.mobile:
          this._userService.connectionType = AppConstants.INT_CONN_MOBILE;
          console.log('Connection type changed to mobile.');
          break;
        default:
          break;
      }
    });
}