安卓JNI传结构体

安卓层代码:
[java]  view plain copy print ?
  1. package com.demouhf.uhfjni;  
  2.   
  3. import android.util.Log;  
  4. class Foo {  
  5.     protected int len;  
  6.     protected String name;  
  7. }  
  8. public class Demouhf {  
  9.     static {  
  10.         try {  
  11.             Log.i("JNI""wqf Trying to load libnhf.so");  
  12.             /* 調用gsensor.so */  
  13.             System.loadLibrary("jni_nhf");   
  14.         }  
  15.         catch (UnsatisfiedLinkError ule) {  
  16.             Log.e("JNI""wqf WARNING: Could not load libnhf.so");  
  17.         }}  
  18.        
  19.       
  20.     //public static native int as3992_get_version(Reader_Version_Info *readerinfo);  
  21.     public static native int openport(String port,int baud,char parity, int databits, int stopbits);  
  22.     public static native int closeport();  
  23.     public static native String getversion();  
  24.     public static native String inventory();  
  25.     public static native int select(int[] epc);  
  26.     public static native int stopinventory();  
  27.   
  28.     //public static native int openport();  
  29.   
  30.     public static native int open();  
  31.     public static native int foo(Foo fooObj);  
  32. }  
[java]  view plain copy print ?
  1. package com.demouhf.uhfjni;  
  2.   
  3. import java.text.DecimalFormat;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.hardware.Sensor;  
  8. import android.hardware.SensorEvent;  
  9. import android.hardware.SensorEventListener;  
  10. import android.hardware.SensorManager;  
  11. import android.os.Bundle;  
  12. import android.text.util.Linkify;  
  13. import android.view.View;  
  14. import android.widget.Button;  
  15. import android.widget.EditText;  
  16. import android.widget.LinearLayout;  
  17. import android.widget.TextView;  
  18. import android.widget.Toast;  
  19. import android.util.Log;  
  20. import android.os.Handler;   
  21.   
  22.   
  23.   
  24. public class UHFActivity extends Activity {  
  25.     /** Called when the activity is first created. */  
  26.     private Button open_device;  
  27.     private Button getvision;  
  28.     private Button onceseeklabel;  
  29.     private Button stopseeklabel;  
  30.     private Button moreseeklabel;  
  31.     private Button selectepc;  
  32.     private EditText selectedit;  
  33.     private TextView text_show;  
  34.   
  35.       
  36.     public  int fd = 0;  
  37.     public  static int flag=0;  
  38.     public  static int flag_time=0;   
  39.   
  40.     Handler handler=new Handler();  
  41.     Runnable runnable=new Runnable(){  
  42.     @Override  
  43.     public void run() {  
  44.     // TODO Auto-generated method stub  
  45.     //要做的事情  
  46.         Log.i("wqfrunnable""-----------------time--------------------\n");  
  47.         String text=null;  
  48.         text = Demouhf.inventory();  
  49.         if(text!=null)text_show.setText(text);  
  50.         else text_show.setText("NULL");  
  51.         handler.postDelayed(this1000);  
  52.     }  
  53.     };  
  54.   
  55.     //打开设备  
  56.     private Button.OnClickListener open_listener= new Button.OnClickListener(){  
  57.      public void onClick(View v)  
  58.      {  
  59.         if(flag==0){  
  60.                 fd = Demouhf.openport("/dev/ttyS0",115200,'N',8,1);  
  61.                 Toast.makeText(getApplicationContext(), "Demouhf.openport",Toast.LENGTH_SHORT).show();  
  62.                 if(fd>0){  
  63.                     setTitle("Connect  device success! ");  
  64.                     Log.i("BUTTON""Connect  device sucdddcess!");  
  65.                     open_device.setText("Close");  
  66.                     flag=1;  
  67.                 }else{  
  68.                     setTitle("Connect  device false! ");  
  69.                     Log.i("BUTTON""Connect  device false!");  
  70.                 }  
  71.             }else{  
  72.                 Demouhf.closeport();  
  73.                 open_device.setText("Open");  
  74.                 flag=0;  
  75.                 Toast.makeText(getApplicationContext(), "Demouhf.closeport",Toast.LENGTH_SHORT).show();  
  76.                 setTitle("Close  device success! ");  
  77.                 Log.i("BUTTON""Close  device success!");  
  78.             }  
  79.       }  
  80.         
  81. };  
  82.     //获取版本号  
  83.     private Button.OnClickListener getvision_listener= new Button.OnClickListener(){  
  84.      public void onClick(View v)  
  85.      {  
  86.             if(flag==0){text_show.setText("please open the series first! ");}else{  
  87.             String text="wqf";  
  88.             Log.i("JNI""wqf getvision_listener");  
  89.             text = Demouhf.getversion();  
  90.             if(text!=null)text_show.setText(text);  
  91.             else text_show.setText("NULL");  
  92.             setTitle("getvision_listener! ");  
  93.             }  
  94.      }  
  95. };  
  96.     //单次查询标签  
  97.     private Button.OnClickListener onceseeklabel_listener= new Button.OnClickListener(){  
  98.      public void onClick(View v)  
  99.      {  
  100.             Foo fooe = new Foo();  
  101.             Demouhf.foo(fooe);  
  102.             Log.i("JNI""wqf ---2323423-----------------------------------\n");  
  103.             System.out.println(fooe.name);  
  104.             System.out.println(fooe.len);  
  105.         /*  if(flag==0){text_show.setText("please open the series first! ");}else{ 
  106.             String text="wqf"; 
  107.             Log.i("JNI", "wqf seeklabel_listener"); 
  108.             text = Demouhf.inventory(); 
  109.             if(text!=null)text_show.setText(text); 
  110.             else text_show.setText("NULL"); 
  111.             setTitle("seeklabel_listener! "); 
  112.             }*/  
  113.      }  
  114. };  
  115.     //多次查询标签  
  116.     private Button.OnClickListener moreseeklabel_listener= new Button.OnClickListener(){  
  117.      public void onClick(View v)  
  118.      {  
  119.             if(flag==0){text_show.setText("please open the series first! ");}else{  
  120.             Log.i("JNI""wqf moreseeklabel_listener");  
  121.             if(flag_time==0){  
  122.                 handler.postDelayed(runnable, 1000);//每两秒执行一次runnable.  
  123.                 moreseeklabel.setText("StopMoreSeekLabel");  
  124.                 flag_time=1;  
  125.             }else{  
  126.                 handler.removeCallbacks(runnable);//停止定时器  
  127.                 moreseeklabel.setText("StartMoreSeekLabel");  
  128.                 flag_time=0;  
  129.             }  
  130.             setTitle("moreseeklabel_listener! ");  
  131.             }  
  132.      }  
  133. };  
  134.     //停止查询标签  
  135.     private Button.OnClickListener stopseeklabel_listener= new Button.OnClickListener(){  
  136.      public void onClick(View v)  
  137.      {  
  138.             if(flag==0){text_show.setText("please open the series first! ");}else{  
  139.             int k;  
  140.             k = Demouhf.stopinventory();  
  141.             if(k>0)text_show.setText("Stop Seek Label success!");  
  142.             else text_show.setText("Stop Seek Label fail!");  
  143.             setTitle("stopseeklabel_listener! ");  
  144.             }  
  145.      }  
  146. };  
  147.     //选中标签  
  148.     private Button.OnClickListener selectepc_listener= new Button.OnClickListener(){  
  149.      public void onClick(View v)  
  150.      {  
  151.             if(flag==0){text_show.setText("please open the series first! ");}else{  
  152.             String text="wqf";  
  153.             int sFlag=0;  
  154.             int[] strTab={0xE20x000x930x660x660x170x000x360x210x400x360xBD};  
  155.             //strTab =  new int[12]={0xE2, 0x00, 0x93, 0x66, 0x66, 0x17, 0x00, 0x36, 0x21, 0x40, 0x36, 0xBD};  
  156.             Log.i("JNI""wqf selectepc_listener");  
  157.             //sFlag = Demouhf.select(strTab);  
  158.             if(sFlag>0)text_show.setText("Selectepc success!");  
  159.             else text_show.setText("Selectepc fail!");  
  160.             setTitle("selectepc_listener! ");  
  161.             }         
  162.         /*   
  163.             int[] strTab ;   
  164.             strTab = new int[12] ;  
  165.             String str; 
  166.             str=selectedit.getText().toString(); 
  167.             text_show.setText(str); 
  168.             */  
  169.      }  
  170. };  
  171.   
  172.     @Override  
  173.     protected void onResume()  
  174.     {  
  175.      
  176.         super.onResume();  
  177.     }  
  178.   
  179.     @Override  
  180.     protected void onPause()  
  181.     {  
  182.       super.onPause();  
  183.     }  
  184.     @Override  
  185.     public void onCreate(Bundle savedInstanceState) {  
  186.         super.onCreate(savedInstanceState);  
  187.         setContentView(R.layout.main);  
  188.   
  189.         open_device =(Button) findViewById(R.id.button_open);  
  190.         getvision=(Button) findViewById(R.id.getvision);  
  191.         onceseeklabel=(Button) findViewById(R.id.onceseeklabel);   
  192.         stopseeklabel=(Button)findViewById(R.id.stopseeklabel);  
  193.         moreseeklabel=(Button)findViewById(R.id.moreseeklabel);  
  194.         selectepc=(Button)findViewById(R.id.selectepc);  
  195.         selectedit=(EditText)findViewById(R.id.select_edit);  
  196.         text_show=(TextView)findViewById(R.id.text_show);  
  197.          /*使用setOnClickListener來監聽事件*/  
  198.         open_device.setOnClickListener(open_listener);  
  199.         getvision.setOnClickListener(getvision_listener);  
  200.         onceseeklabel.setOnClickListener(onceseeklabel_listener);   
  201.         stopseeklabel.setOnClickListener(stopseeklabel_listener);   
  202.         moreseeklabel.setOnClickListener(moreseeklabel_listener);  
  203.         selectepc.setOnClickListener(selectepc_listener);  
  204.           
  205.         text_show.setText("show text");  
  206.   
  207.     }  
  208. }  

