package com.demouhf.uhfjni; import android.util.Log; class Foo { protected int len; protected String name; } public class Demouhf { static { try { Log.i("JNI", "wqf Trying to load libnhf.so"); /* 調用gsensor.so */ System.loadLibrary("jni_nhf"); } catch (UnsatisfiedLinkError ule) { Log.e("JNI", "wqf WARNING: Could not load libnhf.so"); }} //public static native int as3992_get_version(Reader_Version_Info *readerinfo); public static native int openport(String port,int baud,char parity, int databits, int stopbits); public static native int closeport(); public static native String getversion(); public static native String inventory(); public static native int select(int[] epc); public static native int stopinventory(); //public static native int openport(); public static native int open(); public static native int foo(Foo fooObj); }
package com.demouhf.uhfjni; import java.text.DecimalFormat; import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.text.util.Linkify; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import android.util.Log; import android.os.Handler; public class UHFActivity extends Activity { /** Called when the activity is first created. */ private Button open_device; private Button getvision; private Button onceseeklabel; private Button stopseeklabel; private Button moreseeklabel; private Button selectepc; private EditText selectedit; private TextView text_show; public int fd = 0; public static int flag=0; public static int flag_time=0; Handler handler=new Handler(); Runnable runnable=new Runnable(){ @Override public void run() { // TODO Auto-generated method stub //要做的事情 Log.i("wqfrunnable", "-----------------time--------------------\n"); String text=null; text = Demouhf.inventory(); if(text!=null)text_show.setText(text); else text_show.setText("NULL"); handler.postDelayed(this, 1000); } }; //打开设备 private Button.OnClickListener open_listener= new Button.OnClickListener(){ public void onClick(View v) { if(flag==0){ fd = Demouhf.openport("/dev/ttyS0",115200,'N',8,1); Toast.makeText(getApplicationContext(), "Demouhf.openport",Toast.LENGTH_SHORT).show(); if(fd>0){ setTitle("Connect device success! "); Log.i("BUTTON", "Connect device sucdddcess!"); open_device.setText("Close"); flag=1; }else{ setTitle("Connect device false! "); Log.i("BUTTON", "Connect device false!"); } }else{ Demouhf.closeport(); open_device.setText("Open"); flag=0; Toast.makeText(getApplicationContext(), "Demouhf.closeport",Toast.LENGTH_SHORT).show(); setTitle("Close device success! "); Log.i("BUTTON", "Close device success!"); } } }; //获取版本号 private Button.OnClickListener getvision_listener= new Button.OnClickListener(){ public void onClick(View v) { if(flag==0){text_show.setText("please open the series first! ");}else{ String text="wqf"; Log.i("JNI", "wqf getvision_listener"); text = Demouhf.getversion(); if(text!=null)text_show.setText(text); else text_show.setText("NULL"); setTitle("getvision_listener! "); } } }; //单次查询标签 private Button.OnClickListener onceseeklabel_listener= new Button.OnClickListener(){ public void onClick(View v) { Foo fooe = new Foo(); Demouhf.foo(fooe); Log.i("JNI", "wqf ---2323423-----------------------------------\n"); System.out.println(fooe.name); System.out.println(fooe.len); /* if(flag==0){text_show.setText("please open the series first! ");}else{ String text="wqf"; Log.i("JNI", "wqf seeklabel_listener"); text = Demouhf.inventory(); if(text!=null)text_show.setText(text); else text_show.setText("NULL"); setTitle("seeklabel_listener! "); }*/ } }; //多次查询标签 private Button.OnClickListener moreseeklabel_listener= new Button.OnClickListener(){ public void onClick(View v) { if(flag==0){text_show.setText("please open the series first! ");}else{ Log.i("JNI", "wqf moreseeklabel_listener"); if(flag_time==0){ handler.postDelayed(runnable, 1000);//每两秒执行一次runnable. moreseeklabel.setText("StopMoreSeekLabel"); flag_time=1; }else{ handler.removeCallbacks(runnable);//停止定时器 moreseeklabel.setText("StartMoreSeekLabel"); flag_time=0; } setTitle("moreseeklabel_listener! "); } } }; //停止查询标签 private Button.OnClickListener stopseeklabel_listener= new Button.OnClickListener(){ public void onClick(View v) { if(flag==0){text_show.setText("please open the series first! ");}else{ int k; k = Demouhf.stopinventory(); if(k>0)text_show.setText("Stop Seek Label success!"); else text_show.setText("Stop Seek Label fail!"); setTitle("stopseeklabel_listener! "); } } }; //选中标签 private Button.OnClickListener selectepc_listener= new Button.OnClickListener(){ public void onClick(View v) { if(flag==0){text_show.setText("please open the series first! ");}else{ String text="wqf"; int sFlag=0; int[] strTab={0xE2, 0x00, 0x93, 0x66, 0x66, 0x17, 0x00, 0x36, 0x21, 0x40, 0x36, 0xBD}; //strTab = new int[12]={0xE2, 0x00, 0x93, 0x66, 0x66, 0x17, 0x00, 0x36, 0x21, 0x40, 0x36, 0xBD}; Log.i("JNI", "wqf selectepc_listener"); //sFlag = Demouhf.select(strTab); if(sFlag>0)text_show.setText("Selectepc success!"); else text_show.setText("Selectepc fail!"); setTitle("selectepc_listener! "); } /* int[] strTab ; strTab = new int[12] ; String str; str=selectedit.getText().toString(); text_show.setText(str); */ } }; @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); open_device =(Button) findViewById(R.id.button_open); getvision=(Button) findViewById(R.id.getvision); onceseeklabel=(Button) findViewById(R.id.onceseeklabel); stopseeklabel=(Button)findViewById(R.id.stopseeklabel); moreseeklabel=(Button)findViewById(R.id.moreseeklabel); selectepc=(Button)findViewById(R.id.selectepc); selectedit=(EditText)findViewById(R.id.select_edit); text_show=(TextView)findViewById(R.id.text_show); /*使用setOnClickListener來監聽事件*/ open_device.setOnClickListener(open_listener); getvision.setOnClickListener(getvision_listener); onceseeklabel.setOnClickListener(onceseeklabel_listener); stopseeklabel.setOnClickListener(stopseeklabel_listener); moreseeklabel.setOnClickListener(moreseeklabel_listener); selectepc.setOnClickListener(selectepc_listener); text_show.setText("show text"); } }
C代码:
JNIEXPORT jint JNICALL Java_com_demouhf_uhfjni_Demouhf_foo (JNIEnv *env, jclass mc, jobject fooObj) { Foo_t * bar = malloc(sizeof(Foo_t)); jclass clazz; jfieldID fid; //init the bar data of C strcpy(bar->name, "Yachun Miao"); bar->len = strlen(bar->name); // mapping bar of C to foo clazz = (*env)->GetObjectClass(env, fooObj); if (0 == clazz) { printf("GetObjectClass returned 0\n"); return (-1); } fid = (*env)->GetFieldID(env, clazz, "len", "I"); (*env)->SetLongField(env, fooObj, fid, bar->len); fid = (*env)->GetFieldID(env, clazz, "name", "Ljava/lang/String;"); jstring name = (*env)->NewStringUTF(env, bar->name); (*env)->SetObjectField(env, fooObj, fid, name); free(bar); return 0; }