IMEI on android(with JNA)



  • Good day. I apologize for my English. I use Google Translate. I have the code. This code works without error in 2014. At the moment, I get an error >> JNI DETECTED ERROR IN APPLICATION: GetStringUTFLength received NULL jstring in call to GetStringITFLength

    please help me edit the code and remove the error (to bring it into working condition)

        QAndroidJniEnvironment env;
        jclass contextClass = env->FindClass("android/content/Context");
        jfieldID fieldId = env->GetStaticFieldID(contextClass, "TELEPHONY_SERVICE", "Ljava/lang/String;");
        jstring telephonyManagerType = (jstring) env->GetStaticObjectField(contextClass, fieldId);
    
        jclass telephonyManagerClass = env->FindClass("android/telephony/TelephonyManager");
        jmethodID methodId = env->GetMethodID(contextClass, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
    
       QAndroidJniObject qtActivityObj = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative",  "activity", "()Landroid/app/Activity;");
        jobject telephonyManager = env->CallObjectMethod(qtActivityObj.object<jobject>(), methodId, telephonyManagerType);
    
        methodId = env->GetMethodID(telephonyManagerClass, "getDeviceId", "()Ljava/lang/String;");
        jstring jstr = (jstring) env->CallObjectMethod(telephonyManager, methodId);
    
        jsize len = env->GetStringUTFLength(jstr);
        char* buf_devid = new char[32];
        env->GetStringUTFRegion(jstr, 0, len, buf_devid);
        QString imei(buf_devid);
        delete buf_devid;
        return imei;

  • Moderators

    @sufferin_ollegator It looks like jstr is NULL when you call

    jsize len = env->GetStringUTFLength(jstr);
    

    You need to check why

    jstring jstr = (jstring) env->CallObjectMethod(telephonyManager, methodId);
    

    returns NULL.
    In general you do not have any checks for anything in your code! You should check the values you get and print error messages if something is not valid. For example:

    jclass contextClass = env->FindClass("android/content/Context");
    

    you do not check whether contextClass is valid. What if the class was not found?



  • @jsulm i try this check.

        QAndroidJniEnvironment env;
        jclass contextClass = env->FindClass("android/content/Context");
        if(contextClass){
            qDebug() << "OLLEG: 1 found";
        }
        else{
            qDebug() << "OLLEG: 1 NOT found";
        }
    
        jfieldID fieldId = env->GetStaticFieldID(contextClass, "TELEPHONY_SERVICE", "Ljava/lang/String;");
        if(fieldId){
            qDebug() << "OLLEG: 2 found";
        }
        else{
            qDebug() << "OLLEG: 2 NOT found";
        }
    
    
        jstring telephonyManagerType = (jstring) env->GetStaticObjectField(contextClass, fieldId);
        if(telephonyManagerType){
            qDebug() << "OLLEG: 3 found";
        }
        else{
            qDebug() << "OLLEG: 3 NOT found";
        }
    
        jclass telephonyManagerClass = env->FindClass("android/telephony/TelephonyManager");
        if(telephonyManagerClass){
            qDebug() << "OLLEG: 4 found";
        }
        else{
            qDebug() << "OLLEG: 4 NOT found";
        }
    
    
        jmethodID methodId = env->GetMethodID(contextClass, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
        if(methodId){
            qDebug() << "OLLEG: 5 found";
        }
        else{
            qDebug() << "OLLEG: 5 NOT found";
        }
    
        QAndroidJniObject qtActivityObj = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative",  "activity", "()Landroid/app/Activity;");
    
        jobject telephonyManager = env->CallObjectMethod(qtActivityObj.object<jobject>(), methodId, telephonyManagerType);
        if(telephonyManager){
            qDebug() << "OLLEG: 6 found";
        }
        else{
            qDebug() << "OLLEG: 6 NOT found";
        }
    
        methodId = env->GetMethodID(telephonyManagerClass, "getDeviceId", "()Ljava/lang/String;");
        if(methodId){
            qDebug() << "OLLEG: 7 found";
        }
        else{
            qDebug() << "OLLEG: 7 NOT found";
        }
    
        jstring jstr = (jstring) env->CallObjectMethod(telephonyManager, methodId);
        if(jstr){
            qDebug() << "OLLEG: 8 found";
        }
        else{
            qDebug() << "OLLEG: 8 NOT found";
        }
    
        jsize len = env->GetStringUTFLength(jstr);
        char* buf_devid = new char[32];
        env->GetStringUTFRegion(jstr, 0, len, buf_devid);
        QString imei(buf_devid);
        delete buf_devid;
        return imei;
    
        **CONSOLE:** 
    

    OLLEG: 1 found
    OLLEG: 2 found
    OLLEG: 3 found
    OLLEG: 4 found
    OLLEG: 5 found
    OLLEG: 6 found
    OLLEG: 7 found
    OLLEG: 8 NOT found

    JNI DETECTED ERROR IN APPLICATION: GetStringUTFLength received NULL jstring in call to GetStringUTFLength



  • @sufferin_ollegator

    I'm not tested your code. But i have two questions.

    1. Is the device(like cell phone) subscribed to the carrier?
    2. Is permission(android.permission.READ_PHONE_STATE) added?

    If either is not, getDeviceId return null.



  • @Devopia53

    1. device -> asus nexus 7 (2012)
    2. yes, permission added

    please someone who can. test this code snippet.



  • @sufferin_ollegator

    Your code worked well, but not GetStringUTFLength().
    You should do QString imei = QAndroidJniObject::fromLocalRef(jstr).toString() instead.
    I guess, your phone is not subscribed.
    So that phone does not have an IMEI code.
    The getDeviceId() is always null.



  • @Devopia53 one of these days I'll try this code on another device. Thank you for the answer!



  • @Devopia53 you rights. this code work only with GSM module.

    How to change this code to find ou the MAC ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.