Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In some special cases it is necessary to transfer simple or even complex data to the host application. For this purpose it is fairly enough to only work with static methods. The ARES Touch SDK provides a handful helper classes to avoid working directly with the JNI interfaces and Java environment. The utility class is called FxJavaUtils and provides a large set of classes. These are the steps to address a simple data transfer between the host app and the C++ app part and vice versa.

...

1.1 Create a Java class and add a public static method

public static RETURNTYPE CPP_TO_JAVA( PARAMS );

The RETURNTYPE as well as the PARAMS must be declared with Java known data types. The method name CPP_TO_JAVA does not need to contain characters like _ , may be just CPPTOJAVA.

...

Code Block
public class Geometry
{
...
public static void PointAdded( final double x , final double y , final double z )
{
    CFxARESInstance.instance().runOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
            //
        }
    });
}
...
}

...

Code Block
CFxStaticInvocation invocation( "com/domain/Geometry", "PointAdded", "(DDDDD)V" );
invocation.Call( true, 10056.2343, 100.67 , 0.0 );


For further samples look for any use of CFxStaticInvocation in the SDK Android projects.

1.2 Call a Java method from C++ (Complex Data)

Status
titleJAVA
Status
colourYellow
titleC++
Coming soon …

In some cases it is necessary to process strings or even more complex container. Besides CFxStaticInvocation there are also further helper classes like CFxJavaString and CFxJavaObjectArray. The table down below emphasizes the use of a simple string processing and an arrays of strings:

Status
titleJAVA

Status
colourYellow
titleC++

public class Database{

public static void AddName( String id , String name );

}

CFxJavaString key( L"0" );
CFxJavaString value( L"John Doe" );

CFxStaticInvocation invocation( “com/domain/Database“ , “AddName” , “(Ljava/lang/String;Ljava/lang/String;)V” );

invocation.Call( true, key.GetJString() , value.GetJString() );

public class Database{

public static void SetNames( String[] names );

}

jclass stringClass = GET_JAVA()->FindClass( "java/lang/String" );
CFxJavaObjectArray names( length, stringClass );

CFxJavaString name( “John Doe“ );
names.SetElement( 0, name.GetStrongJString() );

CFxStaticInvocation invocation( “com/domain/Database“ , “SetNames” , “(Ljava/lang/String;)V” );

invocation.Call( true, names.GetJObject() );

Note: When an object is passed as parameter of a method back to Java, it is only necessary to use methods like GetJString() or GetJObject(). For further samples look for any use of CFxStaticInvocation in the SDK Android projects.


1.3 Return values back from C++ to Java

Status
titleJAVA
Status
colourYellow
titleC++

The Java native interface also supports to directly return back values from the native method being called. This is always helpful to avoid processing data multiple times with a CFxStaticInvocation. The table down below emphasizes the use of a simple string processing and an arrays of strings as return value:

Status
titleJAVA

Status
colourYellow
titleC++

...

public class Database{
public static native String GetName( int id );

}

String name = Database.GetName(0);

JNIEXPORT jstring JNICALL Java_com_domain_Database_GetName( JNIEnv * env, jclass clazz, jint id )
{

CFxJavaString name( L"John Doe" );
return name.GetStrongJString();
}

public class Database{
public static native String[] GetNames();

}

String[] names = Database.GetNames();

JNIEXPORT jobjectarray JNICALL Java_com_domain_Database_GetNames( JNIEnv * env, jclass clazz )
{
jclass stringClass = GET_JAVA()->FindClass( "java/lang/String" );
CFxJavaObjectArray names( layouts.size(), stringClass );

CFxJavaString name( “John Doe“ );
names.SetElement( 0, name.GetStrongJString() );

return reinterpret_cast<jobjectArray >( names.GetStrongJObject() );
}

Note: If an object is passed as return value back to Java, it is always necessary to use methods like GetStrongJString() or GetStrongJObject() else there might runtime errors occur. These methods warrant the life time of an object even when the C++ method is being taken from call stack. For further samples look for any use of CFxStaticInvocation and CFxObjectInvocation in the SDK Android projects.