JNIEXPORT jstring JNICALL Java_excute_readMsgFromMsgQ(JNIEnv *env, jobject, jint msgQKey) { char buff[2048]; mMsgQ mq(msgQKey); mq.recvMsg(buff); jstring result = stoJstring(env, (const char*)buff); return result; }
static jstring Operator_device_Cmd_String(JNIEnv *env ,jobject obj,jstring value,jint type ,jint cmd, jint val) { const char * mac = env -> GetStringUTFChars(value, 0); jstring result ; result = stoJstring(env, (device->operator_device_string(mac, type, cmd, val) ) ); return result; }
static jstring Read_device(JNIEnv *env ,jobject obj) { // JNIEnv *env; jstring result ; // ALOGI("from jason debug msg------------------>Now here in List_device!\n"); result = stoJstring(env, (device->read_device() ) ); return result; }
static jstring Write_device(JNIEnv *env ,jobject obj, jstring value, jint type, jint cmd, jint val) { // JNIEnv *env; jstring result ; const char* mac = env ->GetStringUTFChars(value,0) ; // ALOGI("from jason debug msg------------------>Now here in List_device!\n"); result = stoJstring(env, (device->write_device(mac, type, cmd, val) ) ); return result; }
// count is a static value, record the number of TOC items that remained after geting #times. // 100 items each time. JNIEXPORT jobjectArray JNICALL Java_com_pvi_ap_reader_external_pdf_pdfReader_getChapterList (JNIEnv *env, jobject obj, jint times ) { LOG_READER_DEBUG("in : %s", __func__); g_jniEnv = env; g_this = obj; if (reader==NULL) { LOG_ERROR("no pdf reader, init() firstly!\n"); return NULL; } const int max_obj = 100; list< toc_item > toc_list = reader->get_toc_list(); static int count = count = toc_list.size(); jobjectArray args = 0; //申明一个object数组 jsize len = 0; //数组大小 if (count>max_obj) { len = max_obj; count -= max_obj; } else { len = count; } if (toc_list.size()==0) { return args; } //获取object所属类,一般为ava/lang/Object就可以了 jclass objClass = (env)->FindClass("com/pvi/ap/reader/external/pdf/ChapterInfo"); //新建object数组 args = (env)->NewObjectArray(len, objClass, 0); if (args==NULL) { LOG_ERROR("can not new object array"); return args; } /**//* 下面为获取到Java中对应的实例类中的变量*/ //获取Java中的实例类 jclass objectClass = (env)->FindClass("com/pvi/ap/reader/external/pdf/ChapterInfo"); jmethodID methodId= env->GetMethodID(objectClass,"<init>","()V"); //获取类中每一个变量的定义 jfieldID titleId = (env)->GetFieldID(objectClass,"title","Ljava/lang/String;"); jfieldID positionId = (env)->GetFieldID(objectClass,"position","I"); jfieldID levelId= (env)->GetFieldID(objectClass,"level","I"); //给每一个实例的变量付值,并且将实例作为一个object,添加到objcet数组中 list<toc_item>::iterator it; int i; for (it = toc_list.begin(),i=0;i<max_obj*times;it++,i++) ; for(i=0; (it != toc_list.end()) && i<len; it++,i++ ) { jobject _obj= env->AllocObject(objectClass); // jobject _obj= env->NewObject(objectClass, methodId); //给每一个实例的变量付值 jstring jtitle = stoJstring(env, (*it).title); (env)->SetObjectField(_obj,titleId,jtitle); (env)->SetIntField(_obj,positionId,(*it).position); (env)->SetIntField(_obj,levelId,(*it).level); //添加到objcet数组中 (env)->SetObjectArrayElement(args, i, _obj); LOGD("%d: %s:", i, (*it).title); } LOG_READER_DEBUG("out: %s", __func__); //返回object数组 return args; }
void AddArg(JNIEnv *env, jobject args, yeguang::ValueObject& valueobj) { jclass clazz_arg = env->FindClass("com/yeguang/paramprotocol/ParamArg"); jmethodID mid_arg = NULL; jobject argobj = NULL; yeguang::emValueType type = valueobj.GetType(); switch(type) { case yeguang::BOOLVALUE: { mid_arg = env->GetMethodID(clazz_arg, "<init>", "(Z)V"); argobj = env->NewObject(clazz_arg, mid_arg, (jboolean)valueobj.GetBoolean()); }break; case yeguang::CHARVALUE: { mid_arg = env->GetMethodID(clazz_arg, "<init>", "(C)V"); argobj = env->NewObject(clazz_arg, mid_arg, (jchar)valueobj.GetChar()); }break; case yeguang::SHORTVALUE: { mid_arg = env->GetMethodID(clazz_arg, "<init>", "(S)V"); argobj = env->NewObject(clazz_arg, mid_arg, (jshort)valueobj.GetShort()); }break; case yeguang::INTVALUE: { mid_arg = env->GetMethodID(clazz_arg, "<init>", "(I)V"); argobj = env->NewObject(clazz_arg, mid_arg, (jint)valueobj.GetInt()); }break; case yeguang::LONGVALUE: { mid_arg = env->GetMethodID(clazz_arg, "<init>", "(J)V"); argobj = env->NewObject(clazz_arg, mid_arg, (jlong)valueobj.GetLong()); }break; case yeguang::FLOATVALUE: { mid_arg = env->GetMethodID(clazz_arg, "<init>", "(F)V"); argobj = env->NewObject(clazz_arg, mid_arg, (jfloat)valueobj.GetFloat()); }break; case yeguang::DOUBLEVALUE: { mid_arg = env->GetMethodID(clazz_arg, "<init>", "(D)V"); argobj = env->NewObject(clazz_arg, mid_arg, (jfloat)valueobj.GetDouble()); }break; case yeguang::STRINGVALUE: { mid_arg = env->GetMethodID(clazz_arg, "<init>", "(Ljava/lang/String;)V"); jstring strtmp = stoJstring(env, valueobj.GetString().c_str()); argobj = env->NewObject(clazz_arg, mid_arg, strtmp); }break; case yeguang::BYTEARRAY: { mid_arg = env->GetMethodID(clazz_arg, "<init>", "([B)V"); jbyteArray byte_array = env->NewByteArray(valueobj.GetByteLength()); env->SetByteArrayRegion(byte_array, 0, valueobj.GetByteLength(), (jbyte *)valueobj.GetByteArray().get()); argobj = env->NewObject(clazz_arg, mid_arg, byte_array); }break; default: break; } if(argobj != NULL) { jmethodID mid = GetMethod(env, "com/yeguang/paramprotocol/ParamArgs", "AddArgs", "(Lcom/yeguang/paramprotocol/ParamArg;)V"); env->CallVoidMethod(args, mid, argobj); } }