Ionic 5,如何通过电容获取设备序列号?

Ionic 5, how to get the serial number of device via capacitor?

我正在使用 Ionic 5 和 CapacitorJs 构建应用程序,我想知道如何通过电容器插件获取序列号?

如果可能我不想使用 Cordova 插件。

您可以使用 Ionic Device 读取 IMEI、ICCID、IMSI 和 MAC

首先你需要安装插件

$ ionic cordova plugin add cordova-plugin-uid
$ npm install @ionic-native/uid

$ ionic cordova plugin add cordova-plugin-android-permissions
$ npm install @ionic-native/android-permissions 

或带电容:

npm install @ionic-native/android-permissions
npm install cordova-plugin-android-permissions
npm install cordova-plugin-uid
npm install @ionic-native/uid

ionic cap sync

所以你需要实现这样的东西:

import { Component } from '@angular/core';
    
    import { UniqueDeviceID } from '@ionic-native/unique-device-id/ngx';
    import { Uid } from '@ionic-native/uid/ngx';
    import { AndroidPermissions } from '@ionic-native/android-permissions/ngx';
    
    @Component({
      selector: 'app-home',
      templateUrl: 'home.page.html',
      styleUrls: ['home.page.scss'],
    })
    export class HomePage {
    
      constructor(
        private uniqueDeviceID: UniqueDeviceID,
        private uid: Uid,
        private androidPermissions: AndroidPermissions
      ) {
      }
    
        ...
        ...
    
    }

  getPermission(){
    this.androidPermissions.checkPermission(
      this.androidPermissions.PERMISSION.READ_PHONE_STATE
    ).then(res => {
      if(res.hasPermission){
        
      }else{
        this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_PHONE_STATE).then(res => {
          alert("Persmission Granted Please Restart App!");
        }).catch(error => {
          alert("Error! "+error);
        });
      }
    }).catch(error => {
      alert("Error! "+error);
    });
  }

  getID_UID(type){
    if(type == "IMEI"){
      return this.uid.IMEI;
    }else if(type == "ICCID"){
      return this.uid.ICCID;
    }else if(type == "IMSI"){
      return this.uid.IMSI;
    }else if(type == "MAC"){
      return this.uid.MAC;
    }else if(type == "UUID"){
      return this.uid.UUID;
    }
  }

或者可能更好

getID_UID = (type) => (this.uid[type])