简体   繁体   中英

How to get IMEI Number | IONIC | Android

I need to fetch the IMEI from Android device, i am working in an IONIC App.

I am using this plugin - ionic cordova plugin add cordova-plugin-uid

Reference link - 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 -

 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.

You can follow the docs: 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:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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