How to get MAC Address of Android device by calling java function from Qt Application
-
Hi,
i am trying to run Qt Application to get MAC address of android device by calling java function.
here is the code:
// MyClass.java
package org.qtproject.example.JavaCalling;
public class MyClass extends QtActivity{
public String MyMethod() {
WifiManager manager=(WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info=manager.getConnectionInfo();
System.out.println("MAC Address:"+info.getMacAddress());
return info.getMacAddress();
}
}//in main.cpp calling java fuction as below
QAndroidJniObject obj("org/qtproject/example/JavaCalling/MyClass");
QString str= obj.callObjectMethod<jstring>("MyMethod").toString();while run the application its getting crash.
Any solutions??Thank you in advance,
Bahubali P S -
can you give some output?
but I think you should checking example Qt Notification
http://doc.qt.io/qt-5/qtandroidextras-notification-example.html
it will have you to start to call from c++ to java -
@kd_wala
ya sure,here is error :
JNI DETECTED ERROR IN APPLICATION: JNI GetMethodID called with pending exception java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
F art : art/runtime/java_vm_ext.cc:410] at void android.os.Handler.<init>(android.os.Handler$Callback, boolean) (Handler.java:200)
F art : art/runtime/java_vm_ext.cc:410] at void android.os.Handler.<init>() (Handler.java:114)
F art : art/runtime/java_vm_ext.cc:410] at void android.app.Activity.<init>() (Activity.java:758)
F art : art/runtime/java_vm_ext.cc:410] at void org.qtproject.qt5.android.bindings.QtActivity.<init>() (QtActivity.java:94)
F art : art/runtime/java_vm_ext.cc:410] at void org.qtproject.example.JavaCalling.MyClass.<init>() (MyClass.java:21)
F art : art/runtime/java_vm_ext.cc:410]
F art : art/runtime/java_vm_ext.cc:410] in call to GetMethodID
F art : art/runtime/java_vm_ext.cc:410] "QtThread" prio=5 tid=11 Runnable
F art : art/runtime/java_vm_ext.cc:410] | group="main" sCount=0 dsCount=0 obj=0x12d70400 self=0xb844db68
F art : art/runtime/java_vm_ext.cc:410] | sysTid=19191 nice=0 cgrp=default sched=0/0 handle=0xa3052930
F art : art/runtime/java_vm_ext.cc:410] | state=R schedstat=( 0 0 0 ) utm=0 stm=0 core=2 HZ=100
F art : art/runtime/java_vm_ext.cc:410] | stack=0xa2f56000-0xa2f58000 stackSize=1014KB
F art : art/runtime/java_vm_ext.cc:410] | held mutexes= "mutator lock"(shared held)
F art : art/runtime/java_vm_ext.cc:410] native: #00 pc 00370c01 /system/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int, char const*, art::ArtMethod*, void*)+160)
F art : art/runtime/java_vm_ext.cc:410] native: #01 pc 0035054b /system/lib/libart.so (art::Thread::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const+150)
F art : art/runtime/java_vm_ext.cc:410] native: #02 pc 0025a50d /system/lib/libart.so (art::JavaVMExt::JniAbort(char const*, char const*)+740)
F art : art/runtime/java_vm_ext.cc:410] native: #03 pc 0025abe5 /system/lib/libart.so (art::JavaVMExt::JniAbortV(char const*, char const*, std::__va_list)+64)
F art : art/runtime/java_vm_ext.cc:410] native: #04 pc 000fd2e1 /system/lib/libart.so (art::ScopedCheck::AbortF(char const*, ...)+32)
F art : art/runtime/java_vm_ext.cc:410] native: #05 pc 001023f5 /system/lib/libart.so (art::ScopedCheck::Check(art::ScopedObjectAccess&, bool, char const*, art::JniValueType*) (.constprop.95)+5072)
F art : art/runtime/java_vm_ext.cc:410] native: #06 pc 0011303d /system/lib/libart.so (art::CheckJNI::GetMethodID(_JNIEnv*, _jclass*, char const*, char const*)+396)
F art : art/runtime/java_vm_ext.cc:410] native: #07 pc 001c7f49 /data/app/org.qtproject.example.JavaCalling-2/lib/arm/libQt5Core.so (???)
F art : art/runtime/java_vm_ext.cc:410] native: #08 pc 001c9eff /data/app/org.qtproject.example.JavaCalling-2/lib/arm/libQt5Core.so (QJNIObjectPrivate::callObjectMethodV(char const*, char const*, std::__va_list) const+42)
F art : art/runtime/java_vm_ext.cc:410] native: #09 pc 001c9f7f /data/app/org.qtproject.example.JavaCalling-2/lib/arm/libQt5Core.so (QJNIObjectPrivate::callObjectMethod(char const*, char const*, ...) const+18)
F art : art/runtime/java_vm_ext.cc:410] native: #10 pc 000096d5 /data/app/org.qtproject.example.JavaCalling-2/lib/arm/libQt5AndroidExtras.so (QAndroidJniObject QAndroidJniObject::callObjectMethod<_jstring*>(char const*) const+12)
F art : art/runtime/java_vm_ext.cc:410] native: #11 pc 00001c9c /data/app/org.qtproject.example.JavaCalling-2/lib/arm/libJavaCalling.so (MyFunction()+68)
F art : art/runtime/java_vm_ext.cc:410] native: #12 pc 00001d4c /data/app/org.qtproject.example.JavaCalling-2/lib/arm/libJavaCalling.so (main+68)
F art : art/runtime/java_vm_ext.cc:410] native: #13 pc 0001a87d /data/data/org.qtproject.example.JavaCalling/qt-reserved-files/plugins/platforms/android/libqtforandroid.so (???)
F art : art/runtime/java_vm_ext.cc:410] native: #14 pc 00041687 /system/lib/libc.so (__pthread_start(void*)+30)
F art : art/runtime/java_vm_ext.cc:410] native: #15 pc 0001911b /system/lib/libc.so (__start_thread+6)
F art : art/runtime/java_vm_ext.cc:410] (no managed stack frames)
F art : art/runtime/java_vm_ext.cc:410]
F art : art/runtime/runtime.cc:399] Runtime aborting...
F art : art/runtime/runtime.cc:399] Aborting thread:i have already tried one simple program to call java function and it worked, but not for MAC address. please suggest me.
-
@Bahubali_p_s
some Java methods cannot be called from a worker thread. (Qt on Android runs always in a worker thread).
You need to run it on the Android's UIThread.Since Qt 5.7 there is the method QtAndroid::runOnUiThread()
See this example (replace KDAB::Android:: namespace with QtAndroid:: namespace) -
@raven-worx
thanks for your reply. Finally i got the solution for this quest.
i reffered following link to get the solution.
https://www.kdab.com/qt-android-episode-7/Thanks all.