Android — get MEID from JNI

前端 未结 2 1133
甜味超标
甜味超标 2021-01-25 00:22

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/

相关标签:
2条回答
  • 2021-01-25 00:49

    Retrieve the MEID on the Java side, then pass into your JNI function as a jstring parameter. It'll be cleaner than calling back to Java from C.

    As to how to retrieve that, see Abhilasha's answer.

    0 讨论(0)
  • 2021-01-25 00:54

    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 */
    
    0 讨论(0)
提交回复
热议问题