Jni : Call a java method within C++ code, errors

1.2k Views Asked by At

I am working on a project for my engineering school, I have to make two android devices communicate via Wi-Fi. One of the Android devices will turn its hotspot on, and the other device will connect to it.

I am working with Qt 5.4.1 (Qt Creator 3.4.1) in C++.

By searching the web, I found this Java Class : WifiManager.java that has methods to (dis/en)able Wifi Access Point : setWifiApEnabled

Following Bogdan Vatra's guidelines, I successfully called a Java method within a c++ code. For example for fibonacci function :

JniMath.java:

package org.app.test;

public class JniMath {

    public static int fibonacci(int n)
    {
        if(n<0)
            return -1;
        else if(n==0 || n==1)
            return n;

        return fibonacci(n-1)+fibonacci(n-2);
    }
}

C++ code :

int FenetrePrinc::fibonacci(int n)
{
    return QAndroidJniObject::callStaticMethod<jint>("org/app/test/JniMath"
                                                     , "fibonacci"
                                                     , "(I)I" //Because int fibonacci(int)
                                                     , n);
}

It works.

But if I want to do the same for the function "setWifiApEnabled", from "WifiApManager.java" :

package org.app.test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.util.ArrayList;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.util.Log;

public class WifiApManager {
    private final WifiManager mWifiManager;
    private Context context;

    public WifiApManager(Context context) {
        this.context = context;
        mWifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
    }

    public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
        try {
            if (enabled) { // disable WiFi in any case
                mWifiManager.setWifiEnabled(false);
            }

            Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
            return (Boolean) method.invoke(mWifiManager, wifiConfig, enabled);
        } catch (Exception e) {
            Log.e(this.getClass().toString(), "", e);
            return false;
        }
    }

    public WifiConfiguration getWifiApConfiguration() {
        try {
            Method method = mWifiManager.getClass().getMethod("getWifiApConfiguration");
            return (WifiConfiguration) method.invoke(mWifiManager);
        } catch (Exception e) {
            Log.e(this.getClass().toString(), "", e);
            return null;
        }
    }
}

C++ code :

class WifiManager:
{
public:
    WifiManager()
    {
        myJavaWifiApManager = QAndroidJniObject("org/app/test/WifiApManager");
        if(myJavaWifiApManager.isValid())
            qDebug() << "Object is valid";
        else 
            qDebug() << "Object isn't valid";
    }

    bool setWifiApEnabled(QAndroidJniObject wifiConfig, bool enabled)
    {
        return myJavaWifiApManager.callMethod<jboolean>("org/app/test/WifiApManager"
                                               , "setWifiApEnabled"
                                               , "(Landroid/net/wifi/WifiConfiguration;Z)Z"
                                               , wifiConfig.object<jobject>(), enabled);
    }

    QAndroidJniObject WifiManager::getWifiApConfiguration()
    {
        return myJavaWifiApManager.callObjectMethod<jobject>("getWifiApConfiguration"
                                               , "()Landroid/net/wifi/WifiConfiguration");
    }

private:
    QAndroidJniObject myJavaWifiApManager;
    QAndroidJniObject myWifiConfig;        

It compiles well, but when I lauch it, it says that "myJavaWifiApManager is not valid" So if I try to call setWifiApEnabled or getWifiApConfiguration it crashes. Any idea why the object is invalid ? It is described in "WifiApManager.java", located in "%%PROJECT_FOLDER%%/android/src/org/app/test"

If I declare the object as myJavaWifiApManager = QAndroidJniObject("org/app/test/JniMath");, the object is valid

0

There are 0 best solutions below