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;
-
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;
@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?
-
@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 foundJNI DETECTED ERROR IN APPLICATION: GetStringUTFLength received NULL jstring in call to GetStringUTFLength
-
@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 foundJNI DETECTED ERROR IN APPLICATION: GetStringUTFLength received NULL jstring in call to GetStringUTFLength
I'm not tested your code. But i have two questions.
- Is the device(like cell phone) subscribed to the carrier?
- Is permission(
android.permission.READ_PHONE_STATE
) added?
If either is not,
getDeviceId
return null. -
I'm not tested your code. But i have two questions.
- Is the device(like cell phone) subscribed to the carrier?
- Is permission(
android.permission.READ_PHONE_STATE
) added?
If either is not,
getDeviceId
return null.- device -> asus nexus 7 (2012)
- yes, permission added
please someone who can. test this code snippet.
-
- device -> asus nexus 7 (2012)
- yes, permission added
please someone who can. test this code snippet.
Your code worked well, but not
GetStringUTFLength()
.
You should doQString imei = QAndroidJniObject::fromLocalRef(jstr).toString()
instead.
I guess, your phone is not subscribed.
So that phone does not have an IMEI code.
ThegetDeviceId()
is always null. -
Your code worked well, but not
GetStringUTFLength()
.
You should doQString imei = QAndroidJniObject::fromLocalRef(jstr).toString()
instead.
I guess, your phone is not subscribed.
So that phone does not have an IMEI code.
ThegetDeviceId()
is always null.@Devopia53 one of these days I'll try this code on another device. Thank you for the answer!
-
Your code worked well, but not
GetStringUTFLength()
.
You should doQString imei = QAndroidJniObject::fromLocalRef(jstr).toString()
instead.
I guess, your phone is not subscribed.
So that phone does not have an IMEI code.
ThegetDeviceId()
is always null.@Devopia53 you rights. this code work only with GSM module.
How to change this code to find ou the MAC ?