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. Native android code with QT
Forum Updated to NodeBB v4.3 + New Features

Native android code with QT

Scheduled Pinned Locked Moved Solved Mobile and Embedded
16 Posts 3 Posters 3.3k Views 2 Watching
  • 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.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by
    #7

    Hm, looks like the static method call needs a jclass instead of string. Weird.

    Try this:

    jclass myClass = "com/amin/QtAndroidToastJava/QtAndroidToastJava";
    auto returnString = QAndroidJniObject::callStaticMethod <jstring>(
        myClass, "getWifiName", "(V)Ljava/lang/String;");
    

    (Z(:^

    1 Reply Last reply
    0
    • ashajgA Offline
      ashajgA Offline
      ashajg
      wrote on last edited by
      #8

      Hi sir @sierdzio
      What I did is in my cpp file I added this line

      QAndroidJniObject returnString = QAndroidJniObject::callStaticObjectMethod("org/qtproject/example/QtAndroidToastJava","getWifiName","(Landroid/content/Context)",QtAndroid::androidContext().object());
      

      As I am using getWifiName(Context context) function it requires context from cpp.
      What I did is I added "(Landroid/content/Context)",QtAndroid::androidContext().object()); to check if it works but it is throwing these errors:

      W System.err: java.lang.NoSuchMethodError: no static method "Lorg/qtproject/example/QtAndroidToastJava;.getWifiName(Landroid/content/Context)"
      W System.err: at org.qtproject.qt5.android.QtNative.startQtApplication(Native Method)
      W System.err: at org.qtproject.qt5.android.QtNative$6.run(QtNative.java:359)
      W System.err: at org.qtproject.qt5.android.QtThread$1.run(QtThread.java:61
      )

      I think I am making some mistake in this line "(Landroid/content/Context)",QtAndroid::androidContext().object());"

      how can I figure out what I should pass or where its getting wrong?

      sierdzioS 1 Reply Last reply
      0
      • ashajgA ashajg

        Hi sir @sierdzio
        What I did is in my cpp file I added this line

        QAndroidJniObject returnString = QAndroidJniObject::callStaticObjectMethod("org/qtproject/example/QtAndroidToastJava","getWifiName","(Landroid/content/Context)",QtAndroid::androidContext().object());
        

        As I am using getWifiName(Context context) function it requires context from cpp.
        What I did is I added "(Landroid/content/Context)",QtAndroid::androidContext().object()); to check if it works but it is throwing these errors:

        W System.err: java.lang.NoSuchMethodError: no static method "Lorg/qtproject/example/QtAndroidToastJava;.getWifiName(Landroid/content/Context)"
        W System.err: at org.qtproject.qt5.android.QtNative.startQtApplication(Native Method)
        W System.err: at org.qtproject.qt5.android.QtNative$6.run(QtNative.java:359)
        W System.err: at org.qtproject.qt5.android.QtThread$1.run(QtThread.java:61
        )

        I think I am making some mistake in this line "(Landroid/content/Context)",QtAndroid::androidContext().object());"

        how can I figure out what I should pass or where its getting wrong?

        sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #9

        @ashajg said in Native android code with QT:

        how can I figure out what I should pass or where its getting wrong?

        Only by guessing, unfortunately. JNI is not clever enough to point our errors exactly.

        "(Landroid/content/Context)" - no return type (your method returns a string).

        I did tell you to drop the context arg, by the way. But if you want to keep it - so be it.

        (Z(:^

        ashajgA 1 Reply Last reply
        0
        • ashajgA Offline
          ashajgA Offline
          ashajg
          wrote on last edited by ashajg
          #10

          ok Sir
          I ll also try this

          jclass myClass = "com/amin/QtAndroidToastJava/QtAndroidToastJava";
          auto returnString = QAndroidJniObject::callStaticMethod <jstring>(
          myClass, "getWifiName", "(V)Ljava/lang/String;");

          it is saying cannot initialize a variable with jclass..

          1 Reply Last reply
          0
          • sierdzioS sierdzio

            @ashajg said in Native android code with QT:

            how can I figure out what I should pass or where its getting wrong?

            Only by guessing, unfortunately. JNI is not clever enough to point our errors exactly.

            "(Landroid/content/Context)" - no return type (your method returns a string).

            I did tell you to drop the context arg, by the way. But if you want to keep it - so be it.

            ashajgA Offline
            ashajgA Offline
            ashajg
            wrote on last edited by ashajg
            #11

            @sierdzio

            you asked to drop context so should I just write the code like this

            public class QtAndroidToastJava extends QtActivity
            {
            public String getWifiName(){
                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();
                      }
                   }
                }
            

            its throwing error

            symbol: variable context
            location: class QtAndroidToastJava
            Note: Some input files use or override a deprecated API.
            if I remove context

            1 Reply Last reply
            0
            • ashajgA Offline
              ashajgA Offline
              ashajg
              wrote on last edited by
              #12

              Hi sir @sierdzio

              I got the solution :) by using this (Landroid/content/Context;)Ljava/lang/String; I got wifi name.

              1 Reply Last reply
              1
              • JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by JoeCFD
                #13

                I am trying to do the same thing. But
                auto returnString = QAndroidJniObject::callStaticMethod <jstring>(
                myClass, "getWifiName", "(V)Ljava/lang/String;");
                causes compiling error.
                undefined reference to `_jstring* QAndroidJniObject::callStaticMethod<_jstring*>(char const*, char const*, char const*, ...)
                QAndroidJniObject::callStaticMethod <jstring> is dropped? I tried int and func call works.

                I checked the doc of Qt5
                https://doc.qt.io/qt-5/qandroidjniobject.html
                // C++ code
                QAndroidJniObject stringNumber = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/TestClass",
                "fromNumber"
                "(I)Ljava/lang/String;",
                10);
                should be work. But the call in my app returns empty object.
                My Qt version: 5.15.2 and OS: Ubuntu 18.04

                JoeCFDJ 1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

                  I am trying to do the same thing. But
                  auto returnString = QAndroidJniObject::callStaticMethod <jstring>(
                  myClass, "getWifiName", "(V)Ljava/lang/String;");
                  causes compiling error.
                  undefined reference to `_jstring* QAndroidJniObject::callStaticMethod<_jstring*>(char const*, char const*, char const*, ...)
                  QAndroidJniObject::callStaticMethod <jstring> is dropped? I tried int and func call works.

                  I checked the doc of Qt5
                  https://doc.qt.io/qt-5/qandroidjniobject.html
                  // C++ code
                  QAndroidJniObject stringNumber = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/TestClass",
                  "fromNumber"
                  "(I)Ljava/lang/String;",
                  10);
                  should be work. But the call in my app returns empty object.
                  My Qt version: 5.15.2 and OS: Ubuntu 18.04

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #14

                  @JoeCFD It works now. In my Java code, String has to be created with new.

                  1 Reply Last reply
                  0
                  • JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #15

                    wifiInfo.getSSID() returns unknown SSID.

                    JoeCFDJ 1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      wifiInfo.getSSID() returns unknown SSID.

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by
                      #16

                      @JoeCFD set permissions for location and wifi state change in manifestfile. And change location permission to my app in the tablet settings. The problem is solved.

                      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