简体   繁体   English

从jni返回并访问Java对象/类

[英]Return and access java object/class from jni

I have a Java method which returns a instance of object/class 'Readcommpacket'. 我有一个Java方法,该方法返回对象/类'Readcommpacket'的实例。 The call to the Java method is initiated from the NDK/JNI side(c++ -> java-> return-> java object-> c++). 从NDK / JNI端开始对Java方法的调用(c ++-> java-> return-> java object-> c ++)。 How do I complete this, I'm struggling with the proper JNI calls/signatures to complete this. 我如何完成此工作,我正努力使用适当的JNI调用/签名来完成此工作。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Java object/class used as a return. Java对象/类用作返回值。

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 JNI调用的Java方法

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

JNI/NDK c/c++ code JNI / NDK c / c ++代码

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 假设包名称为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) PS:如果您从C ++调用此方法,请确保Pro Guard不会混淆类名和方法名(如果使用它)


Run the command javap -s classname.class The output will have the descriptor that you can use as it is Including ";" 运行命令javap -s classname.class输出将包含您可以使用的描述符,因为它包括“;”。

For example: javap -s abc.class 例如: javap -s abc.class

Trying to return an instance of abc 尝试返回abc的实例

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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