安卓Android Studioy读写NXP ICODE2 15693标签源码

2024-01-09 11:38:18

?本示例使用的发卡器:https://item.taobao.com/item.htm?spm=a1z10.5-c-s.w4002-21818769070.11.4391789eCLwm3t&id=615391857885

<?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=".ISO15693Activity">

    <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="15693卡、hid-iclass卡物理卡号测试页"
            android:textColor="@color/white"
            android:gravity="center_horizontal|bottom"
            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"
        android:scrollbars="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/sample_text"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="3dp">

            <TextView
                android:id="@+id/labeltextseletag"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="选择15693标签类型:"
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <Spinner
                android:id="@+id/sp_seletagtype"
                android:layout_width="290dp"
                android:layout_height="20dp"
                android:entries="@array/Tagtype15693"
                android:theme="@style/my_spinner_style"
                app:layout_constraintBottom_toBottomOf="@+id/labeltextseletag"
                app:layout_constraintLeft_toRightOf="@+id/labeltextseletag"
                app:layout_constraintTop_toTopOf="@+id/labeltextseletag" />


            <Button
                android:id="@+id/butt_EasyRead15693Card"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:onClick="easyread15693"
                android:text="轻松读15693卡"
                android:textSize="11sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/labeltextseletag" />

            <Button
                android:id="@+id/butt_EasyWrite15693Card"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="easywrite15693"
                android:text="轻松写15693卡"
                android:textSize="11sp"
                app:layout_constraintBottom_toBottomOf="@+id/butt_EasyRead15693Card"
                app:layout_constraintLeft_toRightOf="@+id/butt_EasyRead15693Card"
                app:layout_constraintTop_toTopOf="@+id/butt_EasyRead15693Card" />

            <TextView
                android:id="@+id/labeltextUID"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="2dp"
                android:layout_marginTop="6dp"
                android:text="UID:"
                android:textSize="12sp"
                app:layout_constraintLeft_toRightOf="@+id/butt_EasyWrite15693Card"
                app:layout_constraintTop_toTopOf="@+id/butt_EasyWrite15693Card" />

            <TextView
                android:id="@+id/labeltextnote4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="2dp"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="6dp"
                android:textColor="#FF0000"
                android:text="读写时感应区内只能有一张卡!"
                android:textSize="10sp"
                app:layout_constraintBottom_toBottomOf="@+id/butt_EasyWrite15693Card"
                app:layout_constraintLeft_toRightOf="@+id/butt_EasyWrite15693Card"
                app:layout_constraintTop_toBottomOf="@+id/labeltextUID" />


            <TextView
                android:id="@+id/textDispUID"
                android:layout_width="110dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="2dp"
                android:background="@drawable/shape4border"
                android:gravity="center"
                android:textColor="#FF0000"
                android:text="                "
                android:textSize="11sp"
                app:layout_constraintBottom_toBottomOf="@+id/labeltextUID"
                app:layout_constraintLeft_toRightOf="@+id/labeltextUID"
                app:layout_constraintTop_toTopOf="@+id/labeltextUID" />

            <TextView
                android:id="@+id/labeltextnote1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="卡片应用标识AFI:"
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/butt_EasyRead15693Card" />

            <EditText
                android:id="@+id/textafi"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/shape4border"
                android:gravity="center"
                android:text="00"
                android:textColor="#FF0000"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/labeltextnote1"
                app:layout_constraintLeft_toRightOf="@+id/labeltextnote1"
                app:layout_constraintTop_toTopOf="@+id/labeltextnote1" />

            <TextView
                android:id="@+id/labeltextnote2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="18dp"
                android:text="本次读写起始块:"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/textafi"
                app:layout_constraintLeft_toRightOf="@+id/textafi"
                app:layout_constraintTop_toTopOf="@+id/textafi" />

            <EditText
                android:id="@+id/textRWbegin"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/shape4border"
                android:gravity="center"
                android:text="0"
                android:textColor="#FF0000"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/labeltextnote2"
                app:layout_constraintLeft_toRightOf="@+id/labeltextnote2"
                app:layout_constraintTop_toTopOf="@+id/labeltextnote2" />

            <TextView
                android:id="@+id/labeltextnote3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="18dp"
                android:text="读写块数:"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/textRWbegin"
                app:layout_constraintLeft_toRightOf="@+id/textRWbegin"
                app:layout_constraintTop_toTopOf="@+id/textRWbegin" />

            <EditText
                android:id="@+id/textRWblocks"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/shape4border"
                android:gravity="center"
                android:text="12"
                android:textColor="#FF0000"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/labeltextnote3"
                app:layout_constraintLeft_toRightOf="@+id/labeltextnote3"
                app:layout_constraintTop_toTopOf="@+id/labeltextnote3" />

            <EditText
                android:id="@+id/editrwdata"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/shape4border"
                android:digits="0123456789ABCDEFabcdef "
                android:inputType="textCapCharacters"
                android:maxLines="8"
                android:gravity="left"
                android:text="                                               "
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/labeltextnote1" />

            <Button
                android:id="@+id/butt_Request15693Card"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:text="搜索感应区内多张15693卡"
                android:textSize="11sp"
                android:onClick="Request15693card"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/editrwdata" />

            <TextView
                android:id="@+id/labeltextflag"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:text="操作标识:"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/butt_Request15693Card"
                app:layout_constraintLeft_toRightOf="@+id/butt_Request15693Card"
                app:layout_constraintTop_toTopOf="@+id/butt_Request15693Card" />

            <EditText
                android:id="@+id/edittextflag"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/shape4border"
                android:gravity="center"
                android:text="00"
                android:textColor="#FF0000"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/labeltextflag"
                app:layout_constraintLeft_toRightOf="@+id/labeltextflag"
                app:layout_constraintTop_toTopOf="@+id/labeltextflag" />

            <TextView
                android:id="@+id/labeltextmac"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:text="掩码长度:"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/edittextflag"
                app:layout_constraintLeft_toRightOf="@+id/edittextflag"
                app:layout_constraintTop_toTopOf="@+id/edittextflag" />

            <EditText
                android:id="@+id/edittextmac"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/shape4border"
                android:gravity="center"
                android:text="0"
                android:textColor="#FF0000"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/labeltextmac"
                app:layout_constraintLeft_toRightOf="@+id/labeltextmac"
                app:layout_constraintTop_toTopOf="@+id/labeltextmac" />

            <TextView
                android:id="@+id/labeltextnote5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="请选择已搜索到的卡片UID:"
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/butt_Request15693Card" />

            <Spinner
                android:id="@+id/sp_seleuid"
                android:layout_width="240dp"
                android:layout_height="20dp"
                app:layout_constraintBottom_toBottomOf="@+id/labeltextnote5"
                app:layout_constraintLeft_toRightOf="@+id/labeltextnote5"
                app:layout_constraintTop_toTopOf="@+id/labeltextnote5" />

            <TextView
                android:id="@+id/labeltextnote6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="请输入本次读写起始块地址:"
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/labeltextnote5" />

            <EditText
                android:id="@+id/edittextblockbegin"
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/shape4border"
                android:gravity="center"
                android:text="0"
                android:textColor="#FF0000"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/labeltextnote6"
                app:layout_constraintLeft_toRightOf="@+id/labeltextnote6"
                app:layout_constraintTop_toTopOf="@+id/labeltextnote6" />

            <TextView
                android:id="@+id/labeltextblocks"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:text="本次读写块数:"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/edittextblockbegin"
                app:layout_constraintLeft_toRightOf="@+id/edittextblockbegin"
                app:layout_constraintTop_toTopOf="@+id/edittextblockbegin" />

            <EditText
                android:id="@+id/textreadwriteblocks"
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/shape4border"
                android:gravity="center"
                android:text="28"
                android:textColor="#FF0000"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/labeltextblocks"
                app:layout_constraintLeft_toRightOf="@+id/labeltextblocks"
                app:layout_constraintTop_toTopOf="@+id/labeltextblocks" />

            <Button
                android:id="@+id/butt_read15693block"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="读取指定UID卡数据"
                android:textSize="11sp"
                android:onClick="read15693block"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/labeltextnote6" />

            <Button
                android:id="@+id/butt_write15693block"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="写数据到指定UID卡"
                android:textSize="11sp"
                android:onClick="write15693block"
                app:layout_constraintBottom_toBottomOf="@+id/butt_read15693block"
                app:layout_constraintLeft_toRightOf="@+id/butt_read15693block"
                app:layout_constraintTop_toTopOf="@+id/butt_read15693block" />

            <EditText
                android:id="@+id/editrwblockdata"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/shape4border"
                android:digits="0123456789ABCDEFabcdef "
                android:inputType="textCapCharacters"
                android:maxLines="8"
                android:gravity="left"
                android:text="                                               "
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/butt_read15693block" />

            <Button
                android:id="@+id/butt_write15693uid"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:onClick="write15693uid"
                android:text="将UID写入SLIX1830卡内"
                android:textSize="11sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/editrwblockdata" />

            <TextView
                android:id="@+id/labeltextnewuid"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:text="新UID:"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/butt_write15693uid"
                app:layout_constraintLeft_toRightOf="@+id/butt_write15693uid"
                app:layout_constraintTop_toTopOf="@+id/butt_write15693uid" />

            <EditText
                android:id="@+id/edittextnewuid"
                android:layout_width="130dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/shape4border"
                android:digits="0123456789ABCDEFabcdef"
                android:gravity="center"
                android:text="               "
                android:textColor="#FF0000"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/labeltextnewuid"
                app:layout_constraintLeft_toRightOf="@+id/labeltextnewuid"
                app:layout_constraintTop_toTopOf="@+id/labeltextnewuid" />

        </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.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<String> 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<DataLen){
            tv.setText("写入数据不足,请输入"+Integer.toString(DataLen*2)+"位16进制写入数据!");
            return;
        }else{
            mydatabuf[0]=DataLen;
            for(int p=0;p<strArr.length;p++) {
                mydatabuf[p+1]=(byte)(Integer.parseInt(strArr[p],16));
            }
        }

        status = ourmifare.iso15693writeex(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);
            tv.setText("写15693标签数据操作成功!");
        } else {
            PrintErrInf(status);   //错误代码提示
        }
    }

    public void Request15693card(View view){
        byte status;                            //存放返回值
        byte[] maskuidbuf = new byte[8];       //卡号掩码,一般可以不理这个参数
        byte[] myuid = new byte[81];           //卡数据缓冲
        byte[] revlen= new byte[1];

        tv.setText(" ");

        Spinner ctrspuid=findViewById(R.id.sp_seleuid);
        List<String> options = new ArrayList<>();
        options.clear();

        adapter = new ArrayAdapter<String>(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<revlen[0]) {
                String dsfidstr = "00" + Integer.toHexString(myuid[j] & 0xFF);
                dsfidstr= dsfidstr.substring(dsfidstr.length() - 2, dsfidstr.length());

                serialnumber = "";
                for (int i = 1; i < 9; i++) {
                    String bytestr = "00" + Integer.toHexString(myuid[j+i] & 0xFF);
                    serialnumber = serialnumber + bytestr.substring(bytestr.length() - 2, bytestr.length());
                }
                dispinf=dispinf+"DSFID:"+dsfidstr+"  UID:" + serialnumber + "\n";
                j=j+9;

                options.add(serialnumber);
            }
            adapter = new ArrayAdapter<String>(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<blocknum){
            status = ourmifare.iso15693readblock( flags, (byte)(startblock+j), (byte)1, myuid,revlen, mydatabuf) ;
            if (status==0){
                for (int i=0;i<revlen[0];i++ ){
                    String bytestr = "00" + Integer.toHexString(mydatabuf[i] & 0xFF);
                    readdatastr = readdatastr + bytestr.substring(bytestr.length() - 2, bytestr.length())+" ";
                }
                j++;
            }else{ j=blocknum;}
        }

        if (status == 0) {
            ourmifare.pcdbeep(38);
            ctrtextrwdata.setText(readdatastr);
            tv.setText("读取15693标签数据操作成功!");
        } else {
            PrintErrInf(status);   //错误代码提示
        }
    }

    public void write15693block(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("");

        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时的错误
        }

        Spinner spls = findViewById(R.id.sp_seletagtype);
        int tagtype =(int)(spls.getSelectedItemId());   //标签类型
        int blockbytes=4;
        if (tagtype==0) {
            blockbytes=4;
        }else {
            if (tagtype==1){
                blockbytes=8;     //
            } else{blockbytes=32;}
        }

        EditText ctrrwdata=findViewById(R.id.editrwblockdata);
        String rwdatahex=ctrrwdata.getText().toString().trim();
        String[] strArr = rwdatahex.split("\\ ");    /*分割接收到的数据后再分析、处理、返回指令*/
        if (strArr.length<blocknum*blockbytes) {
            tv.setText("写入数据不足,请输入" + Integer.toString(blocknum*blockbytes * 2) + "位16进制写入数据!");
            return;
        }

        int j=0;
        while (j<blocknum) {
            String disps="";
            for (int i=0;i<blockbytes;i++){
                mydatabuf[i]=(byte)(Integer.parseInt(strArr[j*blockbytes+i],16));
                disps=disps+strArr[j*blockbytes+i];
            }
            tv.setText(disps+"\n");
            status = ourmifare.iso15693writeblock( flags, (byte)(startblock+j), (byte)1, myuid,(byte)blockbytes, mydatabuf) ;
            if (status==0){
                j++;
            }else{j=blocknum;}
        }
        if (status==0){
            ourmifare.pcdbeep(38);
            tv.setText("写15693标签数据操作成功!");
        } else {
            PrintErrInf(status);   //错误代码提示
        }
    }

    public void write15693uid(View view){
        byte afi;
        byte myctrlword = 0;              //读写控制字,一般取0 就行了
        byte[] olduid = new byte[8];       //8字节卡序列号缓冲
        byte[] newuid = new byte[8];       //8字节卡序列号缓冲

        tv.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 ctrnewuid=findViewById(R.id.edittextnewuid) ;
        String newuidstr=ctrnewuid.getText().toString().trim();
        if (newuidstr.length() < 16)
        {
            tv.setText("UID是16位16进制数据,请输入正确的UID!");
            return;
        }else {
            for(int i=0;i<8;i++) {
                newuid[i]=(byte)Integer.parseInt(newuidstr.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
        }
        byte status = ourmifare.iso15693writeuid( myctrlword, afi, olduid, newuid) ;
        if (status==0){
            ourmifare.pcdbeep(38);
            tv.setText("写UID操作成功!");
        } else {
            PrintErrInf(status);   //错误代码提示
        }
    }
    public void PrintErrInf(byte errcode) {
        String dispstr="";
        switch(errcode){
            case 8:
                dispstr="错误代码:8,未寻到卡,请重新拿开卡后再放到感应区!";
                break;
            case 9:
                dispstr="错误代码:9,有多张卡在感应区,寻卡过程中防冲突失败,读序列吗错误!";
                break;
            case 10:
                dispstr="错误代码:10,该卡可能已被休眠,无法选中卡片!";
                break;
            case 13:
                dispstr="错误代码:13,读卡块失败!";
                break;
            case 14:
                dispstr="错误代码:14,写本块失败!";
                break;
            case 21:
                dispstr="错误代码:21,没有动态库!";
                break;
            case 22:
                dispstr="错误代码:22,动态库或驱动程序异常!";
                break;
            case 23:
                dispstr="错误代码:23,发卡器尚未插入!";
                break;
            case 24:
                dispstr="错误代码:24,操作超时,一般是动态库没有反映!";
                break;
            case 25:
                System.out.print("错误代码:25,发送字数不够!");
                break;
            case 26:
                System.out.print("错误代码:26,发送的CRC错!");
                break;
            case 27:
                System.out.print("错误代码:27,接收的字数不够!");
                break;
            case 28:
                System.out.print("错误代码:28,接收的CRC错!");
                break;
            case 41:
                System.out.print("错误代码:41,选中卡操作失败!");
                break;
            case 42:
                System.out.print("错误代码:42,让卡从静止中重起准备失败!");
                break;
            case 44:
                System.out.print("错误代码:44,一次写块不能超过12块!");
                break;
            case 46:
                System.out.print("错误代码:46,写AFI失败!");
                break;
            default:
                System.out.print("未知错误,错误代码:"+Integer.toString(errcode));
                break;
        }
        tv = findViewById(R.id.sample_text);
        tv.setText(dispstr);
    }
}

?

文章来源:https://blog.csdn.net/zhangjin7422/article/details/135421888
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。