Native android code with QT
-
Hm, looks like the static method call needs a jclass instead of string. Weird.
Try this:
jclass myClass = "com/amin/QtAndroidToastJava/QtAndroidToastJava"; auto returnString = QAndroidJniObject::callStaticMethod <jstring>( myClass, "getWifiName", "(V)Ljava/lang/String;");
-
Hi sir @sierdzio
What I did is in my cpp file I added this lineQAndroidJniObject returnString = QAndroidJniObject::callStaticObjectMethod("org/qtproject/example/QtAndroidToastJava","getWifiName","(Landroid/content/Context)",QtAndroid::androidContext().object());
As I am using getWifiName(Context context) function it requires context from cpp.
What I did is I added "(Landroid/content/Context)",QtAndroid::androidContext().object()); to check if it works but it is throwing these errors:W System.err: java.lang.NoSuchMethodError: no static method "Lorg/qtproject/example/QtAndroidToastJava;.getWifiName(Landroid/content/Context)"
W System.err: at org.qtproject.qt5.android.QtNative.startQtApplication(Native Method)
W System.err: at org.qtproject.qt5.android.QtNative$6.run(QtNative.java:359)
W System.err: at org.qtproject.qt5.android.QtThread$1.run(QtThread.java:61)I think I am making some mistake in this line "(Landroid/content/Context)",QtAndroid::androidContext().object());"
how can I figure out what I should pass or where its getting wrong?
-
@ashajg said in Native android code with QT:
how can I figure out what I should pass or where its getting wrong?
Only by guessing, unfortunately. JNI is not clever enough to point our errors exactly.
"(Landroid/content/Context)"
- no return type (your method returns a string).I did tell you to drop the context arg, by the way. But if you want to keep it - so be it.
-
ok Sir
I ll also try thisjclass myClass = "com/amin/QtAndroidToastJava/QtAndroidToastJava";
auto returnString = QAndroidJniObject::callStaticMethod <jstring>(
myClass, "getWifiName", "(V)Ljava/lang/String;");it is saying cannot initialize a variable with jclass..
-
you asked to drop context so should I just write the code like this
public class QtAndroidToastJava extends QtActivity { public String getWifiName(){ WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (manager.isWifiEnabled()) { WifiInfo wifiInfo = manager.getConnectionInfo(); if (wifiInfo != null) { DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()); if (state == DetailedState.CONNECTED || state == DetailedState.OBTAINING_IPADDR) { return wifiInfo.getSSID(); } } }
its throwing error
symbol: variable context
location: class QtAndroidToastJava
Note: Some input files use or override a deprecated API. if I remove context -
I am trying to do the same thing. But
auto returnString = QAndroidJniObject::callStaticMethod <jstring>(
myClass, "getWifiName", "(V)Ljava/lang/String;");
causes compiling error.
undefined reference to `_jstring* QAndroidJniObject::callStaticMethod<_jstring*>(char const*, char const*, char const*, ...)
QAndroidJniObject::callStaticMethod <jstring> is dropped? I tried int and func call works.I checked the doc of Qt5
https://doc.qt.io/qt-5/qandroidjniobject.html
// C++ code
QAndroidJniObject stringNumber = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/TestClass",
"fromNumber"
"(I)Ljava/lang/String;",
10);
should be work. But the call in my app returns empty object.
My Qt version: 5.15.2 and OS: Ubuntu 18.04