Android的 - 从JNI得到MEID [英] Android -- get MEID from JNI
问题描述
我在Android的工作,写一些JNI code和我正在寻找一种方式来查询设备的移动设备识别码(MEID)。
I'm working in Android, writing some JNI code, and I'm looking for a way to query the Mobile Equipment Identifier (MEID) from the device.
http://en.wikipedia.org/wiki/Mobile_equipment_identifier
我试着写C或C ++ code,可自行在Android设备上运行,所以我不认为我可以使用Java(即从获得MEID TelephonyManager
)。
I'm trying to write C or C++ code that can run by itself on an Android device, so I don't think I can use Java (i.e., get MEID from TelephonyManager
).
一个搜索计算器的认定:<一href=\"http://stackoverflow.com/questions/6852106/is-there-an-android-shell-or-adb-command-that-i-could-use-to-get-a-devices-imei\">Is有一个Android shell或ADB命令,我可以用得到的设备的IMEI / MEID?
A search of StackOverflow finds: Is there an android shell or adb command that I could use to get a device's IMEI/MEID?
好大, dumpsys iphonesubinfo
可以得到我需要的信息。和它的作品!
Okay great, dumpsys iphonesubinfo
can get the info I need. And it works!
我无法找到 dumpsys
源除了作为源为Android的一部分。所以,我下载了......我的硬盘填满了下载完成之前,但我没有得到源$ C $ C到 dumpsys
。这是一个令人惊讶的简短的C ++文件。它所做的就是查询Android的的IBinder
接口。
I couldn't find the source for dumpsys
except as part of the source for Android. So I downloaded that... my hard disk filled up before the download finished, but I did get the source code to dumpsys
. It is a surprisingly short C++ file. All it does is query Android's IBinder
interface.
所以,我的问题:
0)有什么办法我可以写在NDK仅使用的东西对的IBinder
查询?通过 dumpsys.cpp
使用的包含文件不在NDK和的grep
在NDK目录中没有发现的IBinder
中的任何包含文件或code样品,所以我的猜测是没有(但我想是错误的)。
0) Is there any way I can write a query against IBinder
using just the stuff in the NDK? The include files used by dumpsys.cpp
are not in the NDK, and grep
in the NDK directory didn't find IBinder
in any include files or code samples, so my guess is "no" (but I would like to be wrong).
1)是否有任何其他好的方式得到MEID?
1) Is there any other good way to get the MEID?
我认真想我应该只使用系统(dumpsys iphonesubinfo&GT; /tmp/myprogname_dumpsys.tmp
,然后打开生成的文件并解析它这一点。应该工作,但是我很难把它优雅......我不知道,如果 dumpsys
可每Android设备上或没有。
I'm seriously thinking I should just use system("dumpsys iphonesubinfo > /tmp/myprogname_dumpsys.tmp"
and then open the resulting file and parse it. That should work, but I would hardly call it elegant... and I'm not sure if dumpsys
is available on every Android device or not.
编辑:使用的理念体系()
来运行 dumpsys
将无法工作,因为 dumpsys
需要 android.permission.DUMP
和Android不再允许非系统应用程序有这个权限。
The idea of using system()
to run dumpsys
will not work, because dumpsys
needs android.permission.DUMP
and Android no longer allows non-system apps to have that permission.
推荐答案
我相信的Dalvik实现所有的JVM做同样的JNI接口,因此,尽管这是一个有点繁琐,这是完全可能使从本地code调用通过JNI任意Java类和方法。
I believe Dalvik implements all the same JNI interfaces that the JVM does, so while it's a bit fiddly, it's perfectly possible to make calls from native code through JNI to arbitrary Java classes and methods.
/* assuming you already have */
JNIEnv *env;
jobject context;
/* then call (with error-checking) */
jclass cls = (*env)->FindClass(env, "android/context/Context");
jmethodId mid = (*env)->GetMethodID(env, context_cls, "getSystemService",
"(Ljava/lang/String;)Ljava/lang/Object;");
jfieldID fid = (*env)->GetStaticFieldID(env, cls, "TELEPHONY_SERVICE",
"Ljava/lang/String;");
jstring str = (*env)->GetStaticObjectField(env, cls, fid);
jobject telephony = (*env)->CallObjectMethod(env, context, mid, str);
cls = (*env)->FindClass(env, "android/telephony/TelephonyManager");
mid =(*env)->GetMethodID(env, cls, "getDeviceId", "()Ljava/lang/String;");
str = (*env)->CallObjectMethod(env, telephony, mid);
jsize len = (*env)->GetStringUTFLength(env, str);
char* deviceId = calloc(len + 1, 1);
(*env)->GetStringUTFRegion(env, str, 0, len, deviceId);
(*env)->DeleteLocalRef(env, str);
/* to get a string in deviceId */
这篇关于Android的 - 从JNI得到MEID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!