0

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?

Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
Ash
  • 41
  • 7
  • Did you add `QT += androidextras` to the .pro file? – Frank Osterfeld Nov 23 '18 at 07:30
  • @FrankOsterfeld I have added QT += androidextras ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android in my pro file – Ash Nov 23 '18 at 08:40
  • By adding this line `QAndroidJniObject returnString = QAndroidJniObject::callStaticObjectMethod("org/qtproject/example/QtAndroidToastJava","getWifiName","(Landroid/content/Context)",QtAndroid::androidContext().object());**` I am getting error: **"W System.err: java.lang.NoSuchMethodError: no static method "Lorg/qtproject/example/QtAndroidToastJava;.getWifiName(Landroid/content/Context)""** I think I need proper arguments for context as I am using context **getWifiName(Context context)** as parameter to my function in java@FrankOsterfeld what should I add in place of(Landroid/content/Context). – Ash Nov 23 '18 at 08:55

1 Answers1

0

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.

Felix
  • 6,885
  • 1
  • 29
  • 54
  • In that case you should accept/upvote the answer, so other people can see that this solved the problem. – Felix Nov 23 '18 at 12:34