I am trying to get wifi name connected to my mobile using QAndroidJniObject.
java file:
package org.qtproject.example;
import android.net.NetworkInfo.DetailedState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class QtAndroidToastJava extends QtActivity
{
public static String getWifiName(Context context) {
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();
}
}
}
return null;
}
}
My cpp code is
void WIFICLASS::updateAndroidNotification()
{
qDebug()<<"******************************************8";
auto returnString = QAndroidJniObject::callStaticMethod <jstring>("org/qtproject/example/QtAndroidToastJava",
"getWifiName","(V;)Ljava/lang/String");
// // QString user = juser.toString();
// qDebug()<<"ANSWER"<<user;
qDebug()<<returnString;
}
After trying to build this I am getting this errors: 23: error: undefined reference to '_jstring* QAndroidJniObject::callStaticMethod<_jstring*>(char const*, char const*, char const*, ...)'
How can I solve this issue?
What is the correct way to do this?
There are two things wrong here:
1.) The message signature you are passing in C++ is wrong. It should be:
Mind the
;
at the end of each class name! It is alwaysL<classname>;
! Also, you have to always exactly match the method as declared in java. Multiple parameters do not need to be seperated. If you have e.g. a methodvoid test(int a, int b)
, the signature would be(II)V
.2.) The method you are calling is an object method, which means you must use
QAndroidJniObject::callStaticObjectMethod
That method returns you an
QAndroidJniObject
and you can callQAndroidJniObject::toString()
to convert the result to a string.