jni 从c传递map到java_JNI 从C文件向Java文件传递多个参数

1 #include

2 #include

3 #include

4 #include "Provider.h"

5 #include "libCold_chain.h"

6

7 JNIEnv*jniEnv;8

9 jint10 Java_com_panasonic_communicationtest_StateConfirmFragment_openSerialDevice( JNIEnv*env, jobject thiz, jstring devName )11 {12 const char* devName_id = (*env)->GetStringUTFChars(env, devName, NULL);13 if(jniEnv ==NULL) {14 jniEnv =env;15 }16

17 returnopenSerialDevice(devName_id);18 }19

20 jint21 Java_com_panasonic_communicationtest_StateConfirmFragment_readSerialData( JNIEnv*env, jobject thiz )22 {23 long dataTest = 0;24

25 if(jniEnv ==NULL) {26 jniEnv =env;27 }28

29 jclass class;30 jfieldID send_instruction_record;31 jfieldID receive_instruction_record;32 class = (*env)->GetObjectClass(env, thiz);33 if (class ==NULL)34 {35 return -1;36 }37 send_instruction_record = (*env)->GetFieldID(env, class, "send_instruction_str", "Ljava/lang/String;");38 receive_instruction_record = (*env)->GetFieldID(env, class, "receive_instruction_str", "Ljava/lang/String;");39 if (send_instruction_record == NULL || receive_instruction_record ==NULL)40 {41 return -1;42 }43

44 unsigned char* rcmdReq = malloc(9*sizeof(jchar));45 unsigned char* rcmdRes = malloc(10*sizeof(jchar));46

47 if(readSerialData(1, 1, 80, &dataTest, 500, rcmdReq, rcmdRes) == 0){48

49 unsigned char* str1 = "hello fan sen!";50 unsigned char* str2 =rcmdReq;51

52 jstring jstr1 = (*env)->NewStringUTF(env, str1);53 jstring jstr2 = (*env)->NewStringUTF(env, str2);54 __android_log_print(ANDROID_LOG_INFO, "ComTest", rcmdReq);55 if(jstr1 == NULL || jstr2 ==NULL){56 return 2;57 }58 (*env)->SetObjectField(env, thiz, send_instruction_record, jstr1);59 (*env)->SetObjectField(env, thiz, receive_instruction_record, jstr2);60

61 returndataTest;62 }63

64 return -1;65 }66

67 jint68 Java_com_panasonic_communicationtest_StateConfirmFragment_closeSerialDevice( JNIEnv*env, jobject thiz, jint devFd )69 {70 if(jniEnv ==NULL) {71 jniEnv =env;72 }73

74 returncloseSerialDevice(devFd);75 }76

77 //===========================================

78 jint79 Java_com_panasonic_communicationtest_ParameterSettingFragment_openSerialDevice( JNIEnv*env, jobject thiz, jstring devName )80 {81 const char* devName_id = (*env)->GetStringUTFChars(env, devName, NULL);82 if(jniEnv ==NULL) {83 jniEnv =env;84 }85

86 returnopenSerialDevice(devName_id);87 }88

89 jint90 Java_com_panasonic_communicationtest_ParameterSettingFragment_readSerialData( JNIEnv*env, jobject thiz, jint devFd, jchar unitno, jshort addr, jlong data )91 {92 if(jniEnv ==NULL) {93 jniEnv =env;94 }95

96 unsigned char rcmdReq[10];97 unsigned char rcmdRes[10];98

99 return readSerialData(devFd, unitno, addr, data, 500, rcmdReq[10], rcmdRes[10]);100 }101

102 jint103 Java_com_panasonic_communicationtest_ParameterSettingFragment_writeSerialData( JNIEnv*env, jobject thiz, jint devFd, jchar unitno, jshort addr, jlong data )104 {105 if(jniEnv ==NULL) {106 jniEnv =env;107 }108

109 unsigned char rcmdReq[10];110 unsigned char rcmdRes[10];111

112 return writeSerialData(devFd, unitno, addr, data, 500, rcmdReq[10], rcmdRes[10]);113 }114

115 jint116 Java_com_panasonic_communicationtest_ParameterSettingFragment_closeSerialDevice( JNIEnv*env, jobject thiz, jint devFd )117 {118 if(jniEnv ==NULL) {119 jniEnv =env;120 }121

122 returncloseSerialDevice(devFd);123 }124

125

126

127 //--------------------------------------

128 jstring129 Java_com_panasonic_communicationtest_StateConfirmFragment_getStringFromJNI( JNIEnv*env, jobject thiz, jstring address )130 {131 const char* address_id = (*env)->GetStringUTFChars(env, address, NULL);132 if(jniEnv ==NULL) {133 jniEnv =env;134 }135

136 //return getString(address_id);

137 return (*env)->NewStringUTF(env, address_id);138 }139

140 jstring141 Java_com_panasonic_communicationtest_ParameterSettingFragment_getStringFromJNI( JNIEnv*env, jobject thiz, jstring address )142 {143 const char* address_id = (*env)->GetStringUTFChars(env, address, NULL);144 if(jniEnv ==NULL) {145 jniEnv =env;146 }147 returngetString(address_id);148 }149

150 jstring151 Java_com_panasonic_communicationtest_ParameterSettingFragment_setStringIntoJNI( JNIEnv*env, jobject thiz, jstring address )152 {153 const char* address_id = (*env)->GetStringUTFChars(env, address, NULL);154 if(jniEnv ==NULL) {155 jniEnv =env;156 }157 returnsetString(address_id);158 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值