Search code examples
angularcordovaionic-frameworkionic3nodes

How to obtain IMEI Number of a device Using Ionic 3?


I need to identify every device uniquely. I think best way to obtain the IMEi num. But How can I do that.

I have read the documentation https://ionicframework.com/docs/native/uid/ but not clear.

If there have any other process to do that suggest me.


Solution

  • I had the same need to identify each device in a unique way and the solution I found was as follows.

    GET IMEI

    it must be installed, a plugin and two dependencies, in the version that is specified and that the latest version is not compatible with the project I am working on (ionic 3)

    ionic cordova plugin add cordova-plugin-uid
    npm install --save @ionic-native/uid@^4.20.0
    npm install --save @ionic-native/android-permissions@^4.20.0
    cordova plugin add cordova-plugin-android-permissions         
    cordova plugin add cordova-plugin-permission
    

    Dependent applications must be imported into the app.module.ts file and add providers of the same file to the object.

    app.module.ts file

    import {Uid} from '@ionic-native/uid';
    import {AndroidPermissions} from '@ionic-native/android-permissions';
    
    
    providers: [
        StatusBar,
        Uid,
        AndroidPermissions,
        SplashScreen,
        {provide: ErrorHandler, useClass: IonicErrorHandler},
      ]
    

    in the file where the image is captured, the previously installed dependencies must be imported, in this case home.ts.

    we build the function getIMEI () and declare it in the constructor.

    home.ts file

    import {Uid} from '@ionic-native/uid';
    import {AndroidPermissions} from '@ionic-native/android-permissions';
    
    device_id: any;
    builder(
        public uid: Uid,
        public androidPermissions: AndroidPermissions
        ) {
        this.getIMEI();
    }
    
    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 identifiers after
    restart the application
            return 0;
        }
        this.device_id = this.uid.IMEI
        return this.uid.IMEI;
    }
    

    to show the image on the screen it would only be necessary to edit the html in this case home.html

    home.html file

    <ion-content padding>
     <p> {{device_id}} </p>
    </ion-content>
    

    Remember that when using these native dependencies, we could only see the results if we run the application from an emulator or from a physical device, in the browser it will not work.

    in my case it worked, I hope it serves someone