Get context in Java code run from C++



  • Hi
    I would like to create programmatically an access point in Qt8 on Android. It seems that this is possible in Java with the code below

      public int createAp(String ssid, String pwd) {
          WifiConfiguration netConfig = new WifiConfiguration();
    
          netConfig.SSID = ssid;
          netConfig.preSharedKey = pwd;
          netConfig.hiddenSSID = false;
          netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
          netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
          netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
          netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    
          try {
              WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
              Method setWifiApMethod = wifiManager.getClass().getMethod("setWifiApEnabled",  WifiConfiguration.class, boolean.class);
              boolean apstatus=(Boolean) setWifiApMethod.invoke(wifiManager, netConfig, true);
    
              Method isWifiApEnabledmethod = wifiManager.getClass().getMethod("isWifiApEnabled");
              while(!(Boolean)isWifiApEnabledmethod.invoke(wifiManager)){};
              Method getWifiApStateMethod = wifiManager.getClass().getMethod("getWifiApState");
              int apstate=(Integer)getWifiApStateMethod.invoke(wifiManager);
              Method getWifiApConfigurationMethod = wifiManager.getClass().getMethod("getWifiApConfiguration");
              netConfig=(WifiConfiguration)getWifiApConfigurationMethod.invoke(wifiManager);
          } catch (Exception e) {
              return -1;
          }
          return 0;
    

    However, the method above requires the Activity context (getBaseContext()). So my question is if there is a way to obtain this from Qt application that runs this Java code ?

    thanks
    Bogdan



  • @cristeab Hi;
    You must to add a new parameter to createAp method as Context context. Then you must to pass QtAndroid::activityContext() to this parameter. You can write full code like this:
    Create Templates -> android/src/com/myjavacode/MyJava.java:

    package com.myjavacode;
    
    import android.content.Context;
    import android.net.wifi.WifiConfiguration;
    import android.net.wifi.WifiManager;
    
    import java.lang.reflect.Method;
    
    public class MyJava extends org.qtproject.qt5.android.bindings.QtActivity
    {
      // You use static method:
      public static int createAp(String ssid, String pwd, Context context /* Add context parameter */) {
        WifiConfiguration netConfig = new WifiConfiguration();
    
        netConfig.SSID = ssid;
        netConfig.preSharedKey = pwd;
        netConfig.hiddenSSID = false;
        netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    
        try {
          WifiManager wifiManager = (WifiManager) /* change getBaseContext() to context parameter */ context.getSystemService(Context.WIFI_SERVICE);
          Method setWifiApMethod = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
          boolean apstatus = (Boolean) setWifiApMethod.invoke(wifiManager, netConfig, true);
    
          Method isWifiApEnabledmethod = wifiManager.getClass().getMethod("isWifiApEnabled");
          while (!(Boolean) isWifiApEnabledmethod.invoke(wifiManager)) {
          }
          ;
          Method getWifiApStateMethod = wifiManager.getClass().getMethod("getWifiApState");
          int apstate = (Integer) getWifiApStateMethod.invoke(wifiManager);
          Method getWifiApConfigurationMethod = wifiManager.getClass().getMethod("getWifiApConfiguration");
          netConfig = (WifiConfiguration) getWifiApConfigurationMethod.invoke(wifiManager);
        } catch (Exception e) {
          return -1;
        }
        return 0;
      }
    }
    

    You can use this Java code like this:

    QAndroidJniObject ssid = QAndroidJniObject::fromString("ssid");
      QAndroidJniObject pwd = QAndroidJniObject::fromString("pwd");
    
      jint res = QAndroidJniObject::callStaticMethod<jint>("com/myjavacode/MyJava",
                                                           "createAp",
                                                           "(Ljava/lang/String;Ljava/lang/String;Landroid/content/Context)I",
                                                           ssid.object<jstring>(), pwd.object<jstring>(), QtAndroid::androidContext().object());
    


  • thank you, it works


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.