本示例使用的读卡器:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-21818769070.35.44005b43nb1q2h&id=562957272162
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp"
tools:context=".IdCardActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="?attr/colorPrimary"
app:navigationIcon="@drawable/baseline_arrow_back_ios_24"
app:titleTextColor="@color/white"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="0dp">
<TextView
android:id="@+id/TextViewlabelDispleft"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="返回"
android:textColor="@color/white"
android:textSize="16sp"
android:gravity="center"
android:onClick="retmain" />
<TextView
android:id="@+id/TextViewlabelDisp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="ID卡、HID卡测试页 "
android:textColor="@color/white"
android:textSize="16sp"
android:gravity="center_horizontal|right|center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/TextViewlabelDispleft"
app:layout_constraintTop_toTopOf="parent" />
</androidx.appcompat.widget.Toolbar>
<TextView
android:id="@+id/sample_text"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:padding="3dp"
android:text="操作结果"
android:textSize="12sp"
android:background="@drawable/shape4border"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<ScrollView
android:id="@+id/scrollViewIC"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintBottom_toTopOf="@+id/sample_text"
android:scrollbars="horizontal"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp" >
<Button
android:id="@+id/btnReadID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="3dp"
android:onClick="idr_read"
android:text="读卡"
app:layout_constraintStart_toStartOf="@+id/btnReadOnceID"
app:layout_constraintEnd_toEndOf="@+id/btnReadOnceID"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnReadOnceID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="idr_read_once"
android:text="仅读一次,重新取放卡才能读到第二次"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnReadID"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
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);
}
}