本示例使用的发卡器:https://item.taobao.com/item.htm?spm=a1z10.5-c-s.w4002-21818769070.11.4391789eCLwm3t&id=615391857885
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.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import android.widget.ArrayAdapter;
import com.reader.ourmifare;
public class ISO15693Activity extends AppCompatActivity {
private TextView tv;
private Spinner spinner;
private ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iso15693);
androidx.appcompat.widget.Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tv = findViewById(R.id.sample_text);
tv.setText("操作结果");
}
@Override
public void onBackPressed(){
super.onBackPressed();
finish();
}
@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();
}
public void easyread15693(View view){
byte status; //存放返回值
byte myctrlword = 0; //读写控制字,一般取0 就行了
byte afi ; //卡片应用标识
byte startblock ; //指定读起始块地址,最大取值255(根据卡类型)
byte blocknum ; //指定读取块数,最大取值12
byte[] myuid = new byte[8]; //8字节卡序列号缓冲
byte[] mydatabuf = new byte[255]; //读卡数据缓冲共255个字节
tv.setText(" ");
TextView ctruid=findViewById(R.id.textDispUID);
ctruid.setText("");
EditText ctrrwdata=findViewById(R.id.editrwdata);
ctrrwdata.setText("");
EditText ctrafi=findViewById(R.id.textafi) ;
String afistr=ctrafi.getText().toString().trim();
if (afistr.length() < 2)
{
tv.setText("应用标识AFI是2位16进制数据,请输入正确的AFI!");
return;
}else {afi=(byte) Integer.parseInt(afistr, 16); }
EditText ctrbeginadd = findViewById(R.id.textRWbegin); //操作起始地址
startblock=(byte)Integer.parseInt(ctrbeginadd.getText().toString().trim());
EditText ctrrwblocks = findViewById(R.id.textRWblocks); //读写块数
blocknum=(byte)Integer.parseInt(ctrrwblocks.getText().toString().trim());
Spinner spls = findViewById(R.id.sp_seletagtype);
int tagtype =(int)(spls.getSelectedItemId()); //标签类型
if (tagtype==0) {
if (blocknum < 1 || blocknum > 12) {
tv.setText("一次最多读写12块数据,如要读写更多的数据请使用循环的方式读写。");
return;
}
}else{
if (blocknum != 1) {
tv.setText("富士通标签每次只能读写一块数据!");
return;
}
}
status = ourmifare.iso15693readex(myctrlword, afi, startblock, blocknum, myuid, mydatabuf) ;
if (status == 0) {
ourmifare.pcdbeep(38);
String serialnumber = "";
for (int i = 0; i < 8; i++) {
String bytestr = "00" + Integer.toHexString(myuid[i] & 0xFF);
serialnumber = serialnumber + bytestr.substring(bytestr.length() - 2, bytestr.length());
}
ctruid.setText(serialnumber);
EditText ctrnewuid=findViewById(R.id.edittextnewuid);
ctrnewuid.setText(serialnumber);
String databuf = "";
for (int i = 1; i < mydatabuf[0]+1; i++) {
String bytestr = "00" + Integer.toHexString(mydatabuf[i] & 0xFF);
databuf = databuf + bytestr.substring(bytestr.length() - 2, bytestr.length())+" ";
}
ctrrwdata.setText(databuf);
tv.setText("读取15693标签数据操作成功!");
} else {
PrintErrInf(status); //错误代码提示
}
}
public void easywrite15693(View view){
byte status; //存放返回值
byte myctrlword = 0; //读写控制字,一般取0 就行了
byte afi ; //卡片应用标识
byte startblock ; //指定读起始块地址,最大取值255(根据卡类型)
byte blocknum ; //指定读取块数,最大取值12
byte[] myuid = new byte[8]; //8字节卡序列号缓冲
byte[] mydatabuf = new byte[255]; //读卡数据缓冲共255个字节
byte DataLen;
tv.setText(" ");
TextView ctruid=findViewById(R.id.textDispUID);
ctruid.setText("");
EditText ctrafi=findViewById(R.id.textafi) ;
String afistr=ctrafi.getText().toString().trim();
if (afistr.length() < 2)
{
tv.setText("应用标识AFI是2位16进制数据,请输入正确的AFI!");
return;
}else {afi=(byte) Integer.parseInt(afistr, 16); }
EditText ctrbeginadd = findViewById(R.id.textRWbegin); //操作起始地址
startblock=(byte)Integer.parseInt(ctrbeginadd.getText().toString().trim());
EditText ctrrwblocks = findViewById(R.id.textRWblocks); //读写块数
blocknum=(byte)Integer.parseInt(ctrrwblocks.getText().toString().trim());
Spinner spls = findViewById(R.id.sp_seletagtype);
int tagtype =(int)(spls.getSelectedItemId()); //标签类型
if (tagtype==0) {
if (blocknum < 1 || blocknum > 12) {
tv.setText("一次最多读写12块数据,如要读写更多的数据请使用循环的方式读写。");
return;
}
DataLen=(byte)(blocknum*4);
}else {
if (blocknum != 1) {
tv.setText("富士通标签每次只能读写一块数据!");
return;
}
if (tagtype==1){
DataLen=(byte)(blocknum*8); //
} else{DataLen=(byte)(blocknum*32);}
}
EditText ctrrwdata=findViewById(R.id.editrwdata);
String rwdatahex=ctrrwdata.getText().toString().trim();
String[] strArr = rwdatahex.split("\\ "); /*分割接收到的数据后再分析、处理、返回指令 */
if (strArr.length options = new ArrayList<>();
options.clear();
adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,options);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ctrspuid.setAdapter(adapter);
EditText ctrtextafi=findViewById(R.id.textafi);
ctrtextafi.setText("00");
byte afi = 0; //卡片应用标识
EditText ctrtextflage=findViewById(R.id.edittextflag);
ctrtextflage.setText("16");
byte flags = (byte) 0x16; //操作卡片的标识字,详细说明参看文档
EditText ctrtextmasklen=findViewById(R.id.edittextmac);
ctrtextmasklen.setText("0");
byte masklen = 0; //掩码长度,有一张卡最大为40,一张卡为43,理论上为64
status = ourmifare.iso15693inventory16(flags, afi, masklen, maskuidbuf, revlen,myuid);
if (status == 0) {
ourmifare.pcdbeep(38);
int j=0;
String dispinf="";
String serialnumber = "";
while (j(this,android.R.layout.simple_spinner_dropdown_item,options);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ctrspuid.setAdapter(adapter);
EditText ctrnewuid=findViewById(R.id.edittextnewuid);
ctrnewuid.setText(serialnumber);
tv.setText("搜寻15693卡成功!\n\n"+dispinf);
} else {
PrintErrInf(status); //错误代码提示
}
}
public void read15693block(View view){
String uidstr="";
byte status=1; //存放返回值
byte startblock = 0; //指定读起始块地址,最大取值255(根据卡类型)
byte blocknum = 1; //指定读取块数,最大取值12
byte[] myuid = new byte[8]; //8字节卡序列号缓冲
byte[] revlen = new byte[1]; //返回读取数据长度
byte[] mydatabuf = new byte[255]; //读卡数据缓冲共255个字节
tv.setText(" ");
EditText ctrtextrwdata=findViewById(R.id.editrwblockdata);
ctrtextrwdata.setText("");
Spinner ctrspuid=findViewById(R.id.sp_seleuid);
if (ctrspuid.getSelectedItemId()<0){
tv.setText("请先搜寻卡片获取需读写卡的UID!");
return;
}else{
uidstr=ctrspuid.getSelectedItem().toString();
tv.setText(uidstr);
}
EditText ctrtextflage=findViewById(R.id.edittextflag);
ctrtextflage.setText("22");
byte flags = (byte) 0x22; //操作卡片的标识字,详细说明参看文档
EditText ctrbeginadd = findViewById(R.id.edittextblockbegin); //操作起始地址
startblock=(byte)Integer.parseInt(ctrbeginadd.getText().toString().trim());
EditText ctrrwblocks = findViewById(R.id.textreadwriteblocks); //读写块数
blocknum=(byte)Integer.parseInt(ctrrwblocks.getText().toString().trim());
for(int i=0;i<8;i++) {
myuid[i]=(byte)Integer.parseInt(uidstr.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
}
int j=0;
String readdatastr="";
while (j