Wie übergibt man eine komplexe Struktur zwischen C und Java mit JNI unter Android NDK

Ich habe eine komplexe Verteilung in den C-code auf meinem Android-Anwendung, und ich möchte, um es in den Java-Seite.
Ich habe einige Forschung bei google und stackoverflow, also ich habe die java-Klasse von meinem C-Verteilung, aber jetzt, wie man es in Java.

Ich habe festgestellt, diese Informationen, die über einen pointer in der Klasse und verwenden Sie diese auf der C-Seite :

Get the field ID : (*env)->GetFieldID(...)
Get the pointer : (*env)->GetLongField(...)
Set the pointer : (*env)->SetLongField(...)

Aber ich verstehe nicht, wie es wirklich funktioniert ...

Oben, können Sie finden, was ich gemacht habe bis jetzt ... nicht so viel !
Auf der C-Seite :

ComplexStructure Java_com_main_MainActivity_listenUDP(JNIEnv* env, jclass clazz)
{
    int i,taille;
    ComplexStructure myStruct;    
    taille = -1;    
    taille = recvfrom(socket, &myStruct, sizeof(ComplexStructure ), 0, &rcvAddr, &sizeOfSock);
    if(taille != -1)
    {   
        return myStruct;
    }
    return NULL;
}

Und auf der Java-Seite :

public void getFromUDP() {

    ComplexClass myClass = new ComplexClass();
    myClass = listenUDP();              
}

@Override
public void run() {
    initUDP();
    getFromUDP();
}


public static native ComplexClass listenUDP();
public static native void initUDP();
public static native void closeUDP();

/** Load jni .so on initialization */
static {
     System.loadLibrary("native-interface");
}

EDIT : ich möchte hinzufügen, dass meine Struktur ist sehr Komplex, so :

typedef struct{
  TYPE_A myStructA;
  TYPE_B myStructB;
  TYPE_C myStructC;
  TYPE_D myStructD;
}ComplexStructure;

typedef struct{
  float rad;
  int size;
  bool isEmpty;
}TYPE_A;

typedef struct{
  float rad;
  bool isEmpty;
  float color;
  int temp;
}TYPE_B;

typedef struct{
  int temp;
  float rain;
  bool isEmpty;
}TYPE_C;

typedef struct{
  float rad;
  int idPerson;
  bool isOnTime;
}TYPE_D;

Sogar noch komplexer, nur ein Beispiel, um zu zeigen wie es ist !

InformationsquelleAutor Bibu | 2012-04-24
Schreibe einen Kommentar