High level: C++ and JNI data transfer
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.Transfer data from C++ to Java JAVA C++
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.
Java Type | Description |
---|---|
boolean | unsigned 8 bits |
byte | signed 8 bits |
char | unsigned 16 bits |
short | signed 16 bits |
int | signed 32 bits |
long | signed 64 bits |
float | 32 bits |
double | 64 bits |
void | N/A |
java/lang/String | String class |
android/util/ArrayMap | Array map class |
For more details check official Java documentation: | java.lang (Java Platform SE 7 ) |
Note: Whenever the code inside the Java method manipulates the UI of the host application, the code must be executed from the UI thread.
JAVA
public class JAVA_CLASS
{
...
public static RETURNTYPE CPP_TO_JAVA( [PARAMS ...] )
{
CFxARESInstance.instance().runOnUiThread(new Runnable()
{
@Override
public void run()
{
}
});
}
...
}
1.1 Call a Java method from C++ (Simple Data) JAVA C++
The SDK ships a helper class CFxStaticInvocation that takes care about most of the JNI settings and can be used quite fast. The class CFxStaticInvocation constructor only requires the location of the Java class PATH_OF_JAVA_CLASS, the java class method name JAVA_METHOD and the Java method signature JAVA_METHOD_SIGNATURE. If every detail matches perfectly it is only necessary to call it using the void Call( bool bWithParameters = false, ... ); method of CFxStaticInvocation.
C++
CFxStaticInvocation obj( "PATH_OF_JAVA_CLASS", "JAVA_METHOD", "JAVA_METHOD_SIGNATURE" );
obj.Call( true, [PARAMS, ...] );
Specifying the Java class method signature is the only challenging part. Here is a list of the matching Java / JNI signature pairs.
Type Signature | Java Type |
---|---|
Z | boolean |
B | byte |
C | char |
S | short |
I | int |
J | long |
F | float |
D | double |
L fully-qualified-class ; | fully-qualified-class |
[ type | type[] |
( arg-types ) ret-type | method type |
For more details check official Java documentation: |
Sample (None executable code):
JAVA
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()
{
//
}
});
}
...
}
C++
For further samples look for any use of CFxStaticInvocation in the SDK Android projects.
1.2 Call a Java method from C++ (Complex Data) JAVA C++
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:
JAVA | C++ |
---|---|
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 JAVA C++
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:
JAVA | C++ |
---|---|
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.