package com.example.a7105demo;
public class a7105Class {
/*聲明函數*/
//初始化函數
public native int Init();
public native String stringFromJNI();
public native int Read(int buf[], int length);
public native int Write(int buf[], int length);
public native int CLOSE();
static{
System.loadLibrary("a7105");/*加載JNI庫*/
}
}
package com.example.a7105demo;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
a7105Class a7105_class;
private Button btn1 = null;
private Button btn2 = null;
private TextView mTextView1;
private EditText mEditText1;
public static String str1;
public static String str2;
private static final String TAG = "a7105_MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a7105_class = new a7105Class(); //聲明類
a7105_class.Init(); //調用JNI庫裏的初始化函數
btn1 = (Button)findViewById(R.id.a7105_read);
btn1.setOnClickListener(new Btn1Listener()); //捆綁監聽器
//按鈕
btn2 = (Button)findViewById(R.id.a7105_write);
btn2.setOnClickListener(new Btn2Listener()); //捆綁監聽器
mTextView1 = (TextView)findViewById(R.id.textView1);
mEditText1 = (EditText)findViewById(R.id.editText1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class Btn1Listener implements OnClickListener {
int[] buf = new int[8];
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//a7105_class.Init();
System.out.println("debug a7105_READ");
a7105_class.Read(buf, 8);
Log.d(TAG,
"buf0= " + Integer.toHexString(buf[0]) + " buf1= "
+ Integer.toHexString(buf[1]) + " buf2= "
+ Integer.toHexString(buf[2]) + " buf=3 "
+ Integer.toHexString(buf[3]));
str1 = Integer.toHexString(buf[0]);
for (int j = 1; j < 8; j++) {
str1 += Integer.toHexString(buf[j]);
}
mTextView1.setText(str1);
//a7105_class.CLOSE();
}
}
class Btn2Listener implements OnClickListener {
int[] intBuf = new int[8];
char[] byteBuf = new char[8];
int length;
@Override
public void onClick(View v) {
//a7105_class.Init();
str2 = mEditText1.getText().toString();
Log.d(TAG, "str2 = "+str2);
byteBuf = str2.toCharArray();
if(byteBuf.length < 8)
length = byteBuf.length;
if(byteBuf.length > 8)
length = 8;
for (int j = 0; j < length; j++) {
intBuf[j] = byteBuf[j] - '0';
Log.d(TAG, "intBuf = "+intBuf[j]+"byteBuf = "+byteBuf[j]);
}
System.out.println("debug a7105_WRITE");
a7105_class.Write(intBuf, length);
//a7105_class.CLOSE();
}
}
}
jni學習實例(三)-a7105模塊驅動之activity
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.