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. QtAndroidExtras - call Qt from Java
QtWS25 Last Chance

QtAndroidExtras - call Qt from Java

Scheduled Pinned Locked Moved Mobile and Embedded
10 Posts 4 Posters 4.0k 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.
  • D Offline
    D Offline
    dualfaces
    wrote on last edited by
    #1

    Hi,

    i have a small test function which does the following:
    @
    QString tmp = "def";
    if (QAndroidJniObject::isClassAvailable("java/lang/String")) {
    tmp = "string";
    }
    if (QAndroidJniObject::isClassAvailable("org/qtproject/qt5/android/QtNative")) {
    tmp = "qt5";
    }
    if (QAndroidJniObject::isClassAvailable("com/somePackage/JniTester")) {
    tmp = "demo";
    }
    @
    The first two tests are successfully executed, but the third line crashes with a segmentation fault.

    The file JniTester located in android-sources/src/com/somePackage/JniTester.java looks like this:
    @
    package com.somePackage;

    public class JniTester extends org.qtproject.qt5.android.bindings.QtActivity {

    public static int test() {
        return 17;
    }
    

    }
    @

    The project file contains these lines next to the files:
    @
    ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources
    ANDROID_PACKAGE = com.somePackage
    ANDROID_MINIMUM_VERSION = 16
    ANDROID_TARGET_VERSION = 16
    ANDROID_APP_NAME = Qt Test

    TARGET = MyTest
    TEMPLATE = app
    @

    What do i have to add to find the class and call methods?

    Regards

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dualfaces
      wrote on last edited by
      #2

      Ok, now i removed the derivation and after several rebuilds and deleting of the build-directory the three tests are successful.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dualfaces
        wrote on last edited by
        #3

        but it keeps crashing if i add
        @
        if (QAndroidJniObject::isClassAvailable("android/bluetooth/BluetoothAdapter")) {
        tmp = "bluetooth";
        }
        @
        How can i specify an AndroidManifest file for my java file. I could imagine that missing permissions might be the problem. But i have no idea how to specify one.
        The documentation is very short and does not go into detail on these parts...
        Thank you

        edit: I test on an 4.1.2 device. But the same call works on an 4.2.2. That is very strange....

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dualfaces
          wrote on last edited by
          #4

          Ok,

          now I've made some progress. The difficulties caused by different android versions may stem from the bluetooth stack which was exchanged from 4.1 to 4.2.
          Now my problem is to supply data from the java class back to my qt app.
          Here is a code snippet
          @
          //Test.java
          package de.somePackage.Test;

          imports....

          public class Test {
          private native void processData(int x);
          int getSomething() {
          //here is a thread started which calls
          processData(23);
          }
          }

          //Mainwindow.cpp

          //constructor: QAndroidJniEnvironment env, QAndroidJniObject m_tobj

          JNINativeMethod methods[] {{"processData", "(I)V", reinterpret_cast<void*>(&MainWindow::processData)}};
          m_tobj = QAndroidJniObject("de/somePackage/Test");
          
          jclass objectClass = env->GetObjectClass(m_list.object<jobject>());
          env->RegisterNatives(objectClass, methods, sizeof(methods) / sizeof(methods[0]));
          env->DeleteLocalRef(objectClass);
          

          void MainWindow::someFunc() {
          jint k = m_tobj.callMethod("getSomething", "(V)I");
          }

          void MainWindow::processData(JNIEnv *env, jobject thiz, jint x) {
          Q_UNUSED(env)
          Q_UNUSED(thiz)
          int k = x;
          QString mytmp = "dudu";
          }
          @
          getSomething() is executed, also processData is called, but the argument does not contain the 23, just some memory garbage (although its the same number each time).
          But what changes each call is the pointer to env, what is really strange.

          Any ideas why the parameter is not correclty supplied?

          Regards

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dualfaces
            wrote on last edited by
            #5

            no ideas?

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dualfaces
              wrote on last edited by
              #6

              ok, made a little step.
              If i declare processData static everythink works. Now i just need to figure out how to use non-static functions.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                maxvanceffer
                wrote on last edited by
                #7

                Post here solution pls, if u will find the siolution )))

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  TioRoy
                  wrote on last edited by
                  #8

                  You need to pass a function pointer from an instance of the MainWindow.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    ahmad88me
                    wrote on last edited by
                    #9

                    I found this http://doc-snapshot.qt-project.org/qt5-stable/qtandroidextras-notification-example.html

                    but I couldn't make it work

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      TioRoy
                      wrote on last edited by
                      #10

                      The example show how to call Java from Qt. And do you need to call Qt from Java, right?

                      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