简体   繁体   English

如何获得IMEI号码| 离子 | 安卓

[英]How to get IMEI Number | IONIC | Android

I need to fetch the IMEI from Android device, i am working in an IONIC App.我需要从 Android 设备获取 IMEI,我正在使用 IONIC 应用程序。

I am using this plugin - ionic cordova plugin add cordova-plugin-uid我正在使用这个插件 - ionic cordova plugin add cordova-plugin-uid

Reference link - https://ionicframework.com/docs/native/uid/参考链接 - https://ionicframework.com/docs/native/uid/

Here is my code snippet -这是我的代码片段 -

async getIMEI() {

    const {hasPermission} = await this.androidPermissions.checkPermission(
        this.androidPermissions.PERMISSION.READ_PHONE_STATE
    );

    console.log("hasPermission : " + hasPermission);
    if (!hasPermission) {
        const result = await this.androidPermissions.requestPermission(
            this.androidPermissions.PERMISSION.READ_PHONE_STATE
        );

        if (!result.hasPermission) {
            throw new Error('Permissions required');
        }

        // ok, a user gave us permission, we can get him identifiers after restart app
        return 0 ;
    }

    return this.uid.IMEI;
}

I am calling getIMEI() in constructor -我在构造函数中调用 getIMEI() -

 constructor(public navCtrl: NavController, public navParams: NavParams, public alertCtrl: AlertController,
                public apiProvider: ApiProvider, public storage: Storage, public platform: Platform, public fcm: FCM,
                public uid: Uid, public androidPermissions: AndroidPermissions) {


        platform.ready().then(() => {

            if (this.platform.is('android')) {
                console.log("running on Android device!");
                this.deviceType = 'ANDROID';
                this.getIMEI();
            }
            if (this.platform.is('ios')) {
                console.log("running on iOS device!");
                this.deviceType = 'IPHONE';
            }



        });
    }

I am always getting IMEI number as null value.我总是将 IMEI 号码作为值。

You can follow the docs: https://ionicframework.com/docs/native/uid您可以按照文档: https : //ionicframework.com/docs/native/uid

First add the plugin to you app:首先将插件添加到您的应用程序:

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

In the ts file you want to get IMEI:在你想要获取 IMEI 的 ts 文件中:

import { Uid } from '@ionic-native/uid/ngx';
import { AndroidPermissions } from '@ionic-native/android-permissions/ngx';

constructor(private uid: Uid, private androidPermissions: AndroidPermissions) { }


async getImei() {
 const { hasPermission } = await this.androidPermissions.checkPermission(
   this.androidPermissions.PERMISSION.READ_PHONE_STATE
 );

 if (!hasPermission) {
   const result = await this.androidPermissions.requestPermission(
     this.androidPermissions.PERMISSION.READ_PHONE_STATE
   );

   if (!result.hasPermission) {
     throw new Error('Permissions required');
   }

   // ok, a user gave us permission, we can get him identifiers after restart app
   return;
 }

  return this.uid.IMEI
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM