4

Í 有需要返回一个 jbyte 数组的 JNI 函数。该数组包含带有数据的 ac struct 的字节数据。但是当我将数据从 unsinged char 数组传递到 jbyte 数组时出现错误。

C结构定义如下:

// ObjectInfo struct definition
 struct ObjectInfo {
    int ObjectXCor;
    int ObjectYCor;
    int ObjectMass;
 };

 // ObjectInfo struct definition
 struct SensorDataStruct{
    int PingData;
    int IRData;
    int ForceData;
    int CompassData;
 };

 // ObjectInfo struct definition
 union PackedSend{
    struct CommStruct{
        ObjectInfo VisionData;
        SensorDataStruct SensorData;
    } CommData;
    unsigned char bytes[28];
 }SendData;

JNI方法定义如下:

JNIEXPORT jbyteArray JNICALL Java_com_example_communicationmodule_MainActivity_Convert(
    JNIEnv *env, jobject,
    jint var1,
    jint var2,
    jint var3,
    jint var4,
    jint var5,
    jint var6,
    jint var7)
{
// Array to fill with data
jbyteArray Array;

// Init  java byte array
Array = env->NewByteArray(28);

SendData.CommData.SensorData.PingData = var1;
SendData.CommData.SensorData.IRData = var2;
SendData.CommData.SensorData.ForceData = var3;
SendData.CommData.SensorData.CompassData = var4;
SendData.CommData.VisionData.ObjectXCor = var5;
SendData.CommData.VisionData.ObjectYCor = var6;
SendData.CommData.VisionData.ObjectMass = var7;

//Put the native unsigned chars in the java byte array
for(int Index=0; Index < 28; Index++){
    Array[Index] = SendData.bytes[Index];

}


// Return java array
return Array;
}

我得到的错误是:jni/HelperFunctions.cpp:44:38: error: no match for 'operator=' in '*(Array + ((unsigned int)Index)) = SendData.PackedSend::bytes[Index] '

我的问题是将原生 unsigned char 数组转换为 jbyte 数组的正确方法是什么?有没有任何男孩有建议?欢迎所有提示!

更新

转换为 (jbyte) 后出现以下错误: jni/HelperFunctions.cpp:54:46: error: no match for 'operator=' in '*(Array + ((unsigned int)Index)) = (jbyte) SendData.PackedSend::bytes[Index]' jni/HelperFunctions.cpp:54:46: 注意:候选是:C:/android-ndk-r9/platforms/android-8/arch-arm/usr/include/jni。 h:66:7: 注意:_jbyteArray& _jbyteArray::operator=(const _jbyteArray&)

铸造代码如下:

//Put the native unsigned chars in the java byte array
for(int Index=0; Index < 28; Index++){
    Array[Index] = (jbyte) SendData.bytes[Index];
}

有人有想法或建议吗?欢迎一切!

4

1 回答 1

17
env->SetByteArrayRegion(Array, 0, 28, (jbyte*)SendData.bytes)
于 2013-10-31T11:58:15.003 回答