How to get IMEI number in PhoneGap?
Asked Answered
C

8

14

I'm developing a PhoneGap Android mobile application using jQuery, JavaScript and HTML. I want to get the mobile IMEI. I have tried this code from this Tutorial.

I am getting the number like this: 97734a345d234d. I have checked my device to get IMEI number using *#06#. I don't know whether it is correct or not.

Cinque answered 5/1, 2012 at 12:56 Comment(5)
To confirm your IMEI, it's usually printed under the battery or somewhere hidden (and on the box it came in if that's available).Inartificial
@Inartificial As you correctly pointed out, it is usually printed under the battery, so to read it, it is required to turn off the device and remove the battery. Using *#06# quick code, metioned by OP is way, way faster and easier. Doesn't require powering off the device.Haldan
@Inartificial would be super helpful if you could provide some code to programmatically remove the battery and read the printed IMEI.Infliction
lol @ both you wise asses, including @CodeBling. Part of his question was: "I dunno whether it is correct or wrong". I was just saying how he could confirm whether the number he was getting from *#06# was accurate.Inartificial
@Inartificial haha ok, I think he meant that he wasn't sure if the hex number is correct - looks to me like it could be valid if converted to decimal. *#06# is a well-known way to obtain the IMEI on most phonesInfliction
B
5

Fetch the IMEI number in the class which extends DroidGap class and save the value of imei number in static member and then access this static field from where ever you want... example code is here

public class MyApp extends DroidGap
{
    private static String imei; 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        imei=tm.getDeviceId();
    }
    static public String getIMEI(){
        return imei;
    }
}
// where ever u need imei number use this code

String imei=MyApp.getIMEI();
Breathless answered 25/1, 2013 at 10:14 Comment(2)
Brilliant example! Pitty, that I can give you only +1. Thanks!Haldan
@bschandramohan you need to create a plugin that invoke some native code (PhoneGap) via JS, see this link: tricedesigns.com/2012/03/01/pushing-data-to-a-phonegap-web-viewBoyceboycey
E
4

You cannot access the IMEI via html or JavaScript. But you can write an app which reads the IMEI for you.

Just call getDeviceId(). Don't forget that you need the READ_PHONE_STATE permission in your manifest.

Explanatory answered 5/1, 2012 at 12:58 Comment(2)
I have i tried this getDeviceId().But am getting the error as network status 1=Reference error :can't find a variable getDeviceIdCinque
@Priya The linked function getDeviceId() returns a string. Do you wrote a Android App? Also note this: You do not instantiate this class directly; instead, you retrieve a reference to an instance through Context.getSystemService(Context.TELEPHONY_SERVICE).Explanatory
P
4

You could write a phonegap plugin to return you the IMEI number. As the phonegap doesnt return imei for Android devices, it returns an random 64-bit number.

Here is a complete plugin to get the real IMEI number in a phonegap application (please note that this plugin is not "plugmanable" but if follow the instructions it will work. I have tested on phonegap 2.9. I am planning on writing a real "plugmanable" plugin with a few device information for phonegap 3.0).

Here is the java class for that extends from CordovaPlugin (DeviceInfo.java):

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;

public class DeviceInfo extends CordovaPlugin {

    public DeviceInfo(){
    }

    public String DeviceImeiNumber(){
        TelephonyManager tManager = (TelephonyManager)cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);            
        return tManager.getDeviceId();
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
        if (action.equals("imeiNumber")) {
            callbackContext.success(this.DeviceImeiNumber());
            return true;
        } 
        else {
            return false;
        }
    } 
}

And then you need a js object so you can access your plugin on the other side (deviceinfo.js):

(function( cordova ) {

    function DeviceInfo() {}

    DeviceInfo.prototype.imeiNumber = function(win, fail) {
        return cordova.exec(
                function (args) { if(win !== undefined) { win(args); } },
                function (args) { if(fail !== undefined) { fail(args); } },
                "DeviceInfo", "imeiNumber", []);
    };

    if(!window.plugins) {
        window.plugins = {};
    }

    if (!window.plugins.DeviceInfo) {
        window.plugins.DeviceInfo = new DeviceInfo();
    }

})( window.cordova );

Add your plugin on res/xml/config.xml file inside your project like this:

<plugins>
    <plugin name="DeviceInfo" value="com.XXX.XXXXX.DeviceInfo"/>
</plugins>

And add the READ_PHONE_STATE permission inside your AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

After this you need to add you deviceinfo.js file into your page, and after the deviceready you can use your plugin like this:

plugins.DeviceInfo.imeiNumber(function(imei){
   console.log("imei "+ imei);                   
});
Provocation answered 16/8, 2013 at 16:52 Comment(0)
R
2

I believe this would only be possible if you exploit some other apps logging of the IMEI and I'm not sure if any do.

Assuming some do, ideally a system app, you can read the logs and parse them for that information.

Good luck, post back with successes/failures

Renata answered 5/1, 2012 at 13:1 Comment(3)
getDeviceId() is a great option too, if it will work with phonegapRenata
I have i tried this getDeviceId().But am getting the error as network status 1=Reference error :can't find a variable getDeviceIdCinque
Then I think your only bet is to somehow achieve it how I described in my answer above.Renata
H
2

You're pointing to PhoneGap Tutorial version 1.0.0. It is very old and it lacs a lot of information. If you, for example, read the very same page for version 2.1.0 of PhoneGap you'll notice a comment made for device.uuid Quick Example, where it is said, that device.uuid returns IMEI number only in case of Tizen platform. All other platforms, including Android, you mentioned, does return something else.

You can't get device's IMEI in pure HTML / Javascript (application compiled through PhoneGap Build). To read it, you have to build locally, for Android platform only and include some native plugin that will do the job for you -- for example something like this one.

Haldan answered 3/8, 2013 at 16:30 Comment(0)
V
0

You can use the "Device" object from Phonegap. Since "device" is assigned to the window object, it is implicitly in the global scope so you can easily call it in your onDeviceReady function eg:

var deviceID = device.uuid

Please note that on Android it returns a random 64-bit integer as a string. The integer is generated on the device's first boot. On BlackBerry it returns the PIN number of the device. This is a nine-digit unique integer as a string and on iPhone it returns a string of hash values created from multiple hardware identifies.It is guaranteed to be unique for every device and cannot be tied to the user account.

See the full example here

Viipuri answered 4/8, 2013 at 3:1 Comment(0)
B
0

You may change getUuid function defined in platforms/android/src/org/apache/cordova/device/Device.java in this way

public String getUuid() {
    //String uuid = Settings.Secure.getString(this.cordova.getActivity().getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
    TelephonyManager tManager = (TelephonyManager)this.cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
    String uuid = tManager.getDeviceId();
    return uuid;
}

just comment the first line and add the two new ones.

You also have to add

import android.content.Context;
import android.telephony.TelephonyManager;
Boadicea answered 13/4, 2014 at 22:20 Comment(0)
M
0

I have created a plugin called cordova-plugin-imeigetter for cordova (only for Android devices) that does what you want. I hope it helps you.


Installation:

cordova plugin add https://github.com/tomloprod/cordova-plugin-imeigetter.git

Usage:

This plugin exports an object with one method called get:

window.plugins.imeiGetter.get(function(imei){
   console.log(imei);
});

GitHub: https://github.com/tomloprod/cordova-plugin-imeigetter

Mint answered 9/1, 2017 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.