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 | ||
---|---|---|
|
Status | ||||
---|---|---|---|---|
|
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:
|
| ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
public class Database{ | CFxJavaString key( L"0" ); CFxStaticInvocation invocation( “com/domain/Database“ , “AddName” , “(Ljava/lang/String;Ljava/lang/String;)V” ); invocation.Call( true, key.GetJString() , value.GetJString() ); | ||||||||||
public class Database{ | jclass stringClass = GET_JAVA()->FindClass( "java/lang/String" ); 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 | ||
---|---|---|
|
Status | ||||
---|---|---|---|---|
|
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:
|
|
---|
...
public class Database{ | JNIEXPORT jstring JNICALL Java_com_domain_Database_GetName( JNIEnv * env, jclass clazz, jint id ) | |
public class Database{ String[] names = Database.GetNames(); | JNIEXPORT jobjectarray JNICALL Java_com_domain_Database_GetNames( JNIEnv * env, jclass clazz ) |
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.