简体   繁体   中英

Return and access java object/class from jni

I have a Java method which returns a instance of object/class 'Readcommpacket'. The call to the Java method is initiated from the NDK/JNI side(c++ -> java-> return-> java object-> c++). How do I complete this, I'm struggling with the proper JNI calls/signatures to complete this. Any help is greatly appreciated.

Java object/class used as a return.

public class ReadCommPacket {

    public byte[] RCPbtyeread;
    public int RCPbytecount;

    public ReadCommPacket(byte[] btyeread, int bytecount){
        this.RCPbtyeread = btyeread;
        this.RCPbytecount = bytecount;
    }
}

Java method called by JNI

public ReadCommPacket BTMsgToNDKComm(){
        if(bluetoothConnectedRunnable != null){
            return bluetoothConnectedRunnable.readBT();//returns ReadCommPacket obj
        }
        return new ReadCommPacket(null, -1);
}

JNI/NDK c/c++ code

void recvFromBlueTooth(char * recvdByte){

JniMethodInfo methodInfo;
if (! getMethodInfo(&methodInfo, "BTMsgToNDKComm", "V"))//what signature would I use?
{
    LOGD("Cannot find method!");
    return;
}

//retreive btye[] and int from Readcommpacket here. What JNI calls?

}

signature contains package name.

let's suppose package name is com.example

then signature in your example will be:

()Lcom/example/ReadCommPacket

PS if you call this method from c++, make sure class name & method name will not be obfuscated by pro guard (if you use it)


Run the command javap -s classname.class The output will have the descriptor that you can use as it is Including ";"

For example: javap -s abc.class

Trying to return an instance of abc

Public static com.getInstance();
    descriptor: **()Lcom;**

public int init(java.lang.String, java.lang.String);
    descriptor: **(Ljava/lang/String;Ljava/lang/String;)I**

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM