简体   繁体   English

如何在PhoneGap中获取IMEI号码?

[英]How to get IMEI number in PhoneGap?

I'm developing a PhoneGap Android mobile application using jQuery, JavaScript and HTML. 我正在使用jQuery,JavaScript和HTML开发PhoneGap Android移动应用程序。 I want to get the mobile IMEI. 我想获得移动IMEI。 I have tried this code from this Tutorial . 我在本教程中尝试了这段代码。

I am getting the number like this: 97734a345d234d . 我得到的数字是这样的: 97734a345d234d I have checked my device to get IMEI number using *#06# . 我已经使用*#06#检查了我的设备以获取IMEI号码。 I don't know whether it is correct or not. 我不知道它是否正确。

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 获取扩展DroidGap类的类中的IMEI编号,并在静态成员中保存imei编号的值,然后从任何地方访问此静态字段...示例代码在这里

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();

You could write a phonegap plugin to return you the IMEI number. 您可以编写一个phonegap插件来返回IMEI号码。 As the phonegap doesnt return imei for Android devices, it returns an random 64-bit number. 由于手机没有为Android设备返回imei,它会返回一个随机的64位数字。

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). 这是一个完整的插件,可以在phonegap应用程序中获取真正的IMEI号码(请注意,此插件不是“可插件”,但如果按照说明操作,它将起作用。我已经在phonegap 2.9上进行了测试。我打算写一个真正的“可插拔的“插件,带有一些设备信息,适用于phonegap 3.0)。

Here is the java class for that extends from CordovaPlugin ( DeviceInfo.java ): 这是从CordovaPlugin( DeviceInfo.java )扩展的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): 然后你需要一个js对象,这样你就可以在另一端访问你的插件(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: 在项目内的res / xml / config.xml文件中添加插件,如下所示:

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

And add the READ_PHONE_STATE permission inside your AndroidManifest.xml: 并在AndroidManifest.xml中添加READ_PHONE_STATE权限:

<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: 在此之后,您需要将deviceinfo.js文件添加到您的页面中,在设备准备完成后,您可以使用以下插件:

plugins.DeviceInfo.imeiNumber(function(imei){
   console.log("imei "+ imei);                   
});

You cannot access the IMEI via html or JavaScript. 您无法通过html或JavaScript访问IMEI。 But you can write an app which reads the IMEI for you. 但是你可以编写一个为你读取IMEI的应用程序。

Just call getDeviceId() . 只需调用getDeviceId()即可 Don't forget that you need the READ_PHONE_STATE permission in your manifest. 不要忘记您需要清单中的READ_PHONE_STATE权限。

You're pointing to PhoneGap Tutorial version 1.0.0 . 您指的是PhoneGap Tutorial 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 . 例如,如果您阅读PhoneGap版本2.1.0的相同页面,您会注意到对device.uuid的评论快速示例 ,据说, 只有在Tizen平台的情况下device.uuid返回IMEI号码。 All other platforms, including Android, you mentioned, does return something else . 您提到的所有其他平台,包括Android,都会返回其他内容

You can't get device's IMEI in pure HTML / Javascript (application compiled through PhoneGap Build). 您无法在纯HTML / Javascript(通过PhoneGap Build编译的应用程序)中获取设备的IMEI。 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 . 要阅读它,你必须在本地构建,仅适用于Android平台,并包含一些可以为你完成工作的本机插件 - 例如像这样的东西。

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. 我相信只有利用IMEI的其他一些应用程序记录才能实现这一点,我不确定是否有。

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 祝你好运,回复成功/失败

You can use the "Device" object from Phonegap. 您可以使用Phonegap中的“Device”对象 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: 由于“device”被分配给window对象,因此它隐式位于全局范围内,因此您可以在onDeviceReady函数中轻松调用它,例如:

var deviceID = device.uuid

Please note that on Android it returns a random 64-bit integer as a string. 请注意,在Android上,它会返回一个随机的64位整数作为字符串。 The integer is generated on the device's first boot. 在设备的首次启动时生成整数。 On BlackBerry it returns the PIN number of the device. 在BlackBerry上,它会返回设备的PIN码。 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. 这是一个九位数的唯一整数作为字符串,在iPhone上它返回一个由多个硬件标识创建的哈希值字符串。它保证对每个设备都是唯一的,不能绑定到用户帐户。

See the full example here 请参阅此处的完整示例

You may change getUuid function defined in platforms/android/src/org/apache/cordova/device/Device.java in this way 您可以通过这种方式更改在platforms / android / src / org / apache / cordova / device / Device.java中定义的getUuid函数

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;

I have created a plugin called cordova-plugin-imeigetter for cordova (only for Android devices) that does what you want. 我已经为cordova(仅适用于Android设备)创建了一个名为cordova-plugin-imeigetter您的需求。 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 : 这个插件使用一个名为get方法导出一个对象:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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