C代码:

  1. JNIEXPORT jint JNICALL Java_com_demouhf_uhfjni_Demouhf_foo  
  2. (JNIEnv *env, jclass mc, jobject fooObj)  
  3. {  
  4.     Foo_t * bar = malloc(sizeof(Foo_t));  
  5.     jclass clazz;  
  6.     jfieldID fid;  
  7.   
  8.     //init the bar data of C  
  9.     strcpy(bar->name, "Yachun Miao");  
  10.     bar->len = strlen(bar->name);  
  11.   
  12.     // mapping bar of C to foo  
  13.     clazz = (*env)->GetObjectClass(env, fooObj);  
  14.     if (0 == clazz) {  
  15.         printf("GetObjectClass returned 0\n");  
  16.         return (-1);  
  17.     }  
  18.     fid = (*env)->GetFieldID(env, clazz, "len""I");  
  19.     (*env)->SetLongField(env, fooObj, fid, bar->len);  
  20.   
  21.     fid = (*env)->GetFieldID(env, clazz, "name""Ljava/lang/String;");  
  22.     jstring name = (*env)->NewStringUTF(env, bar->name);  
  23.     (*env)->SetObjectField(env, fooObj, fid, name);  
  24.   
  25.     free(bar);  
  26.     return 0;  
  27. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值