• JNI 从C文件向Java文件传递多个参数


    JNI C主函数

      1 #include <jni.h>
      2 #include <string.h>
      3 #include <android/log.h>
      4 #include "Provider.h"
      5 #include "libCold_chain.h"
      6 
      7 JNIEnv* jniEnv;
      8 
      9 jint
     10 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     return openSerialDevice(devName_id);
     18 }
     19 
     20 jint
     21 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         return dataTest;
     62     }
     63 
     64     return -1;
     65 }
     66 
     67 jint
     68 Java_com_panasonic_communicationtest_StateConfirmFragment_closeSerialDevice( JNIEnv* env, jobject thiz, jint devFd )
     69 {
     70     if(jniEnv == NULL) {
     71         jniEnv = env;
     72     }
     73 
     74     return closeSerialDevice(devFd);
     75 }
     76 
     77 //  ===========================================
     78 jint
     79 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     return openSerialDevice(devName_id);
     87 }
     88 
     89 jint
     90 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 jint
    103 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 jint
    116 Java_com_panasonic_communicationtest_ParameterSettingFragment_closeSerialDevice( JNIEnv* env, jobject thiz, jint devFd )
    117 {
    118     if(jniEnv == NULL) {
    119         jniEnv = env;
    120     }
    121 
    122     return closeSerialDevice(devFd);
    123 }
    124 
    125 
    126 
    127 //--------------------------------------
    128 jstring
    129 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 jstring
    141 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     return getString(address_id);
    148 }
    149 
    150 jstring
    151 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     return setString(address_id);
    158 }

    JNI C代码

     1 extern long readSerialData(long devFd, unsigned char unitno, unsigned short addr, long *data, long timeout, unsigned char* rcmdReq, unsigned char* rcmdRes)
     2 {
     3     /*
     4      * Test by fansen
     5      */
     6 
     7     *data = 1;
     8     int m = 0;
     9     int n = 0;
    10     unsigned char test[10] = "0123456789";    //{0xff,0x1f,0x07,0x08};
    11     strcpy(rcmdReq, test);
    12 
    13 //    rcmdReq = test;
    14 //    for(m = 0; m <= 8; m++){
    15 //        rcmdReq[m] = test[m];
    16 //        LOGI("test1: %c", test[m]);
    17 //    }
    18 
    19     LOGI(rcmdReq);
    20 
    21 
    22 //    char test2[10] = {0xff,0x1f,0x07,0x08,0x00,0x87,0x00,0x01};
    23 //    __android_log_print(ANDROID_LOG_INFO, "ComTest", "Communication Test 2");
    24 //    for(n = 0; n <= 9; n++){
    25 //        rcmdReq[n] = test2[n];
    26 //    }
    27 
    28     return OK;
    29 
    30     /*
    31      * Test over
    32      */
  • 相关阅读:
    CSS边框(圆角、阴影、背景图片)
    CSS3浏览器兼容
    HTML5全局属性
    HTLM5新增属性
    HTML5标签
    如何开始使用bootstrap
    重新了解Java基础(三)-运行机制&HelloWorld
    重新了解Java基础(二)-Java的特性
    Java关键字之native
    重新了解Java基础(一)
  • 原文地址:https://www.cnblogs.com/fansen/p/4838686.html
Copyright © 2020-2023  润新知