QtAndroidExtras - call Qt from Java



  • 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



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



  • 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....



  • 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



  • no ideas?



  • 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.



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



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





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


Log in to reply
 

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