Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Get context in Java code run from C++
Forum Update on Monday, May 27th 2025

Get context in Java code run from C++

Scheduled Pinned Locked Moved Solved Mobile and Embedded
3 Posts 2 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cristeab
    wrote on last edited by
    #1

    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

    I 1 Reply Last reply
    0
    • C cristeab

      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

      I Offline
      I Offline
      Ibrahim
      wrote on last edited by
      #2

      @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());
      
      1 Reply Last reply
      1
      • C Offline
        C Offline
        cristeab
        wrote on last edited by
        #3

        thank you, it works

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved