/
High level: C++ and JNI data transfer

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

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 )
java.util (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

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:

JNI Types and Data Structures


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++

JAVA

C++

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 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++

JAVA

C++

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.

Related content

Low level: C++ and JNI data transfer
Low level: C++ and JNI data transfer
More like this
Android: Java and C++ synergy
Android: Java and C++ synergy
More like this
Android Developer's Guide
Android Developer's Guide
More like this
Android: LISP (Unsupported)
Android: LISP (Unsupported)
More like this