如何获取IMEI编号|离子| Android

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何获取IMEI编号|离子| Android相关的知识,希望对你有一定的参考价值。

我需要从android设备获取IMEI,我正在使用IONIC应用程序。

我正在使用此插件-ionic cordova plugin add cordova-plugin-uid

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

这是我的代码段-

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;
}

我正在构造函数中调用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';
            }



        });
    }

我总是将IMEI号码作为null值。

答案

您可以关注文档:https://ionicframework.com/docs/native/uid

首先将插件添加到您的应用中:

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

在ts文件中,您想获取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
}

以上是关于如何获取IMEI编号|离子| Android的主要内容,如果未能解决你的问题,请参考以下文章

如何通过代码编程获取设备的 IMEI/ESN 号码但在 android > 6

如何在 Android 10 中获取有关手机 IMEI 的信息?

如何在 android 10 或更高版本中获取设备 ID 或 imei 号码

android如何通过adb命令获取手机的IMEI

Android 获取手机的IMEI

Android 获取 唯一GUID ,替换 IMEI (兼容 Android 10+获取IMEI问题)