安卓Android Studio读写FM1208CPU卡源码

本示例使用的发卡器:https://item.taobao.com/item.htm?spm=a1z10.5-c-s.w4002-21818769070.11.6c46789elLwMzv&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=".CpuCardActivity">

    <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="CPU卡测试页"
            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="100dp"
        android:layout_marginLeft="2dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/shape4border"
        android:padding="3dp"
        android:text="操作结果"
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

    <ScrollView
        android:id="@+id/scrollViewIC"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="3dp"

        android:scrollbars="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/sample_text"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:layout_constraintVertical_bias="0.0">

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

            <Button
                android:id="@+id/requestcpucard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="3dp"
                android:layout_marginRight="3dp"
                android:onClick="cpu_request"
                android:text="激活感应区内的CPU卡"
                android:textSize="11sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/textView0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginTop="5dp"
                android:text="卡片UID:"
                android:textSize="12sp"
                app:layout_constraintLeft_toRightOf="@+id/requestcpucard"
                app:layout_constraintTop_toTopOf="@+id/requestcpucard" />

            <TextView
                android:id="@+id/textView10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:text="每次卡片重新进入到感应区,都必须先激活!"
                android:textColor="#FF0000"
                android:textSize="11sp"
                app:layout_constraintBottom_toBottomOf="@+id/requestcpucard"
                app:layout_constraintLeft_toRightOf="@+id/requestcpucard" />


            <TextView
                android:id="@+id/textcarduid"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:background="@drawable/shape4border"
                android:gravity="center"
                android:textSize="14sp"
                app:layout_constraintBottom_toBottomOf="@+id/textView0"
                app:layout_constraintLeft_toRightOf="@+id/textView0"
                app:layout_constraintTop_toTopOf="@+id/textView0" />

            <Button
                android:id="@+id/cpu_rsinit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:onClick="cpu_rsinit"
                android:text="初始化CPU卡"
                android:textSize="11sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/requestcpucard" />

            <CheckBox
                android:id="@+id/checkcleckcard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="选择先清空卡再初始化,本次分配空间:"
                android:textSize="11sp"
                app:layout_constraintLeft_toRightOf="@+id/cpu_rsinit"
                app:layout_constraintTop_toTopOf="@+id/cpu_rsinit" />

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

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="设置卡片32位16进制密钥:"
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cpu_rsinit" />

            <EditText
                android:id="@+id/cardkey"
                android:layout_width="235dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:background="@drawable/shape4border"
                android:digits="0123456789ABCDEFabcdef"
                android:gravity="center"
                android:inputType="textCapCharacters"
                android:maxLength="32"
                android:maxLines="1"
                android:text="FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/textView2"
                app:layout_constraintLeft_toRightOf="@+id/textView2"
                app:layout_constraintTop_toTopOf="@+id/cpu_rsinit"
                app:layout_constraintVertical_bias="0.96" />

            <Button
                android:id="@+id/butt_cpu_rseasyfileadd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:onClick="cpu_rseasyfileadd"
                android:text="创建易写文件"
                android:textSize="11sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView2" />

            <Button
                android:id="@+id/butt_cpursfileadd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="cpu_rsfileadd"
                android:text="创建标准文件"
                android:textSize="11sp"
                app:layout_constraintBottom_toBottomOf="@+id/butt_cpu_rseasyfileadd"
                app:layout_constraintLeft_toRightOf="@+id/butt_cpu_rseasyfileadd"
                app:layout_constraintTop_toTopOf="@+id/butt_cpu_rseasyfileadd" />

            <TextView
                android:id="@+id/textviewcreafile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="请选择本次创建的文件号:"
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/butt_cpu_rseasyfileadd" />

            <Spinner
                android:id="@+id/spin_SelCreatefilenum"
                android:layout_width="130dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:entries="@array/cpucardfile"
                android:textSize="11sp"
                app:layout_constraintBottom_toBottomOf="@+id/textviewcreafile"
                app:layout_constraintLeft_toRightOf="@+id/textviewcreafile"
                app:layout_constraintTop_toTopOf="@+id/textviewcreafile" />


            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:text="文件大小:"
                android:textSize="11sp"
                app:layout_constraintBottom_toBottomOf="@+id/spin_SelCreatefilenum"
                app:layout_constraintLeft_toRightOf="@+id/spin_SelCreatefilenum"
                app:layout_constraintTop_toTopOf="@+id/spin_SelCreatefilenum" />

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

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="文件32位16进制只读密钥:"
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textviewcreafile" />

            <EditText
                android:id="@+id/readkey"
                android:layout_width="235dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:background="@drawable/shape4border"
                android:digits="0123456789ABCDEFabcdef"
                android:gravity="center"
                android:inputType="textCapCharacters"
                android:maxLength="32"
                android:maxLines="1"
                android:text="FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/textView3"
                app:layout_constraintLeft_toRightOf="@+id/textView3" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="文件32位16进制只读密钥:"
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView3" />

            <EditText
                android:id="@+id/writekey"
                android:layout_width="235dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:background="@drawable/shape4border"
                android:digits="0123456789ABCDEFabcdef"
                android:gravity="center"
                android:inputType="textCapCharacters"
                android:maxLength="32"
                android:maxLines="1"
                android:text="FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/textView4"
                app:layout_constraintLeft_toRightOf="@+id/textView4" />

            <Button
                android:id="@+id/butt_changfilekey"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:onClick="cpu_rsfilekeychg"
                android:text="修改文件密钥"
                android:textSize="11sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView4" />

            <Spinner
                android:id="@+id/spin_Selchangkeyfile"
                android:layout_width="130dp"
                android:layout_height="wrap_content"
                android:entries="@array/cpucardfile"
                android:textSize="11sp"
                app:layout_constraintBottom_toBottomOf="@+id/butt_changfilekey"
                app:layout_constraintLeft_toRightOf="@+id/butt_changfilekey"
                app:layout_constraintTop_toTopOf="@+id/butt_changfilekey" />

            <TextView
                android:id="@+id/textviewchanekeyfile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="请选择:"
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/butt_changfilekey" />

            <Spinner
                android:id="@+id/spin_Selauthkey"
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:entries="@array/cpucardauthkey"
                android:textSize="11sp"
                app:layout_constraintBottom_toBottomOf="@+id/textviewchanekeyfile"
                app:layout_constraintLeft_toRightOf="@+id/textviewchanekeyfile"
                app:layout_constraintTop_toTopOf="@+id/textviewchanekeyfile" />

            <Spinner
                android:id="@+id/spin_SelchangRWkey"
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:entries="@array/cpucardchangkey"
                android:textSize="11sp"
                app:layout_constraintBottom_toBottomOf="@+id/spin_Selauthkey"
                app:layout_constraintLeft_toRightOf="@+id/spin_Selauthkey"
                app:layout_constraintTop_toTopOf="@+id/spin_Selauthkey" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="32位16进制认证密钥:"
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/spin_Selauthkey" />

            <EditText
                android:id="@+id/authkey0"
                android:layout_width="235dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:background="@drawable/shape4border"
                android:digits="0123456789ABCDEFabcdef"
                android:gravity="center"
                android:inputType="textCapCharacters"
                android:maxLength="32"
                android:maxLines="1"
                android:text="FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/textView5"
                app:layout_constraintLeft_toRightOf="@+id/textView5"
                app:layout_constraintTop_toTopOf="@+id/textView5" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="32位16进制新建密钥:"
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView5" />

            <EditText
                android:id="@+id/newkey"
                android:layout_width="235dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:background="@drawable/shape4border"
                android:digits="0123456789ABCDEFabcdef"
                android:gravity="center"
                android:inputType="textCapCharacters"
                android:maxLength="32"
                android:maxLines="1"
                android:text="FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/textView6"
                app:layout_constraintLeft_toRightOf="@+id/textView6"
                app:layout_constraintTop_toTopOf="@+id/textView6" />

            <Button
                android:id="@+id/butt_readfile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:onClick="cpu_readfile"
                android:text="读文件数据"
                android:textSize="11sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView6" />

            <Button
                android:id="@+id/butt_writeeasyfile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="cpu_writeeasyfile"
                android:text="写数据到易写文件"
                android:textSize="11sp"
                app:layout_constraintBottom_toBottomOf="@+id/butt_readfile"
                app:layout_constraintLeft_toRightOf="@+id/butt_readfile"
                app:layout_constraintTop_toTopOf="@+id/butt_readfile" />

            <Button
                android:id="@+id/butt_writefile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="cpu_writefile"
                android:text="写数据到标准文件"
                android:textSize="11sp"
                app:layout_constraintBottom_toBottomOf="@+id/butt_writeeasyfile"
                app:layout_constraintLeft_toRightOf="@+id/butt_writeeasyfile"
                app:layout_constraintTop_toTopOf="@+id/butt_writeeasyfile" />

            <TextView
                android:id="@+id/textviewrwfile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="请选择:"
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/butt_readfile" />

            <Spinner
                android:id="@+id/spin_SelRWfile"
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:entries="@array/cpucardfile"
                android:textSize="11sp"
                app:layout_constraintBottom_toBottomOf="@+id/textviewrwfile"
                app:layout_constraintLeft_toRightOf="@+id/textviewrwfile"
                app:layout_constraintTop_toTopOf="@+id/textviewrwfile" />

            <Spinner
                android:id="@+id/spin_SelRWauthkey"
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:entries="@array/cpucardauthkey"
                android:textSize="11sp"
                app:layout_constraintBottom_toBottomOf="@+id/spin_SelRWfile"
                app:layout_constraintLeft_toRightOf="@+id/spin_SelRWfile"
                app:layout_constraintTop_toTopOf="@+id/spin_SelRWfile" />

            <TextView
                android:id="@+id/textviewrwauthkey"
                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/textviewrwfile" />

            <EditText
                android:id="@+id/editrwauthkey"
                android:layout_width="235dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:background="@drawable/shape4border"
                android:digits="0123456789ABCDEFabcdef"
                android:gravity="center"
                android:inputType="textCapCharacters"
                android:maxLength="32"
                android:maxLines="1"
                android:text="FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/textviewrwauthkey"
                app:layout_constraintLeft_toRightOf="@+id/textviewrwauthkey"
                app:layout_constraintTop_toTopOf="@+id/textviewrwauthkey" />


            <TextView
                android:id="@+id/textviewrwbegin"
                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/textviewrwauthkey" />

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

            <TextView
                android:id="@+id/textviewrwlen"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="52dp"
                android:text="本次读写长度:"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="@+id/textviewrwbegin"
                app:layout_constraintLeft_toRightOf="@+id/textrwbeginadd"
                app:layout_constraintTop_toTopOf="@+id/textviewrwbegin"
                app:layout_constraintVertical_bias="1.0" />

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

            <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:text="                                               "
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textviewrwbegin" />

            <Button
                android:id="@+id/butt_clearcupcard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:onClick="cpu_cardclr"
                android:text="强制清空已初始化过的CPU卡"
                android:textSize="11sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/editrwdata" />

        </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.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.reader.ourmifare;
