1、c/c++和java之间的通信经常通过jni来实时传递参数,但是由于参数不固定或者参数类型很多需要一个合适的方法来传递。
2、这里有个实例,记录以备用,头文件
/*
* jni_tbox_observer.h
*
*/
#ifndef JNI_TBOX_OBSERVER_H_
#define JNI_TBOX_OBSERVER_H_
#include <map>
#include "jni.h"
#ifdef HAVE_ANDROID_OS
#include "JNIHelp.h"
#endif
#include "tbox_interfaces.h"
#include "common_head.h"
#include "tbox_status.h"
namespace dls
{
using std::string;
class jni_tbox_observer : public tbox_observer
{
public:
// void on_mic_forward_changed(const u8& forward);
// void on_telema_business_status(const u8& business_id, const u8& status);
// void on_tbox_key_event(const u8& key_code, const u8& action);
void on_tbox_communication_state(const u8& state);
void on_call_status(const u8& call_status);
void on_tbox_reply_gps(const char *gps_info);
void on_test_uart_result(const u8& result);
jni_tbox_observer(JavaVM *jvm, JNIEnv* env, jobject obj);
virtual ~jni_tbox_observer();
private:
JavaVM* _jvm;
JNIEnv* _construct_env;
jobject _corresponding_jobj;
jclass _corresponding_jclz;
std::map<int, JNIEnv*> _callback_thread_JNIEnvs;
pthread_mutex_t _JNIEnvs_lock;
tbox_status* _tbox_status;
JNIEnv* obtain_callback_thread_env();
void call_java_method(const char* method_name, const char* arg_format, ...);
};
} /* namespace dls */
#endif /* JNI_TBOX_OBSERVER_H_ */
3、实例
/*
* jni_tbox_observer.cpp
*
*/
#include <sys/syscall.h>
#include "jni_tbox_observer.h"
#undef LOG_TAG
#define LOG_TAG "TB_JNI_Observer"
namespace dls {
using std::map;
struct _gpsInfo {
u8 _gpsStatus;
int _gpsLongtitude;
int _gpsLatitude;
u16 _gpsSpeed;
u16 _gpsBearing;
u16 _gpsAltitude;
u32 _gpsTime;
u16 _gpsGeoDirection;
u8 _gpsHdop;
u8 _gpsVdop;
u8 _gpsCurTolSat;
u8 _gpsPosSat;
};
/** Represents SV information. */
struct GpsSvInfo {
/** Pseudo-random number for the SV. */
u8 prn;
/** Elevation of SV in degrees. */
u8 elevation;
/** Azimuth of SV in degrees. */
u16 azimuth;
/** Signal to noise ratio. */
u8 snr;
} ;
jni_tbox_observer::jni_tbox_observer(JavaVM *jvm, JNIEnv* env, jobject obj) :
_jvm(jvm), _construct_env(env), _corresponding_jobj(
env->NewGlobalRef(obj)), _corresponding_jclz(NULL) {
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE_NP);
pthread_mutex_init(&_JNIEnvs_lock, &mutex_attr);
jclass clz = env->GetObjectClass(_corresponding_jobj);
_corresponding_jclz = reinterpret_cast<jclass>(env->NewGlobalRef(clz));
_tbox_status = new tbox_status();
}
jni_tbox_observer::~jni_tbox_observer() {
_construct_env->DeleteGlobalRef(_corresponding_jobj);
_construct_env->DeleteGlobalRef(_corresponding_jclz);
for (map<int, JNIEnv*>::iterator it = _callback_thread_JNIEnvs.begin();
it != _callback_thread_JNIEnvs.end(); it++) {
//XXX How To detach current thread JNI environment???? OMG, I don't know, fortunately we needn't to do so.
}
_callback_thread_JNIEnvs.clear();
}
/*
void jni_tbox_observer::on_mic_forward_changed(const u8& forward) {
call_java_method("onMicForwardFromNative", "(I)V", forward);
}
void jni_tbox_observer::on_telema_business_status(const u8& business_id,
const u8& status) {
call_java_method("onTelematicBusinessStatusFromNative", "(II)V",
business_id, status);
}
void jni_tbox_observer::on_tbox_key_event(const u8& key_code,
const u8& action) {
call_java_method("onTBoxKeyEventFromNative", "(II)V", key_code, action);
}
*/
void jni_tbox_observer::on_tbox_communication_state(const u8& state) {
call_java_method("onTBoxCommunicationStat