我正在开发一个本机模块,允许在java中使用RPI sense hat,而我的本机将无法编译: 目前我只是想把数据作为字符串返回,如果你有一个更简单的方法,请告诉我。 这是源文件(SenseHat.cpp):
#include <jni.h>
#include <stdio.h>
#include "SenseHat.h"
#include "RTIMULib.h"
RTIMU *imu;
RTPressure *pressure;
RTHumidity *humidity;
JNIEXPORT void JNICALL Java_SenseHat_init(JNIEnv *env, jobject thisObj) {
RTIMUSettings *settings = new RTIMUSettings("RTIMULib");
imu = RTIMU::createIMU(settings);
pressure = RTPressure::createPressure(settings);
humidity = RTHumidity::createHumidity(settings);
if ((imu == NULL) || (imu->IMUType() == RTIMU_TYPE_NULL)) {
printf("No IMU found\n");
exit(1);
}
// This is an opportunity to manually override any settings before the call IMUInit
// set up IMU
imu->IMUInit();
// this is a convenient place to change fusion parameters
imu->setSlerpPower(0.02);
imu->setGyroEnable(true);
imu->setAccelEnable(true);
imu->setCompassEnable(true);
// set up pressure sensor
if (pressure != NULL)
pressure->pressureInit();
// set up humidity sensor
if (humidity != NULL)
humidity->humidityInit();
}
JNIEXPORT jstring JNICALL Java_SenseHat_getTemperature(JNIEnv *env, jobject thisObj) {
RTIMU_DATA imuData = imu->getIMUData();
if (humidity != NULL)
humidity->humidityRead(imuData);
return ((*env)->NewStringUTF(env,imuData.temperature));
}
JNIEXPORT jstring JNICALL Java_SenseHat_getHumidity(JNIEnv *env, jobject thisObj) {
RTIMU_DATA imuData = imu->getIMUData();
if (humidity != NULL)
humidity->humidityRead(imuData);
return ((*env)->NewStringUTF(env,imuData.humidity));
}
JNIEXPORT jstring JNICALL Java_SenseHat_getOrientation(JNIEnv *env, jobject thisObj) {
RTIMU_DATA imuData = imu->getIMUData();
return ((*env)->NewStringUTF(env,imuData.fusionPose));
}
完整错误是:
SenseHat.cpp: In function ‘_jstring* Java_SenseHat_getTemperature(JNIEnv*, jobject)’:
SenseHat.cpp:53:18: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
return ((*env)->NewStringUTF(env,imuData.temperature));
^
SenseHat.cpp: In function ‘_jstring* Java_SenseHat_getHumidity(JNIEnv*, jobject)’:
SenseHat.cpp:60:18: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
return ((*env)->NewStringUTF(env,imuData.humidity));
^
SenseHat.cpp: In function ‘_jstring* Java_SenseHat_getOrientation(JNIEnv*, jobject)’:
SenseHat.cpp:66:18: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
return ((*env)->NewStringUTF(env,imuData.fusionPose));
^
编译代码:
gcc -I /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/include/ -I /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/include/linux/ -shared -o libSenseHat.so SenseHat.cpp
答案 0 :(得分:1)
JNIEnv
声明略有不同,有关详细信息,请参阅jni.h
。用C中的几个词来表示我们使用
(*env)->NewStringUTF(env,imuData.temperature);
在C ++中它应该是
env->NewStringUTF(imuData.temperature);
如您所见,C ++版本不需要env
取消引用并将其作为第一个参数传递给JNI函数。