Mainactity.java
package com.topeet.adctest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
/* add by cym 20140618 */
adc adc = new adc();
private Button read;
//String rxIdCode = "";
private EditText ET1;
private int[] RfidBuffer=new int[20];
private int RfidRxCount=0;
/* end add */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* add by cym 20140618 */
read = (Button)findViewById(R.id.button1);
ET1 = (EditText)findViewById(R.id.tv1);
adc.Open();
read.setOnClickListener(new manager());
/* end add */
}
class manager implements OnClickListener{
public void onClick(View v) {
String rxIdCode = "";
String str;
int i;
switch (v.getId()) {
case R.id.button1:
int[] RX= adc.Read();
if(RX != null)
{
System.arraycopy(RX, 0, RfidBuffer, RfidRxCount, RX.length);
}
for(i=0; i<5; i++)
{
str = Integer.toHexString(RfidBuffer[i]);
rxIdCode += str.substring(1);
}
ET1.setText(rxIdCode);
break;
}
}
}
@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;
}
/* add by cym 20140617 */
static {
System.loadLibrary("adc");
}
/* end add */
}
JNI
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdint.h>
#include <termios.h>
#include <android/log.h>
#include <sys/ioctl.h>
#include "com_topeet_adctest_adc.h"
#undef TCSAFLUSH
#define TCSAFLUSH TCSETSF
#ifndef _TERMIOS_H_
#define _TERMIOS_H_
#endif
int fd=0;
/*
* Class: com_topeet_adctest_adc
* Method: Open
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_topeet_adctest_adc_Open
(JNIEnv *env, jobject obj)
{
if(fd<=0)fd = open("/dev/adc", O_RDWR|O_NDELAY|O_NOCTTY);
if(fd <=0 )__android_log_print(ANDROID_LOG_INFO, "serial", "open /dev/adc Error");
else __android_log_print(ANDROID_LOG_INFO, "serial", "open /dev/adc Sucess fd=%d",fd);
}
/*
* Class: com_topeet_adctest_adc
* Method: Close
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_topeet_adctest_adc_Close
(JNIEnv *env, jobject obj)
{
if(fd > 0)close(fd);
}
/*
* Class: com_topeet_adctest_adc
* Method: Ioctl
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_topeet_adctest_adc_Ioctl
(JNIEnv *env, jobject obj, jint num, jint en)
{
ioctl(fd, en, num);
}
/*
* Class: com_topeet_adctest_adc
* Method: Read
* Signature: ()[I
*/
JNIEXPORT jintArray JNICALL Java_com_topeet_adctest_adc_Read
(JNIEnv *env, jobject obj)
{
unsigned char buffer[512];
int BufToJava[512];
int len=0, i=0;
memset(buffer,0,sizeof(buffer));
memset(BufToJava,0,sizeof(BufToJava));
len=read(fd,buffer,512);
if(len<=0)return NULL;
for(i=0;i<len;i++)
{
printf("%x", buffer[i]);
BufToJava[i] = buffer[i];
}
jintArray array = (*env)-> NewIntArray(env, len);
(*env)->SetIntArrayRegion(env,array, 0, len, BufToJava);
return array;
}