2019年12月23日 星期一

[morse]Ionic 檢查設備硬件功能是否已啟用或對應用程序可用


Install
    ionic cordova plugin add cordova.plugins.diagnostic
    npm install @ionic-native/diagnostic



Example 1:

import { Diagnostic } from '@ionic-native/diagnostic/ngx';
constructor(private diagnostic: Diagnostic) { }
...
let successCallback = (isAvailable) => { console.log('Is available? ' + isAvailable); }
let errorCallback = (e) => console.error(e);
this.diagnostic.isCameraAvailable().then(successCallback).catch(errorCallback);
this.diagnostic.isBluetoothAvailable().then(successCallback, errorCallback);

this.diagnostic.getBluetoothState()
.then((state) => {
    if (state == this.diagnostic.bluetoothState.POWERED_ON){
    // do something
    } else {
    // do something else
    }
}).catch(e => console.error(e));


Example 2:

app.module.ts

import { Diagnostic } from '@ionic-native/diagnostic/ngx';

@NgModule({
  providers: [
    Diagnostic
  ]
})


xxx.page.ts

  import { Diagnostic } from '@ionic-native/diagnostic/ngx';

  constructor(private _diagnostic: Diagnostic) { }
  
  Gpsstart() {
    // 檢查GPS
    this._diagnostic.isGpsLocationEnabled().then(
      (OnSuccess) => {
        if (OnSuccess){
          alert('GPS 開啟中');          
          this.showWatch();  
        }
        else{
          alert('GPS 沒開啟!');
          // 進入 GPS 啟用設定畫面
          this._diagnostic.switchToLocationSettings();  
        }
      },
      (OnFail) => {
        alert('GPS 檢查時出現錯誤!');
        this._totast.presentToastWithOptions('GPS 檢查時出現錯誤! '+ OnFail.message);
        //cordova.plugins.diagnostic.switchToLocationSettings();
      }
    ).catch(
      (OnError) => {
        this._totast.presentToastWithOptions('Catch error: ' + OnError.message);
      }
    );    
  }
  
  gpsWatch() {
    this.watchPosition = undefined;
    const options = {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0
    };
    this.gpsWatchId = this._geolocation.watchPosition(options).subscribe(
      (p) => {
        if (p) {
          this.watchPosition = p;
        }
        //alert(p.coords.latitude);
      },
      error => {
        // console.log('GPS Watch fail!');
        // console.error(error);
        // this._totast.presentToastWithOptions(error.message);
        alert(error.message);
      }
    );
  }



referance :
如何檢查是否啟用了gps
https://stackoverflow.com/questions/35304103/phonegap-how-to-check-if-gps-is-enabled

診斷-檢查設備硬件功能是否已啟用或對應用程序可用,例如相機,GPS,WiFi
https://ionicframework.com/docs/native/diagnostic

2019年12月5日 星期四

[morse]Using WifiWizard2 with Ionic

安裝WifiWizrd2插件
ionic cordova plugin add wifiwizard2



 home.page.html
< ion-header>
  < ion-toolbar>
    < ion-title>
     List Wifi AP
    < /ion-title>
  < /ion-toolbar>
< /ion-header>

< ion-content padding>

    < ion-button color="primary" (click)="getNetworks()">Get Networks< /ion-button>
    {{info_txt}}
    < ion-list>
        < ion-item *ngFor="let data of results">
            {{data.SSID}} -
            {{data.BSSID}}
        < /ion-item>
      < /ion-list>
< /ion-content>


 home.page.ts
import { Component } from '@angular/core';

declare var WifiWizard2: any;

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  results = [];
  info_txt = "";
  async getNetworks() {
    this.info_txt = "loading...";
    try {
      let results = await WifiWizard2.scan();
      this.results = results;
      this.info_txt = "";
    } catch (error) {
      this.info_txt = error;
    }
  }
}


注意: 需在模擬器或手機上才能試出效果。