public class CpuCardActivity extends AppCompatActivity {
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cpu_card);
        androidx.appcompat.widget.Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        tv = findViewById(R.id.sample_text);
        tv.setText("操作结果");

        Spinner spls = findViewById(R.id.spin_SelRWauthkey);
        spls.setSelection(1);
    }

    @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 cpu_request(View view){
        byte status;                         //存放返回值
        byte[] mypiccserial = new byte[4];   //4字节设备编号
        byte[] myparam = new byte[4];        //4字节卡参数
        byte[] myver= new byte[1];
        byte[] mycode= new byte[1];

        TextView ctrcarduid;
        ctrcarduid = findViewById(R.id.textcarduid);
        ctrcarduid.setText("");

        tv = findViewById(R.id.sample_text);
        tv.setText("");

        status =ourmifare.cpurequest(mypiccserial,myparam, myver, mycode);
        if (status == 0 || status == 52) {
            ourmifare.pcdbeep(38);
            String serialnumber = "";
            for (int i = 0; i < 4; i++) {
                String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);
                serialnumber = serialnumber + bytestr.substring(bytestr.length() - 2, bytestr.length());
            }
            ctrcarduid.setText(serialnumber);
            tv.setText("CPU卡已激活,卡号:"+serialnumber);
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }

    public void cpu_rsinit(View view){
        byte status;                         //存放返回值
        byte[] mypicckey = new byte[16];     //卡片认证密码,16进制,最好不要随便修改,此密码不影响数据安全
        byte mykeylen = 16 ;                 //卡片密码长度
        int  spacesize;                      //应用空间字节大小(要大于所有文件总和),根据不同容量的卡设不同值
        byte emptycard;                      //初始化前是否清空卡内数据,取值1将先清空卡,取值为0不清空现有数据

        String cardkeystr;
        CheckBox ctrcheck;
        EditText ctrcardkey;
        EditText ctrspace;

        ctrcheck=findViewById(R.id.checkcleckcard) ;
        if(ctrcheck.isChecked()) {
            emptycard=1;
        }else {emptycard=0;}

        ctrspace=findViewById(R.id.textusespace) ;
        spacesize=Integer.parseInt(ctrspace.getText().toString().trim());

        ctrcardkey=findViewById(R.id.cardkey) ;
        cardkeystr=ctrcardkey.getText().toString().trim();
        if (cardkeystr.length() < 32)
        {
            tv.setText("卡密钥是32位16进制数据,请输入正确的卡片密钥!");
            return;
        }else {
            for (int i = 0; i < 16; i++) {
                mypicckey[i] = (byte) Integer.parseInt(cardkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
        }

        status =ourmifare.cpursinit(emptycard, mypicckey, mykeylen, spacesize);
        if (status == 0 ) {
            ourmifare.pcdbeep(38);
            tv.setText("初始化卡成功!" );
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }

    public void cpu_rseasyfileadd(View view){
        byte status;                         //存放返回值
        byte RKLen = 16 ;                    //文件 只读权限密码 长度
        byte[] RFilekey = new byte[RKLen];   //文件 只读权限密码
        byte WKLen = 16 ;                    //文件 读写权限密码 长度
        byte[] WFilekey = new byte[WKLen];   //文件 读写权限密码,此权限高,可认证此密码修改只读密码,要记住文件密码

        Spinner spls = findViewById(R.id.spin_SelCreatefilenum);
        byte FileIndex = (byte)(spls.getSelectedItemId());   //指定文件号

        EditText ctrfilesize = findViewById(R.id.textfileszie);  //文件大小
        int FileSize=Integer.parseInt(ctrfilesize.getText().toString().trim());

        EditText ctrreadkey=findViewById(R.id.readkey) ;
        String readkeystr=ctrreadkey.getText().toString().trim();
        if (readkeystr.length() < 32)
        {
            tv.setText("文件只读密钥是32位16进制数据,请输入正确的只读密钥!");
            return;
        }else {
            for (int i = 0; i < 16; i++) {
                RFilekey[i] = (byte) Integer.parseInt(readkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
        }

        EditText ctrwritekey=findViewById(R.id.writekey) ;
        String writekeystr=ctrwritekey.getText().toString().trim();
        if (writekeystr.length() < 32)
        {
            tv.setText("文件读写密钥是32位16进制数据,请输入正确的读写密钥!");
            return;
        }else {
            for (int i = 0; i < 16; i++) {
                WFilekey[i] = (byte) Integer.parseInt(writekeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
        }

        status =ourmifare.cpursfileadds(FileIndex, RFilekey, RKLen, WFilekey, WKLen, FileSize) ;  //
        if (status == 0 ) {
            ourmifare.pcdbeep(38);
            tv.setText(Integer.toHexString(FileIndex+1)+" 号文件创建成功!");
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }

    public void cpu_rsfileadd(View view){
        byte status;                         //存放返回值
        byte RKLen = 16 ;                    //文件 只读权限密码 长度
        byte[] RFilekey = new byte[RKLen];   //文件 只读权限密码
        byte WKLen = 16 ;                    //文件 读写权限密码 长度
        byte[] WFilekey = new byte[WKLen];   //文件 读写权限密码,此权限高,可认证此密码修改只读密码,要记住文件密码

        Spinner spls = findViewById(R.id.spin_SelCreatefilenum);
        byte FileIndex = (byte)(spls.getSelectedItemId());   //指定文件号

        EditText ctrfilesize = findViewById(R.id.textfileszie);  //文件大小
        int FileSize=Integer.parseInt(ctrfilesize.getText().toString().trim());

        EditText ctrreadkey=findViewById(R.id.readkey) ;
        String readkeystr=ctrreadkey.getText().toString().trim();
        if (readkeystr.length() < 32)
        {
            tv.setText("文件只读密钥是32位16进制数据,请输入正确的只读密钥!");
            return;
        }else {
            for (int i = 0; i < 16; i++) {
                RFilekey[i] = (byte) Integer.parseInt(readkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
        }

        EditText ctrwritekey=findViewById(R.id.writekey) ;
        String writekeystr=ctrwritekey.getText().toString().trim();
        if (writekeystr.length() < 32)
        {
            tv.setText("文件读写密钥是32位16进制数据,请输入正确的读写密钥!");
            return;
        }else {
            for (int i = 0; i < 16; i++) {
                WFilekey[i] = (byte) Integer.parseInt(writekeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
        }

        status =ourmifare.cpursfileadd(FileIndex, RFilekey, RKLen, WFilekey, WKLen, FileSize) ;  //
        if (status == 0 ) {
            ourmifare.pcdbeep(38);
            tv.setText(Integer.toHexString(FileIndex+1)+" 号文件创建成功!");
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }

    public void cpu_rsfilekeychg(View view){
        byte status;                         //存放返回值
        byte OldKey;
        byte AKLen = 16 ;                    //文件 认证密码 长度
        byte[] Autkey = new byte[AKLen];     //文件 认证密码
        byte NKLen = 16 ;                    //文件 新密码密码 长度
        byte[] Newkey = new byte[NKLen];     //文件 新密码密码

        Spinner spls = findViewById(R.id.spin_Selchangkeyfile);
        byte FileIndex = (byte)(spls.getSelectedItemId());   //指定文件号

        Spinner spcharw = findViewById(R.id.spin_SelchangRWkey);
        byte ChaKey= (byte)(spcharw.getSelectedItemId());       //取值 0 表示用只读密码来认证,取值 2 表示用读写密码来认证

        Spinner spsuthkey = findViewById(R.id.spin_Selauthkey);
        if (spsuthkey.getSelectedItemId()==0) {
            OldKey = 0;               //取值 0 表示用只读密码来认证,取值 2 表示用读写密码来认证
        }else {OldKey=2;}

        byte ctrlCode=(byte)(ChaKey+OldKey);

        EditText ctrauthkey=findViewById(R.id.authkey0) ;
        String authkeystr=ctrauthkey.getText().toString().trim();
        if (authkeystr.length() < 32)
        {
             tv.setText("文件认证密钥是32位16进制数据,请输入正确的认证密钥!");
             return;
        }else {
            for (int i = 0; i < 16; i++) {
                Autkey[i] = (byte) Integer.parseInt(authkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
        }

        EditText ctrnewkey=findViewById(R.id.newkey) ;
        String newkeystr=ctrnewkey.getText().toString().trim();
        if (newkeystr.length() < 32)
        {
            tv.setText("文件新密钥是32位16进制数据,请输入正确的新密钥!");
            return;
        }else {
            for (int i = 0; i < 16; i++) {
                Newkey[i] = (byte) Integer.parseInt(newkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
        }

        status =ourmifare.cpursfilekeychg(FileIndex, ctrlCode, Autkey, AKLen, Newkey, NKLen) ;//& 0xff用于转为无符号行数据
        if (status == 0 ) {
            ourmifare.pcdbeep(38);
            tv.setText(Integer.toHexString(FileIndex+1)+" 号文件密码修改成功!" );
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }

    public void cpu_readfile(View view){
        byte status;                          //存放返回值
        byte AKLen = 16 ;                     //文件 认证密码 长度
        byte[] Autkey = new byte[AKLen];      //文件 认证密码
        byte DataStart=0;                     //读起始位置
        byte DataLen = 0 ;                   //读长度最大不能超过255,如文件长度>255 要循环读取
        byte[] DataBuf = new byte[300];      //读文件数据缓冲
        byte ctrlCode=0;                     //取值 0 表示用只读密码来认证,取值 2 表示用读写密码来认证

        EditText ctrrwdata = findViewById(R.id.editrwdata);
        ctrrwdata.setText("");

        Spinner spls = findViewById(R.id.spin_SelRWfile);
        byte FileIndex = (byte)(spls.getSelectedItemId());   //指定文件号

        Spinner spsuthkey = findViewById(R.id.spin_SelRWauthkey);
        if (spsuthkey.getSelectedItemId()==0) {
            ctrlCode = 0;               //取值 0 表示用只读密码来认证,取值 2 表示用读写密码来认证
        }else {ctrlCode=2;}

        EditText ctrauthkey=findViewById(R.id.editrwauthkey) ;
        String authkeystr=ctrauthkey.getText().toString().trim();
        if (authkeystr.length() < 32)
        {
            tv.setText("文件认证密钥是32位16进制数据,请输入正确的认证密钥!");
            return;
        }else {
            for (int i = 0; i < 16; i++) {
                Autkey[i] = (byte) Integer.parseInt(authkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
        }

        EditText ctrbeginadd = findViewById(R.id.textrwbeginadd);  //操作起始地址
        DataStart=(byte)Integer.parseInt(ctrbeginadd.getText().toString().trim());

        EditText ctrrwlen = findViewById(R.id.textrwlen);          //读写长度
        DataLen=(byte)Integer.parseInt(ctrrwlen.getText().toString().trim());

        if (DataLen<1 || DataLen>255){
            tv.setText("一次读取的数据长度应大于0,小于256!如要读取更多的数据请使用循环的方式读取。");
            return;
        }

        status = ourmifare.cpursfiledataread(FileIndex, ctrlCode, Autkey, AKLen, DataStart, DataBuf,DataLen) ;//& 0xff用于转为无符号行数据
        if (status == 0 ) {
            ourmifare.pcdbeep(38);
            String filedata="";
            for (int i = 0; i < DataLen; i++) {
                String bytestr = "00" + Integer.toHexString(DataBuf[i] & 0xff);
                filedata = filedata + bytestr.substring(bytestr.length() - 2, bytestr.length()) +" ";
            }

            ctrrwdata.setText(filedata);
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }

    public void cpu_writeeasyfile(View view){
        byte status;                          //存放返回值
        byte AKLen = 16 ;                    //文件 认证密码 长度
        byte[] Autkey = new byte[AKLen];     //文件 认证密码
        byte DataStart=0;                    //写起始位置
        byte DataLen  ;                      //写长度最大不能超过247,如文件长度>247 要循环写入
        byte[] DataBuf = new byte[300];      //写文件数据缓冲
        byte ctrlCode=0;                     //取值 0 表示用只读密码来认证,取值 2 表示用读写密码来认证

        Spinner spls = findViewById(R.id.spin_SelRWfile);
        byte FileIndex = (byte)(spls.getSelectedItemId());   //指定文件号

        Spinner spsuthkey = findViewById(R.id.spin_SelRWauthkey);
        if (spsuthkey.getSelectedItemId()==0) {
            ctrlCode = 0;               //取值 0 表示用只读密码来认证,取值 2 表示用读写密码来认证
        }else {ctrlCode=2;}

        EditText ctrauthkey=findViewById(R.id.editrwauthkey) ;
        String authkeystr=ctrauthkey.getText().toString().trim();
        if (authkeystr.length() < 32)
        {
            tv.setText("文件认证密钥是32位16进制数据,请输入正确的认证密钥!");
            return;
        }else {
            for (int i = 0; i < 16; i++) {
                Autkey[i] = (byte) Integer.parseInt(authkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
        }

        EditText ctrbeginadd = findViewById(R.id.textrwbeginadd);  //操作起始地址
        DataStart=(byte)Integer.parseInt(ctrbeginadd.getText().toString().trim());

        EditText ctrrwlen = findViewById(R.id.textrwlen);          //读写长度
        DataLen=(byte)Integer.parseInt(ctrrwlen.getText().toString().trim());

        if (DataLen<1 || DataLen>247){
            tv.setText("一次写入的数据长度应大于0,小于248,如要写入更多的数据可使用循环的方式写入。");
            return;
        }

        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{
            for(int p=0;p<strArr.length;p++) {
                DataBuf[p]=(byte)(Integer.parseInt(strArr[p],16));
            }
        }

        status =ourmifare.cpursfiledatawrites(FileIndex, ctrlCode, Autkey, AKLen, DataStart, DataBuf,DataLen) ;//& 0xff用于转为无符号行数据
        if (status == 0 ) {
            ourmifare.pcdbeep(38);
            tv.setText("写数据成功!" );
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }

    public void cpu_writefile(View view){
        byte status;                          //存放返回值
        byte AKLen = 16 ;                    //文件 认证密码 长度
        byte[] Autkey = new byte[AKLen];     //文件 认证密码
        byte DataStart=0;                    //写起始位置
        byte DataLen  ;                      //写长度最大不能超过247,如文件长度>247 要循环写入
        byte[] DataBuf = new byte[300];      //写文件数据缓冲
        byte ctrlCode=0;                     //取值 0 表示用只读密码来认证,取值 2 表示用读写密码来认证

        Spinner spls = findViewById(R.id.spin_SelRWfile);
        byte FileIndex = (byte)(spls.getSelectedItemId());   //指定文件号

        Spinner spsuthkey = findViewById(R.id.spin_SelRWauthkey);
        if (spsuthkey.getSelectedItemId()==0) {
            ctrlCode = 0;               //取值 0 表示用只读密码来认证,取值 2 表示用读写密码来认证
        }else {ctrlCode=2;}

        EditText ctrauthkey=findViewById(R.id.editrwauthkey) ;
        String authkeystr=ctrauthkey.getText().toString().trim();
        if (authkeystr.length() < 32)
        {
            tv.setText("文件认证密钥是32位16进制数据,请输入正确的认证密钥!");
            return;
        }else {
            for (int i = 0; i < 16; i++) {
                Autkey[i] = (byte) Integer.parseInt(authkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
        }

        EditText ctrbeginadd = findViewById(R.id.textrwbeginadd);  //操作起始地址
        DataStart=(byte)Integer.parseInt(ctrbeginadd.getText().toString().trim());

        EditText ctrrwlen = findViewById(R.id.textrwlen);          //读写长度
        DataLen=(byte)Integer.parseInt(ctrrwlen.getText().toString().trim());

        if (DataLen<1 || DataLen>247){
            tv.setText("一次写入的数据长度应大于0,小于248,如要写入更多的数据可使用循环的方式写入。");
            return;
        }

        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{
            for(int p=0;p<strArr.length;p++) {
                DataBuf[p]=(byte)(Integer.parseInt(strArr[p],16));
            }
        }

        status =ourmifare.cpursfiledatawrite(FileIndex, ctrlCode, Autkey, AKLen, DataStart, DataBuf,DataLen) ;//& 0xff用于转为无符号行数据
        if (status == 0 ) {
            ourmifare.pcdbeep(38);
            tv.setText("写数据成功!" );
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }

    public void cpu_cardclr(View view){
        byte status=ourmifare.cpurscardclr() ;//& 0xff用于转为无符号行数据
        if (status == 0 ) {
            ourmifare.pcdbeep(38);
            tv.setText("强制清空卡成功!");
        } else if(status == 64){
            tv.setText("此卡为未经过我们cpursinit函数成功初始化的卡!");
        }else{
            PrintErrInf(status);   //返回代码提示
        }
    }
    public void PrintErrInf(byte errcode) {
        String dispstr;
        switch(errcode){
            case 8:
                dispstr="错误代码:8,未寻到卡,请重新拿开卡后再放到感应区!";
                break;
            case 21:
                dispstr="错误代码:21,没有动态库!";
                break;
            case 22:
                dispstr="错误代码:22,动态库或驱动程序异常!";
                break;
            case 23:
                dispstr="错误代码:23,驱动程序错误或尚未安装!";
                break;
            case 24:
                dispstr="错误代码:24,操作超时,一般是动态库没有反映!";
                break;
            case 25:
                dispstr="错误代码:25,发送字数不够!";
                break;
            case 26:
                dispstr="错误代码:26,发送的CRC错!";
                break;
            case 27:
                dispstr="错误代码:27,接收的字数不够!";
                break;
            case 28:
                dispstr="错误代码:28,接收的CRC错!";
                break;
            case 50:
                dispstr="错误代码:50,RATS错误,厂家调试代码,用户不需理会!";
                break;
            case 51:
                dispstr="错误代码:51,PPS错误,厂家调试代码,用户不需理会!";
                break;
            case 52:
                dispstr="错误代码:52,已进入了14443-4协议状态,可进行CPU卡功能所有操作了!";
                break;
            case 53:
                dispstr="错误代码:53,CPU卡功能通讯错误,请先激活卡片!";
                break;
            case 54:
                dispstr="错误代码:54,数据不足,需要接着发送未完成的数据至卡上!";
                break;
            case 55:
                dispstr="错误代码:55,发送ACK指令给卡,让卡接着发送数据回来!";
                break;
            case 56:
                dispstr="错误代码:56,清空根目录失败!";
                break;
            case 57:
                dispstr="错误代码:57,卡片不支持功能!";
                break;
            case 58:
                dispstr="错误代码:58,卡片初始化失败!";
                break;
            case 59:
                dispstr="错误代码:59,分配的空间不足!";
                break;
            case 60:
                dispstr="错误代码:60,本次操作的实体已存在!";
                break;
            case 61:
                dispstr="错误代码:61,无足够空间!";
                break;
            case 62:
                dispstr="错误代码:62,文件不存在!";
                break;
            case 63:
                dispstr="错误代码:63,权限不足,有可能是用只读密码认证,导致无法更改读写密码或无法写文件!";
                break;
            case 64:
                dispstr="错误代码:64,密码不存在,或密钥文件未创建!";
                break;
            case 65:
                dispstr="错误代码:65,传送长度错误!";
                break;
            case 66:
                dispstr="错误代码:66,Le错误,即接收的数据长度指定过大!";
                break;
            case 67:
                dispstr="错误代码:67,功能不支持或卡中无MF 或卡片已锁定!";
                break;
            case 68:
                dispstr="错误代码:68,密码认证错误次数过多,该密码已被锁死!";
                break;
            case 70:
            case 71:
            case 72:
            case 73:
            case 74:
            case 75:
            case 76:
            case 77:
            case 78:
            case 79:
            case 80:
            case 81:
            case 82:
            case 83:
            case 84:
            case 85:
                dispstr="错误代码:"+Integer.toString(errcode)+",密码错误,剩余次数为"+Integer.toString(errcode-70)+",如果为0,该密码将锁死,无法再认证!";
                break;
            case 86:
                dispstr="错误代码:"+Integer.toString(errcode)+",更改后的密码长度必须和创建时的长度一致!";
                break;
            case 87:
                dispstr="错误代码:"+Integer.toString(errcode)+",应用目录不存在!";
                break;
            case 88:
                dispstr="错误代码:"+Integer.toString(errcode)+",应用文件不存在!";
                break;
            case 89:
                dispstr="错误代码:"+Integer.toString(errcode)+",文件号不能超过 5 ";
                break;
            case 90:
                dispstr="错误代码:"+Integer.toString(errcode)+",读取文件时返回的长度不足,数据可能不正确!";
                break;
            case 91:
                dispstr="错误代码:"+Integer.toString(errcode)+",一次读文件的长度不能超过 255";
                break;
            case 92:
                dispstr="错误代码:"+Integer.toString(errcode)+",一次写文件的长度不能超过 247";
                break;
            default:
                dispstr="未知错误,错误代码:"+Integer.toString(errcode);
                break;
        }
        tv = findViewById(R.id.sample_text);
        tv.setText(dispstr);
    }
}

相关推荐

  1. Android.nfc

    2024-01-07 16:34:03       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-07 16:34:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-07 16:34:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-07 16:34:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-07 16:34:03       20 阅读

热门阅读

  1. 38 调优kafka

    2024-01-07 16:34:03       42 阅读
  2. python贪吃蛇

    2024-01-07 16:34:03       35 阅读
  3. 前端算法之动态规划

    2024-01-07 16:34:03       40 阅读
  4. 什么是Selinux

    2024-01-07 16:34:03       37 阅读
  5. Qt实现FTP文件传输协议

    2024-01-07 16:34:03       45 阅读
  6. ISO 26262:引领汽车安全的新标准

    2024-01-07 16:34:03       39 阅读
  7. 【comp221】Flask Python Web

    2024-01-07 16:34:03       38 阅读
  8. C++实现分水岭算法(Watershed Algorithm)

    2024-01-07 16:34:03       32 阅读
  9. Ribbon相关问题及答案(2024)

    2024-01-07 16:34:03       33 阅读