How to integrate android native code with Qt Quick project?

374 views Asked by At

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?

1

There are 1 answers

1
Felix On BEST ANSWER

There are two things wrong here:

1.) The message signature you are passing in C++ is wrong. It should be:

"(Landroid/content/Context;)Ljava/lang/String;"

Mind the ; at the end of each class name! It is always L<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 method void 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

auto res = QAndroidJniObject::callStaticObjectMethod("org/qtproject/example/QtAndroidToastJava",
                                                     "getWifiName",
                                                     "(Landroid/content/Context;)Ljava/lang/String;",
                                                     QtAndroid::androidContext().object());

That method returns you an QAndroidJniObject and you can call QAndroidJniObject::toString() to convert the result to a string.