最近在开发一款Android平台的设备,需要使用到RFID模块,自己写了一个测试程序。模块既可通过串口和USB接口与上位机通讯,具体操作指令根据通信协议来定。程序中用到了两个共享库:一个是友善之臂提供的friendlyarm-hardware.so,另一个是自己为了控制RFID模块的电源写的JNI编译生成库。
package com.example.rfid;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.friendlyarm.AndroidSDK.HardwareControler;
import com.hardware.Hardware;
public class MainActivity extends Activity {
byte[] buf= new byte[100];
TextView productInf;
TextView cardInf;
Button connectBtn;
Button resetBtn;
Button clearBtn;
Button closeBtn;
Button requestBtn;
Button selectBtn;
Button authkey3Btn;
Button continueFindBtn;
String choosed_serial = "/dev/ttyUSB0";
int choosed_buad = 38400;
int fd;
int len;
Hardware hardware=new Hardware();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
productInf = (TextView)findViewById(R.id.productinf);
cardInf = (TextView)findViewById(R.id.cardinf);
connectBtn = (Button)findViewById(R.id.connect);
connectBtn.setText("连接");
resetBtn = (Button)findViewById(R.id.reset);
resetBtn.setText("重启");
clearBtn = (Button)findViewById(R.id.clear);
clearBtn.setText("清空");
closeBtn = (Button)findViewById(R.id.close);
closeBtn.setText("关闭");
requestBtn = (Button)findViewById(R.id.request);
requestBtn.setText("寻卡");
selectBtn = (Button)findViewById(R.id.select);
selectBtn.setText("防冲突");
authkey3Btn = (Button)findViewById(R.id.authkey3);
authkey3Btn.setText("认证");
continueFindBtn = (Button)findViewById(R.id.continuefind);
continueFindBtn.setText("连续寻卡");
System.out.println("openGPIO:"+hardware.openGPIO());
System.out.println("setGPIO:"+hardware.setGPIO(0, 5));
connectBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
fd = HardwareControler.openSerialPort(choosed_serial,choosed_buad, 8, 1);//打开串口
System.out.println(">>>>>>>>>>>>"+fd);
if (fd != -1) {
Toast.makeText(getApplicationContext(), "成功打开串口:"+choosed_serial, 1).show();
} else {
Toast.makeText(getApplicationContext(),"无法打开串口", 1).show();
}
byte[] readVersion = new byte[]{(byte)0x55,(byte)0x00,(byte)0x4f,(byte)0x00,(byte)0x4f};//读硬件版本号
len =HardwareControler.write(fd,readVersion);
System.out.println("readVersion len:"+len);
if(len<1){
System.out.println("write error");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (HardwareControler.select(fd,3, 0)==1)
{
len=HardwareControler.read(fd,buf,30);
System.out.println(len);
if(len<1){
System.out.println("read error");
}
byte[] temp = new byte[16];
for(int i=0;i<16;i++)
{
temp[i] = buf[i+4];
}
productInf.append(bytesToHexString(temp)+"\n");
System.out.println(bytesToHexString(buf)+"\n");
}
byte[] readPSN = new byte[]{(byte)0x55,(byte)0x00,(byte)0x2f,(byte)0x01,(byte)0x14,(byte)0x3a};//读产品序列号
len =HardwareControler.write(fd,readPSN);
System.out.println("readVersion len:"+len);
if(len<1){
System.out.println("write error");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (HardwareControler.select(fd,3, 0)==1)
{
len=HardwareControler.read(fd,buf,30);
System.out.println(len);
if(len<1){
System.out.println("read error");
}
byte[] temp = new byte[14];
for(int i=0;i<14;i++)
{
temp[i] = buf[i+4];
}
productInf.append(bytesToHexString(temp)+"\n");
System.out.println(bytesToHexString(buf)+"\n");
}
}
});
resetBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
byte[] reset = new byte[]{(byte)0x55,(byte)0x00,(byte)0x37,(byte)0x00,(byte)0x37};//重启模块
len =HardwareControler.write(fd,reset);
System.out.println("readVersion len:"+len);
if(len<1){
System.out.println("write error");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (HardwareControler.select(fd,3, 0)==1)
{
len=HardwareControler.read(fd,buf,5);
System.out.println(len);
if(len<1){
System.out.println("read error");
}
if(buf[2]==(byte)0x00 && buf[0]==(byte)0xaa)
{
cardInf.append("reset success\n");
}
else
{
cardInf.append("reset failure\n");
}
System.out.println(bytesToHexString(buf)+"\n");
}
}
});
clearBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
cardInf.setText(null);
}
});
closeBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
HardwareControler.close(fd);
hardware.setGPIO(1, 5);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
});
requestBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
request();
}
});
selectBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
selectCard();
}
});
continueFindBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
new continueFindThread().start();
}
});
authkey3Btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
authKey3();
}
});
}
public void request()//寻卡操作
{
byte[] request = new byte[]{(byte)0x55,(byte)0x00,(byte)0x41,(byte)0x01,(byte)0x00,(byte)0x40};
len =HardwareControler.write(fd,request);
System.out.println("readVersion len:"+len);
if(len<1){
System.out.println("write error");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(bytesToHexString(request));
if (HardwareControler.select(fd,3, 0)==1)
{
len=HardwareControler.read(fd,buf,7);
System.out.println(len);
if(len<1){
System.out.println("read error");
}
}
if(buf[2]==(byte)0x00 && buf[0]==(byte)0xaa)
{
cardInf.append("find card success\n");
}
else
{
cardInf.append("find card failure\n");
}
System.out.println(bytesToHexString(buf));
}
public void selectCard()//防冲突、选卡操作
{
byte[] anticoll = new byte[]{(byte)0x55,(byte)0x00,(byte)0x42,(byte)0x04,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x46};
len =HardwareControler.write(fd,anticoll);
System.out.println("anticoll len:"+len);
if(len<1){
System.out.println("write error");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (HardwareControler.select(fd,3, 0)==1)
{
len=HardwareControler.read(fd,buf,9);
System.out.println(len);
if(len<1){
System.out.println("read error");
}
}
System.out.println(bytesToHexString(buf)+"\n");
if(buf[2]==(byte)0x00 && buf[0]==(byte)0xaa)
{
cardInf.append("Anticoll success\n");
byte[] cardNum = new byte[]{buf[4],buf[5],buf[6],buf[7]};
byte[] select = new byte[]{(byte)0x55,(byte)0x00,(byte)0x43,(byte)0x04,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x47};
for(int i=0;i<4;i++)
{
select[i+4] = cardNum[i];
select[8] ^= cardNum[i];
}
cardInf.append(bytesToHexString(select)+"\n");
len =HardwareControler.write(fd,select);
System.out.println("select len:"+len);
if(len<1){
System.out.println("write error");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (HardwareControler.select(fd,3, 0)==1)
{
len=HardwareControler.read(fd,buf,6);
System.out.println(len);
if(len<1){
System.out.println("read error");
}
}
if(buf[2]==(byte)0x00 && buf[0]==(byte)0xaa)
{
cardInf.append("select success\n");
cardInf.append("Card Id:"+bytesToHexString(cardNum)+"\n");
}
else
{
cardInf.append("select failure\n");
}
}
else
{
cardInf.append("Anticoll failure\n");
}
}
public void authKey3()//认证操作
{
byte[] authkey3 = new byte[]{(byte)0x55,(byte)0x00,(byte)0x54,(byte)0x08,(byte)0x00,(byte)0x03,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0x5f};
len =HardwareControler.write(fd,authkey3);
System.out.println("authkey3 len:"+len);
if(len<1){
System.out.println("write error");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (HardwareControler.select(fd,3, 0)==1)
{
len=HardwareControler.read(fd,buf,5);
System.out.println(len);
if(len<1){
System.out.println("read error");
}
}
if(buf[2]==(byte)0x00 && buf[0]==(byte)0xaa)
{
cardInf.append("authkey3 card success\n");
}
else
{
cardInf.append("authkey3 card failure\n");
}
}
//把字节数组转换成十六进制字符串
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
class continueFindThread extends Thread//
{
public void run()
{
System.out.println("Thread run0\n");
request();
selectCard();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@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;
}
}