How to get IMEI number in PhoneGap?

前端 未结 8 507
北海茫月
北海茫月 2020-12-03 11:30

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

相关标签:
8条回答
  • 2020-12-03 11:57

    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);                   
    });
    
    0 讨论(0)
  • 2020-12-03 11:59

    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.

    0 讨论(0)
提交回复
热议问题