本示例使用的读卡器:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-21818769070.35.44005b43nb1q2h&id=562957272162
package com.usbreadertest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.reader.ouridr;
public class IdCardActivity extends AppCompatActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_id_card);
androidx.appcompat.widget.Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tv = findViewById(R.id.sample_text);
tv.setText("操作结果");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home){
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void retmain(View view)
{
finish();
}
//读ID卡
public void idr_read(View view)
{
byte status;//存放返回值
byte[] idserial = new byte[5];
String strls;
status = ouridr.read(idserial);
if(status == 0)
{
ouridr.beep(38);
strls = "读卡成功!16进制卡号为:";
String strls1 = "0"+Integer.toHexString(idserial[0]);
strls = strls + strls1.substring(strls1.length()-2) +"-";
strls1 = "0"+Integer.toHexString(idserial[1]);
strls = strls + strls1.substring(strls1.length()-2) +"-";
strls1 = "0"+Integer.toHexString(idserial[2]);
strls = strls + strls1.substring(strls1.length()-2) +"-";
strls1 = "0"+Integer.toHexString(idserial[3]);
strls = strls + strls1.substring(strls1.length()-2) +"-";
strls1 = "0"+Integer.toHexString(idserial[4]);
strls = strls + strls1.substring(strls1.length()-2);
long cardhao;
cardhao = idserial[1] & 0xff;
cardhao *= 256;
cardhao += idserial[2] & 0xff;
cardhao *= 256;
cardhao += idserial[3] & 0xff;
cardhao *= 256;
cardhao += idserial[4] & 0xff;
String card8h10d = "000000000"+Long.toString(cardhao);//0305887634 123B7992
card8h10d=card8h10d.substring(card8h10d.length()-10,card8h10d.length());
strls=strls+"\n转8H10D码:"+card8h10d;
String WG341="00000"+Integer.toString((idserial[3]& 0xff)*256+(idserial[4]& 0xff));
WG341=WG341.substring(WG341.length()-5,WG341.length());
String WG342="00000"+Integer.toString((idserial[1]& 0xff)*256+(idserial[2]& 0xff));
WG342=WG342.substring(WG342.length()-5,WG342.length());
strls=strls+"\n转韦根34码:"+WG341+WG342;
String WG261="000"+Integer.toString(idserial[2]& 0xff);
WG261=WG261.substring(WG261.length()-3,WG261.length());
String WG262="00000"+Integer.toString((idserial[3]& 0xff)*256+(idserial[4]& 0xff));
WG262=WG262.substring(WG262.length()-5,WG262.length());
strls=strls+"\n转韦根26码:"+WG261+WG262;
tv.setText(strls);
} else {
PrintErrInf(status);
}
}
//仅读一次ID卡,重新取放卡才能读到第二次
public void idr_read_once(View view)
{
byte status;//存放返回值
byte[] idserial = new byte[5];
String strls;
status = ouridr.readonce(idserial);
if(status == 0)
{
ouridr.beep(38);
strls = "读卡成功!16进制卡号为:";
String strls1 = "0"+Integer.toHexString(idserial[0]);
strls = strls + strls1.substring(strls1.length()-2) +"-";
strls1 = "0"+Integer.toHexString(idserial[1]);
strls = strls + strls1.substring(strls1.length()-2) +"-";
strls1 = "0"+Integer.toHexString(idserial[2]);
strls = strls + strls1.substring(strls1.length()-2) +"-";
strls1 = "0"+Integer.toHexString(idserial[3]);
strls = strls + strls1.substring(strls1.length()-2) +"-";
strls1 = "0"+Integer.toHexString(idserial[4]);
strls = strls + strls1.substring(strls1.length()-2);
long cardhao;
cardhao = idserial[1] & 0xff;
cardhao *= 256;
cardhao += idserial[2] & 0xff;
cardhao *= 256;
cardhao += idserial[3] & 0xff;
cardhao *= 256;
cardhao += idserial[4] & 0xff;
String card8h10d = "000000000"+Long.toString(cardhao);//0305887634 123B7992
card8h10d=card8h10d.substring(card8h10d.length()-10,card8h10d.length());
strls=strls+"\n转8H10D码:"+card8h10d;
String WG341="00000"+Integer.toString((idserial[3]& 0xff)*256+(idserial[4]& 0xff));
WG341=WG341.substring(WG341.length()-5,WG341.length());
String WG342="00000"+Integer.toString((idserial[1]& 0xff)*256+(idserial[2]& 0xff));
WG342=WG342.substring(WG342.length()-5,WG342.length());
strls=strls+"\n转韦根34码:"+WG341+WG342;
String WG261="000"+Integer.toString(idserial[2]& 0xff);
WG261=WG261.substring(WG261.length()-3,WG261.length());
String WG262="00000"+Integer.toString((idserial[3]& 0xff)*256+(idserial[4]& 0xff));
WG262=WG262.substring(WG262.length()-5,WG262.length());
strls=strls+"\n转韦根26码:"+WG261+WG262;
tv.setText(strls);
} else {
PrintErrInf(status);
}
}
public void PrintErrInf(byte errcode) {
String dispstr;
switch(errcode){
case 8:
dispstr="错误代码:8,未寻到卡,请重新将卡放在ID卡读卡器感应区!";
break;
case 22:
dispstr="错误代码:22,动态库或驱动程序异常!";
break;
case 23:
dispstr="错误代码:23,发卡器未连接!";
break;
case 24:
dispstr="错误代码:24,读卡器可能没有该功能!";
break;
default:
dispstr="未知错误,错误代码:"+Integer.toString(errcode);
break;
}
tv.setText(dispstr);
}
}