安卓Android Studio读写MifareOne M1 IC卡源码

本示例使用的发卡器:

https://item.taobao.com/item.htm?id=615391857885&spm=a1z10.5-c-s.w4002-21818769070.11.66af789eLeok2R

<?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=".IcCardActivity"><androidx.appcompat.widget.Toolbarandroid: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"><TextViewandroid: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" /><TextViewandroid:id="@+id/TextViewlabelDisp"android:layout_width="match_parent"android:layout_height="match_parent"android:text="IC卡测试页"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><TextViewandroid: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"        /><ScrollViewandroid: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.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="3dp"><Buttonandroid:id="@+id/btnPiccrequest"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:onClick="piccrequest"android:padding="3dp"android:text="读取IC卡卡号"android:textSize="12sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent"/><TextViewandroid:id="@+id/TextViewlabeluid"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:text="UID:"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/btnPiccrequest"app:layout_constraintLeft_toRightOf="@+id/btnPiccrequest"app:layout_constraintTop_toTopOf="@+id/btnPiccrequest" /><EditTextandroid:id="@+id/TextViewuid"android:layout_width="75dp"android:layout_height="wrap_content"android:text=""android:background="@drawable/shape4border"android:digits="0123456789ABCDEFabcdef"android:gravity="center"android:textSize="14sp"android:textColor="#FF0000"app:layout_constraintBottom_toBottomOf="@+id/TextViewlabeluid"app:layout_constraintLeft_toRightOf="@+id/TextViewlabeluid"app:layout_constraintTop_toTopOf="@+id/TextViewlabeluid" /><TextViewandroid:id="@+id/TextViewlabelconv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:text="转8H10D码:"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/TextViewuid"app:layout_constraintLeft_toRightOf="@+id/TextViewuid"app:layout_constraintTop_toTopOf="@+id/TextViewuid" /><TextViewandroid:id="@+id/TextViewlabel8h10d"android:layout_width="95dp"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:background="@drawable/shape4border"android:text=""android:textSize="14sp"android:gravity="center"android:textColor="#FF0000"app:layout_constraintBottom_toBottomOf="@+id/TextViewlabelconv"app:layout_constraintLeft_toRightOf="@+id/TextViewlabelconv"app:layout_constraintTop_toTopOf="@+id/TextViewlabelconv" /><Buttonandroid:id="@+id/btnwriteuid"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="2dp"android:textSize="12sp"android:onClick="piccwriteserial"android:text="将上面UID号写入到CUID卡内"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/btnPiccrequest" /><Buttonandroid:id="@+id/butt_writekeytoe2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="2dp"android:textSize="12sp"android:onClick="writekeytoe2"android:text="将下面的卡片认证密钥装载到发卡器内部芯片,使密钥不外泄"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/btnwriteuid" /><TextViewandroid:id="@+id/TextViewlabelArea"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="15dp"android:text="选择读写区号:"android:textSize="12sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/butt_writekeytoe2" /><Spinnerandroid:id="@+id/SpinnerArea"android:layout_width="130dp"android:layout_height="wrap_content"android:entries="@array/AreaNumber"android:spinnerMode="dropdown"android:theme="@style/my_spinner_style"app:layout_constraintBottom_toBottomOf="@+id/TextViewlabelArea"app:layout_constraintLeft_toRightOf="@+id/TextViewlabelArea"app:layout_constraintTop_toTopOf="@+id/TextViewlabelArea" /><TextViewandroid:id="@+id/TextViewlabelkeymodel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="15dp"android:layout_marginTop="5dp"android:text="密钥方式:"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/SpinnerArea"app:layout_constraintLeft_toRightOf="@+id/SpinnerArea"app:layout_constraintTop_toTopOf="@+id/SpinnerArea" /><Spinnerandroid:id="@+id/SpinnerInOutKey"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="2dp"android:entries="@array/M1InOutKey"android:spinnerMode="dropdown"android:theme="@style/my_spinner_style"app:layout_constraintBottom_toBottomOf="@+id/TextViewlabelkeymodel"app:layout_constraintLeft_toRightOf="@+id/TextViewlabelkeymodel"app:layout_constraintTop_toTopOf="@+id/TextViewlabelkeymodel" /><TextViewandroid:id="@+id/TextViewlabelSelABkey"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/TextViewlabelArea" /><Spinnerandroid:id="@+id/SpinnerSelABKey"android:layout_width="130dp"android:layout_height="wrap_content"android:entries="@array/M1AuthABKey"android:spinnerMode="dropdown"android:theme="@style/my_spinner_style"app:layout_constraintBottom_toBottomOf="@+id/TextViewlabelSelABkey"app:layout_constraintLeft_toRightOf="@+id/TextViewlabelSelABkey"app:layout_constraintTop_toTopOf="@+id/TextViewlabelSelABkey" /><TextViewandroid:id="@+id/TextViewlabelkey"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="15dp"android:layout_marginTop="5dp"android:text="认证密钥:"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/SpinnerSelABKey"app:layout_constraintLeft_toRightOf="@+id/SpinnerSelABKey"app:layout_constraintTop_toTopOf="@+id/SpinnerSelABKey" /><EditTextandroid:id="@+id/EdittextAuthkey"android:layout_width="100dp"android:layout_height="wrap_content"android:background="@drawable/shape4border"android:digits="0123456789ABCDEFabcdef"android:gravity="center"android:textSize="12sp"android:maxLength="12"android:textColor="#0000FF"android:text="FFFFFFFFFFFF"app:layout_constraintBottom_toBottomOf="@+id/TextViewlabelkey"app:layout_constraintLeft_toRightOf="@+id/TextViewlabelkey"app:layout_constraintTop_toTopOf="@+id/TextViewlabelkey" /><Buttonandroid:id="@+id/btnPiccreadex"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:onClick="piccreadex"android:textSize="12sp"android:text="轻松读取选定扇区数据"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/TextViewlabelSelABkey" /><Buttonandroid:id="@+id/btnPiccwriteex"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="piccwriteex"android:text="轻松写数据到选定扇区"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/btnPiccreadex"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="@+id/btnPiccreadex" /><TextViewandroid:id="@+id/TextViewlabelblock0"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="第0块:"android:textSize="12sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/btnPiccreadex" /><EditTextandroid:id="@+id/Edittextblock0"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:background="@drawable/shape4border"android:digits="0123456789ABCDEFabcdef "android:gravity="left"android:maxLength="47"android:text="00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/TextViewlabelblock0"app:layout_constraintLeft_toRightOf="@+id/TextViewlabelblock0"app:layout_constraintTop_toTopOf="@+id/TextViewlabelblock0" /><TextViewandroid:id="@+id/TextViewlabelblock1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="第1块:"android:textSize="12sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/TextViewlabelblock0" /><EditTextandroid:id="@+id/Edittextblock1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:background="@drawable/shape4border"android:digits="0123456789ABCDEFabcdef "android:gravity="left"android:maxLength="47"android:text="00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/TextViewlabelblock1"app:layout_constraintLeft_toRightOf="@+id/TextViewlabelblock1"app:layout_constraintTop_toTopOf="@+id/TextViewlabelblock1" /><TextViewandroid:id="@+id/TextViewlabelblock2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="第2块:"android:textSize="12sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/TextViewlabelblock1" /><EditTextandroid:id="@+id/Edittextblock2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:background="@drawable/shape4border"android:digits="0123456789ABCDEFabcdef "android:gravity="left"android:maxLength="47"android:text="00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/TextViewlabelblock2"app:layout_constraintLeft_toRightOf="@+id/TextViewlabelblock2"app:layout_constraintTop_toTopOf="@+id/TextViewlabelblock2" /><TextViewandroid:id="@+id/TextViewlabelblock3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="第3块:"android:textSize="12sp"android:textColor="#0000FF"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/TextViewlabelblock2" /><EditTextandroid:id="@+id/Edittextblock3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:background="@drawable/shape4border"android:digits="0123456789ABCDEFabcdef "android:gravity="left"android:maxLength="47"android:textColor="#0000FF"android:text="FF FF FF FF FF FF FF 07 80 69 FF FF FF FF FF FF"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/TextViewlabelblock3"app:layout_constraintLeft_toRightOf="@+id/TextViewlabelblock3"app:layout_constraintTop_toTopOf="@+id/TextViewlabelblock3" /><TextViewandroid:id="@+id/TextViewlabelnote4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="密钥块"android:textSize="12sp"android:textColor="#0000FF"app:layout_constraintBottom_toBottomOf="@+id/Edittextblock3"app:layout_constraintLeft_toRightOf="@+id/Edittextblock3"app:layout_constraintTop_toTopOf="@+id/Edittextblock3" /><Buttonandroid:id="@+id/btnPiccchangesinglekeyex"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:onClick="piccchangesinglekeyex"android:text="修改卡片密钥"android:textSize="12sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/TextViewlabelblock3" /><Spinnerandroid:id="@+id/SpinnerchangeKey"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="2dp"android:entries="@array/M1changeKey"android:spinnerMode="dropdown"android:theme="@style/my_spinner_style"app:layout_constraintBottom_toBottomOf="@+id/btnPiccchangesinglekeyex"app:layout_constraintLeft_toRightOf="@+id/btnPiccchangesinglekeyex"app:layout_constraintTop_toTopOf="@+id/btnPiccchangesinglekeyex" /><TextViewandroid:id="@+id/TextViewlabelnewkeya"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="新A密钥:"android:textSize="12sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/btnPiccchangesinglekeyex" /><EditTextandroid:id="@+id/Edittextnewkeya"android:layout_width="90dp"android:layout_height="wrap_content"android:background="@drawable/shape4border"android:digits="0123456789ABCDEFabcdef"android:gravity="center"android:textSize="12sp"android:maxLength="12"android:textColor="#0000FF"android:text="FFFFFFFFFFFF"app:layout_constraintBottom_toBottomOf="@+id/TextViewlabelnewkeya"app:layout_constraintLeft_toRightOf="@+id/TextViewlabelnewkeya"app:layout_constraintTop_toTopOf="@+id/TextViewlabelnewkeya" /><TextViewandroid:id="@+id/TextViewlabelctr"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="控制字:"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/Edittextnewkeya"app:layout_constraintLeft_toRightOf="@+id/Edittextnewkeya"app:layout_constraintTop_toTopOf="@+id/Edittextnewkeya" /><EditTextandroid:id="@+id/Edittextnewctr"android:layout_width="60dp"android:layout_height="wrap_content"android:background="@drawable/shape4border"android:digits="0123456789ABCDEFabcdef"android:gravity="center"android:textSize="12sp"android:maxLength="8"android:textColor="#0000FF"android:text="FF078069"app:layout_constraintBottom_toBottomOf="@+id/TextViewlabelctr"app:layout_constraintLeft_toRightOf="@+id/TextViewlabelctr"app:layout_constraintTop_toTopOf="@+id/TextViewlabelctr" /><TextViewandroid:id="@+id/TextViewlabelnewkeyb"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="B密钥:"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/Edittextnewctr"app:layout_constraintLeft_toRightOf="@+id/Edittextnewctr"app:layout_constraintTop_toTopOf="@+id/Edittextnewctr" /><EditTextandroid:id="@+id/Edittextnewkeyb"android:layout_width="90dp"android:layout_height="wrap_content"android:background="@drawable/shape4border"android:digits="0123456789ABCDEFabcdef"android:gravity="center"android:textSize="12sp"android:maxLength="12"android:textColor="#0000FF"android:text="FFFFFFFFFFFF"app:layout_constraintBottom_toBottomOf="@+id/TextViewlabelnewkeyb"app:layout_constraintLeft_toRightOf="@+id/TextViewlabelnewkeyb"app:layout_constraintTop_toTopOf="@+id/TextViewlabelnewkeyb" /><Buttonandroid:id="@+id/btnPiccreadex1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:onClick="piccreadexevery200ms"android:textSize="12sp"android:text="启动连续每隔0.2秒读一次卡"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/TextViewlabelnewkeya" /></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 com.reader.ourmifare;import java.util.Timer;
import java.util.TimerTask;public class IcCardActivity extends AppCompatActivity {private TextView tv;private static final byte BLOCK0_EN = 0x01;private static final byte BLOCK1_EN = 0x02;private static final byte BLOCK2_EN = 0x04;private static final byte NEEDSERIAL = 0x08;private static final byte EXTERNKEY = 0x10;private static final byte NEEDHALT = 0x20;private long proctimes;private Timer timer = null;private TimerTask task = null;private boolean iscontinue = true;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_ic_card);androidx.appcompat.widget.Toolbar toolbar=findViewById(R.id.toolbar);setSupportActionBar(toolbar);tv = findViewById(R.id.sample_text);tv.setText("操作结果");Spinner ctrkeymod=findViewById(R.id.SpinnerInOutKey);ctrkeymod.setSelection(1);Spinner ctrselauth=findViewById(R.id.SpinnerSelABKey);ctrselauth.setSelection(1);}@Overridepublic void onBackPressed(){super.onBackPressed();finish();}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if(item.getItemId()==android.R.id.home){finish();return true;}return super.onOptionsItemSelected(item);}public void retmain(View view){finish();}//读IC卡卡号public void piccrequest(View view){byte status;//存放返回值byte[] mypiccserial = new byte[7];long cardhao;String strls;EditText ctruid=findViewById(R.id.TextViewuid);ctruid.setText("");TextView ctr8h10d=findViewById(R.id.TextViewlabel8h10d);ctr8h10d.setText("");tv.setText("");status = ourmifare.piccrequest(mypiccserial);if(status == 0){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());}serialnumber=serialnumber.toUpperCase();ctruid.setText(serialnumber);cardhao = mypiccserial[3] & 0xff;cardhao *= 256;cardhao += mypiccserial[2] & 0xff;cardhao *= 256;cardhao += mypiccserial[1] & 0xff;cardhao *= 256;cardhao += mypiccserial[0] & 0xff;String strls1 = "000000000"+Long.toString(cardhao);//0305887634  123B7992strls1=strls1.substring(strls1.length()-10,strls1.length());ctr8h10d.setText(strls1);strls = "读取成功!16进制卡号为:" +serialnumber+"\n转8H10D卡号:"+ strls1;tv.setText(strls);}else{PrintErrInf(status);}}public void piccwriteserial(View view){byte status;//存放返回值byte authmode;//密码类型,用A密码或B密码byte myctrlword;//控制字byte[] mypiccserial = new byte[4];//卡序列号byte[] mypicckey = new byte[6];//密码byte[] mypiccdata = new byte[48];//卡数据缓冲byte[] myblockdata=new byte[16];//第三块数据TextView ctr8h10d=findViewById(R.id.TextViewlabel8h10d);ctr8h10d.setText("");tv.setText("");myctrlword = BLOCK0_EN ;        //控制字指定,控制字的含义请查看本公司网站提供的动态库说明Spinner ctrauthmode=findViewById(R.id.SpinnerInOutKey);if (ctrauthmode.getSelectedItemId()==1){myctrlword=(byte)(myctrlword + EXTERNKEY);        //选用外部密钥认证}Spinner crtauthabkey=findViewById(R.id.SpinnerSelABKey);authmode =(byte)crtauthabkey.getSelectedItemId();    //大于0表示用A密码认证,推荐用A密码认证EditText ctrauthkey=findViewById(R.id.EdittextAuthkey); //认证密钥String authkeystr=ctrauthkey.getText().toString().trim();if (authkeystr.length()!=12){tv.setText("认证密钥为12位16进制数,请输入正确的卡认证密钥!");return;}for (int i=0;i<6;i++){mypicckey[i]=(byte) Integer.parseInt(authkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误}EditText ctruid=findViewById(R.id.TextViewuid);String newuidstr=ctruid.getText().toString().trim();if (newuidstr.length()!=8){tv.setText("UID为8位16进制数,请输入正确的UID号!");return;}byte edc=0;for (int i=0;i<4;i++){myblockdata[i]=(byte) Integer.parseInt(newuidstr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误edc=(byte)(edc ^ myblockdata[i]);   //UID校验字}myblockdata[4]=edc;EditText ctrblock0=findViewById(R.id.Edittextblock0);  //如果第0块显示有数据,将第0块的第5字节以后的数据也写入String blockstr0=ctrblock0.getText().toString().trim();String[] strArr= blockstr0.split("\\ ");             /*分割接收到的数据后再分析、处理、返回指令 */if (strArr.length==16){for(int p=5;p<16;p++) {myblockdata[p]=(byte)(Integer.parseInt(strArr[p],16));}}status=ourmifare.piccwriteserial(myctrlword,mypiccserial,(byte)0,authmode,mypicckey,myblockdata);if(status == 0){status = ourmifare.piccrequest(mypiccserial);if(status == 0){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());}serialnumber=serialnumber.toUpperCase();ctruid.setText(serialnumber);long cardhao;cardhao = mypiccserial[3] & 0xff;cardhao *= 256;cardhao += mypiccserial[2] & 0xff;cardhao *= 256;cardhao += mypiccserial[1] & 0xff;cardhao *= 256;cardhao += mypiccserial[0] & 0xff;String strls1 = "000000000"+Long.toString(cardhao);//0305887634  123B7992strls1=strls1.substring(strls1.length()-10,strls1.length());ctr8h10d.setText(strls1);String strls = "写UID卡号成功!16进制卡号为:" +serialnumber+"\n转8H10D卡号:"+ strls1;tv.setText(strls);}else{PrintErrInf(status);}}else{PrintErrInf(status);}}public void writekeytoe2(View view){byte status;//存放返回值byte myareano;//区号byte authmode;//密码类型,用A密码或B密码byte[] mypicckey = new byte[6];//密码tv.setText("");Spinner spls= findViewById(R.id.SpinnerArea);myareano = (byte)(spls.getSelectedItemId());          //指定区号String strl="";Spinner crtauthabkey=findViewById(R.id.SpinnerSelABKey);authmode =(byte)crtauthabkey.getSelectedItemId();    //大于0表示用A密码认证,推荐用A密码认证if(authmode==0){strl="B认证密钥";}else {strl="A认证密钥";}EditText ctrauthkey=findViewById(R.id.EdittextAuthkey); //认证密钥String authkeystr=ctrauthkey.getText().toString().trim();if (authkeystr.length()!=12){tv.setText("认证密钥为12位16进制数,请输入正确的卡认证密钥!");return;}for (int i=0;i<6;i++){mypicckey[i]=(byte) Integer.parseInt(authkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误}status = ourmifare.pcdwritekeytoe2(myareano,authmode,mypicckey);if(status == 0) {ourmifare.pcdbeep(38);tv.setText("第"+Integer.toString(myareano)+"区内部"+strl+"写入到发卡器芯片内成功!");}else{PrintErrInf(status);}}//轻松读卡,返回4字节卡号及48字节扇区数据public void piccreadex(View view){byte status;//存放返回值byte myareano;//区号byte authmode;//密码类型,用A密码或B密码byte myctrlword;//控制字byte[] mypiccserial = new byte[4];//卡序列号byte[] mypicckey = new byte[6];//密码byte[] mypiccdata = new byte[48];//卡数据缓冲byte[] myblockdata=new byte[16];//第三块数据EditText ctruid=findViewById(R.id.TextViewuid);ctruid.setText("");TextView ctr8h10d=findViewById(R.id.TextViewlabel8h10d);ctr8h10d.setText("");tv.setText("");myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN;        //控制字指定,控制字的含义请查看本公司网站提供的动态库说明Spinner ctrauthmode=findViewById(R.id.SpinnerInOutKey);if (ctrauthmode.getSelectedItemId()==1){myctrlword=(byte)(myctrlword + EXTERNKEY);        //选用外部密钥认证}Spinner spls= findViewById(R.id.SpinnerArea);myareano = (byte)(spls.getSelectedItemId());          //指定区号Spinner crtauthabkey=findViewById(R.id.SpinnerSelABKey);authmode =(byte)crtauthabkey.getSelectedItemId();    //大于0表示用A密码认证,推荐用A密码认证EditText ctrauthkey=findViewById(R.id.EdittextAuthkey); //认证密钥String authkeystr=ctrauthkey.getText().toString().trim();if (authkeystr.length()!=12){tv.setText("认证密钥为12位16进制数,请输入正确的卡认证密钥!");return;}for (int i=0;i<6;i++){mypicckey[i]=(byte) Integer.parseInt(authkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误}status = ourmifare.piccreadexfm11rf32(myctrlword,mypiccserial,myareano,authmode,mypicckey,mypiccdata);if(status == 0){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());}serialnumber=serialnumber.toUpperCase();ctruid.setText(serialnumber);long cardhao;cardhao = mypiccserial[3] & 0xff;cardhao *= 256;cardhao += mypiccserial[2] & 0xff;cardhao *= 256;cardhao += mypiccserial[1] & 0xff;cardhao *= 256;cardhao += mypiccserial[0] & 0xff;String strls1 = "000000000"+Long.toString(cardhao);//0305887634  123B7992strls1=strls1.substring(strls1.length()-10,strls1.length());ctr8h10d.setText(strls1);String strls = "读取扇区数据成功!16进制卡号为:" +serialnumber+"\n转8H10D卡号:"+ strls1;tv.setText(strls);strls= "";for(int i = 0;i < 16;i++){strls1 = "00"+Integer.toHexString(mypiccdata[i] & 0xFF);strls = strls + strls1.substring(strls1.length()-2) +" ";}EditText ctrblock0=findViewById(R.id.Edittextblock0);ctrblock0.setText(strls.toUpperCase());strls= "";for(int i = 16;i < 32;i++){strls1 = "00"+Integer.toHexString(mypiccdata[i] & 0xFF);strls = strls + strls1.substring(strls1.length()-2) +" ";}EditText ctrblock1=findViewById(R.id.Edittextblock1);ctrblock1.setText(strls.toUpperCase());strls= "";for(int i = 32;i < 48;i++){strls1 = "00"+Integer.toHexString(mypiccdata[i] & 0xFF);strls = strls + strls1.substring(strls1.length()-2) +" ";}EditText ctrblock2=findViewById(R.id.Edittextblock2);ctrblock2.setText(strls.toUpperCase());status= ourmifare.piccread((byte)(myareano*4+3),myblockdata);       //读块第三块密钥块if (status==0){strls= "";for(int i = 0;i < 16;i++){strls1 = "00"+Integer.toHexString(myblockdata[i] & 0xFF);strls = strls + strls1.substring(strls1.length()-2) +" ";}EditText ctrblock3=findViewById(R.id.Edittextblock3);ctrblock3.setText(strls.toUpperCase());}else{tv.setText( "读取扇区数据成功!16进制卡号为:" +serialnumber+"\n读第3块时错误:"+Integer.toString(status));}}else{PrintErrInf(status);}mypiccserial = null;mypicckey = null;mypiccdata = null;}//轻松读卡,返回4字节卡号及48字节扇区数据public void piccreadexevery200ms(View view){if(((Button)findViewById(R.id.btnPiccreadex1)).getText() =="启动连续每隔0.2秒读一次卡"){((Button)findViewById(R.id.btnPiccreadex1)).setText("停止连续读卡");timer = new Timer();task = new TimerTask() {@Overridepublic void run() {if(iscontinue) {iscontinue = false;piccreadex(view);iscontinue = true;proctimes++;tv.setText(tv.getText() + "\n执行次数:" + Long.toString(proctimes));}}};timer.schedule(task, 0,200);}else{((Button)findViewById(R.id.btnPiccreadex1)).setText("启动连续每隔0.2秒读一次卡");if(timer != null) {timer.cancel();proctimes = 0;}timer = null;}}//轻松写卡:8区,密码12个F,数据1-48,返回4字节卡号"public void piccwriteex(View view){byte status;//存放返回值byte myareano;//区号byte authmode;//密码类型,用A密码或B密码byte myctrlword;//控制字byte[] mypiccserial = new byte[4];//卡序列号byte[] mypicckey = new byte[6];//密码byte[] mypiccdata = new byte[48];//卡数据缓冲EditText ctruid=findViewById(R.id.TextViewuid);ctruid.setText("");TextView ctr8h10d=findViewById(R.id.TextViewlabel8h10d);ctr8h10d.setText("");tv.setText("");myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN;        //控制字指定,控制字的含义请查看本公司网站提供的动态库说明Spinner ctrauthmode=findViewById(R.id.SpinnerInOutKey);if (ctrauthmode.getSelectedItemId()==1){myctrlword=(byte)(myctrlword + EXTERNKEY);        //选用外部密钥认证}Spinner spls= findViewById(R.id.SpinnerArea);myareano = (byte)(spls.getSelectedItemId());          //指定区号Spinner crtauthabkey=findViewById(R.id.SpinnerSelABKey);authmode =(byte)crtauthabkey.getSelectedItemId();    //大于0表示用A密码认证,推荐用A密码认证EditText ctrauthkey=findViewById(R.id.EdittextAuthkey); //认证密钥String authkeystr=ctrauthkey.getText().toString().trim();if (authkeystr.length()!=12){tv.setText("认证密钥为12位16进制数,请输入正确的卡认证密钥!");return;}for (int i=0;i<6;i++){mypicckey[i]=(byte) Integer.parseInt(authkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误}String[] strArr;EditText ctrblock0=findViewById(R.id.Edittextblock0);String blockstr0=ctrblock0.getText().toString().trim();strArr= blockstr0.split("\\ ");             /*分割接收到的数据后再分析、处理、返回指令 */if (strArr.length!=16){tv.setText("第0块写入数据不足,请输入32位16进制写入数据!");return;}EditText ctrblock1=findViewById(R.id.Edittextblock1);String blockstr1=ctrblock1.getText().toString().trim();strArr= blockstr1.split("\\ ");             /*分割接收到的数据后再分析、处理、返回指令 */if (strArr.length!=16){tv.setText("第1块写入数据不足,请输入32位16进制写入数据!");return;}EditText ctrblock2=findViewById(R.id.Edittextblock2);String blockstr2=ctrblock2.getText().toString().trim();strArr= blockstr2.split("\\ ");             /*分割接收到的数据后再分析、处理、返回指令 */if (strArr.length!=16){tv.setText("第2块写入数据不足,请输入32位16进制写入数据!");return;}String writedatastr=blockstr0+" "+blockstr1+" "+blockstr2;strArr= writedatastr.split("\\ ");for(int p=0;p<strArr.length;p++) {mypiccdata[p]=(byte)(Integer.parseInt(strArr[p],16));}status = ourmifare.piccwriteexfm11rf32(myctrlword,mypiccserial,myareano,authmode,mypicckey,mypiccdata);if(status == 0){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());}serialnumber=serialnumber.toUpperCase();ctruid.setText(serialnumber);long cardhao;cardhao = mypiccserial[3] & 0xff;cardhao *= 256;cardhao += mypiccserial[2] & 0xff;cardhao *= 256;cardhao += mypiccserial[1] & 0xff;cardhao *= 256;cardhao += mypiccserial[0] & 0xff;String strls1 = "000000000"+Long.toString(cardhao);//0305887634  123B7992strls1=strls1.substring(strls1.length()-10,strls1.length());ctr8h10d.setText(strls1);String strls = "扇区写入数据成功!16进制卡号为:" +serialnumber+"\n转8H10D卡号:"+ strls1;tv.setText(strls);}else{PrintErrInf(status);}}//改单区密码:8区,密码12个F,改成12个F,返回4字节卡号"public void piccchangesinglekeyex(View view){byte status;//存放返回值byte myareano;//区号byte authmode;//密码类型,用A密码或B密码byte myctrlword=0;//控制字byte[] mypiccserial = new byte[4];//卡序列号byte[] mypicckeyold = new byte[6];//密码byte[] mypicckeynew = new byte[17];//卡数据缓冲EditText ctruid=findViewById(R.id.TextViewuid);ctruid.setText("");TextView ctr8h10d=findViewById(R.id.TextViewlabel8h10d);ctr8h10d.setText("");tv.setText("");Spinner ctrauthmode=findViewById(R.id.SpinnerInOutKey);if (ctrauthmode.getSelectedItemId()==1){myctrlword=(byte)(myctrlword + EXTERNKEY);        //选用外部密钥认证}Spinner spls= findViewById(R.id.SpinnerArea);myareano = (byte)(spls.getSelectedItemId());          //指定区号Spinner crtauthabkey=findViewById(R.id.SpinnerSelABKey);authmode =(byte)crtauthabkey.getSelectedItemId();    //大于0表示用A密码认证,推荐用A密码认证EditText ctrauthkey=findViewById(R.id.EdittextAuthkey); //认证密钥String authkeystr=ctrauthkey.getText().toString().trim();if (authkeystr.length()!=12){tv.setText("认证密钥为12位16进制数,请输入正确的卡认证密钥!");return;}for (int i=0;i<6;i++){mypicckeyold[i]=(byte) Integer.parseInt(authkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误}//新A密钥EditText ctrkeya=findViewById(R.id.Edittextnewkeya);String newkeyastr=ctrkeya.getText().toString().trim();if (newkeyastr.length()!=12){tv.setText("新A密钥输入错误,请输入12位16进制新A密钥!");return;}for (int i=0;i<6;i++){mypicckeynew[i]=(byte)Integer.parseInt(newkeyastr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误}//新控制位EditText ctrnewctr=findViewById(R.id.Edittextnewctr);String newctrstr=ctrnewctr.getText().toString().trim();if (newctrstr.length()!=8){tv.setText("新控制字输入错误,请输入8位16进制控制字!");return;}for (int i=0;i<4;i++){mypicckeynew[6+i]=(byte)Integer.parseInt(newctrstr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误}//新B密钥EditText ctrkeyb=findViewById(R.id.Edittextnewkeyb);String newkeybstr=ctrkeyb.getText().toString().trim();if (newkeyastr.length()!=12){tv.setText("新B密钥输入错误,请输入12位16进制新B密钥!");return;}for (int i=0;i<6;i++){mypicckeynew[10+i]=(byte)Integer.parseInt(newkeybstr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误}//选项之Spinner ctrnewkey=findViewById(R.id.SpinnerchangeKey);byte selechangkey=(byte)ctrnewkey.getSelectedItemId();switch (selechangkey){case 0:mypicckeynew[16] = (byte)0x00;//为0表示只改A官密码break;case 1:mypicckeynew[16] = (byte)0x01;//为1表示改A密码同时也改B密码break;case 2:mypicckeynew[16] = (byte)0x03;//为3表示改AB密码及访问位,警示:不要轻易改访问位,FF078069 为出可使用7F 07 88 69控制break;}status = ourmifare.piccchangesinglekeyexfm11rf32(myctrlword,mypiccserial,myareano,authmode,mypicckeyold,mypicckeynew);if(status == 0){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());}serialnumber=serialnumber.toUpperCase();ctruid.setText(serialnumber);long cardhao;cardhao = mypiccserial[3] & 0xff;cardhao *= 256;cardhao += mypiccserial[2] & 0xff;cardhao *= 256;cardhao += mypiccserial[1] & 0xff;cardhao *= 256;cardhao += mypiccserial[0] & 0xff;String strls1 = "000000000"+Long.toString(cardhao);//0305887634  123B7992strls1=strls1.substring(strls1.length()-10,strls1.length());ctr8h10d.setText(strls1);String strls = "更改卡片密钥成功!16进制卡号为:" +serialnumber+"\n转8H10D卡号:"+ strls1;tv.setText(strls);}else{PrintErrInf(status);}}public void PrintErrInf(byte errcode) {String dispstr;switch(errcode){case 1:dispstr="错误代码:1,0~2块都没读出来,可能刷卡太块。但卡序列号已被读出来!";break;case 2:dispstr="错误代码:2,第0块已被读出,但1~2块读取失败。卡序列号已被读出来!";break;case 3:dispstr="错误代码:3,第0、1块已被读出,但2块读取失败。卡序列号已被读出来!";break;case 8:dispstr="错误代码:8,未寻到卡,请重新拿开卡后再放到感应区!";break;case 9:dispstr="错误代码:9,有多张卡在感应区,寻卡过程中防冲突失败,读序列吗错误!";break;case 10:dispstr="错误代码:10,该卡可能已被休眠,无法选中卡片!";break;case 11:dispstr="错误代码:11,密码装载失败!";break;case 12:dispstr="错误代码:12,卡片密码认证失败!";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:dispstr="错误代码:25,发送字数不够!";break;case 26:dispstr="错误代码:26,发送的CRC错!";break;case 27:dispstr="错误代码:27,接收的字数不够!";break;case 28:dispstr="错误代码:28,接收的CRC错!";break;default:dispstr="未知错误,错误代码:"+Integer.toString(errcode);break;}tv.setText(dispstr);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/341784.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

定时启动 × 集简云数据采集,定时采集小红书话题笔记同步到数据表,对话题数据快速分析与监控

1 场景描述 在自媒体时代&#xff0c;众多企业将小红书视为关键的品牌构建平台。通过分析与跟进用户喜爱的话题&#xff0c;企业能够洞察市场趋势和用户需求&#xff0c;从而精准制定营销策略。然而&#xff0c;快速且精确地采集大量数据并非易事。 每个企业的自媒体团队都身兼…

mysql原理--redo日志2

1.redo日志文件 1.1.redo日志刷盘时机 我们前边说 mtr 运行过程中产生的一组 redo 日志在 mtr 结束时会被复制到 log buffer 中&#xff0c;可是这些日志总在内存里呆着也不是个办法&#xff0c;在一些情况下它们会被刷新到磁盘里&#xff0c;比如&#xff1a; (1). log buffer…

【办公技巧】Word中如何对齐选择题中的ABCD选项?

使用word文件制作试卷&#xff0c;如何将ABCD选项全部设置对齐&#xff1f;除了一直按空格或者Tab键以外&#xff0c;还有其他方法吗&#xff1f;今天分享如何将ABCD选项对齐。 首先&#xff0c;我们打开【替换和查找】&#xff0c;在查找内容输入空格&#xff0c;然后点击全部…

jsPlumb、mxGraph和Antv x6实现流程图选型

解决方案 结合我们项目以及主流解决方案&#xff0c;提供以下几种方案&#xff1a; 序号技术栈性质是否开源说明1jsPlumb国外框架社区版、商业版中台项目现有方案2mxGraph国外框架开源比较有名的开源绘图网站draw.io &#xff08;和processOn类似&#xff09;&#xff0c;使用…

【Python】编程练习的解密与实战(三)

​&#x1f308;个人主页&#xff1a;Sarapines Programmer&#x1f525; 系列专栏&#xff1a;《Python | 编程解码》⏰诗赋清音&#xff1a;云生高巅梦远游&#xff0c; 星光点缀碧海愁。 山川深邃情难晤&#xff0c; 剑气凌云志自修。 目录 &#x1fa90;1. 初识Python &a…

工程机械比例阀电流采集方案——IPEhub2与IPEmotion APP

自从国家实施一带一路和新基建计划以来&#xff0c;工程机械的需求量呈现出快速增长的趋势。而关于工程机械&#xff0c;其比例阀的控制问题不容忽视。比例阀是一种新型的液压控制装置——在普通压力阀、流量阀和方向阀上&#xff0c;用比例电磁铁替代原有的控制部分&#xff0…

【漏洞复现】先锋WEB燃气收费系统文件上传漏洞 1day

漏洞描述 /AjaxService/Upload.aspx 存在任意文件上传漏洞 免责声明 技术文章仅供参考,任何个人和组织使用网络应当遵守宪法法律,遵守公共秩序,尊重社会公德,不得利用网络从事危害国家安全、荣誉和利益,未经授权请勿利用文章中的技术资料对任何计算机系统进行入侵操作…

MySQL中datetime和timestamp的区别

datetime和timestamp的区别 相同点: 存储格式相同 datetime和timestamp两者的时间格式都是YYYY-MM-DD HH:MM:SS 不同点: 存储范围不同. datetime的范围是1000-01-01到9999-12-31. 而timestamp是从1970-01-01到2038-01-19, 即后者的时间范围很小. 与时区关系. datetime是存储…

虹科新闻丨LIBERO医药冷链PDF温度计完成2024年航空安全鉴定,可安全空运!

来源&#xff1a;虹科环境监测技术 虹科新闻丨LIBERO医药冷链PDF温度计完成2024年航空安全鉴定&#xff0c;可安全空运&#xff01; 原文链接&#xff1a;https://mp.weixin.qq.com/s/XHT4kU27opeKJneYO0WqrA 欢迎关注虹科&#xff0c;为您提供最新资讯&#xff01; 虹科LIBE…

1071: 分解质因子

题目描述 将一个正整数分解质因数&#xff0c;例如&#xff0c;输入90&#xff0c;输出2 3 3 5。 输入 输入一个正整数n&#xff08;2<n<2000&#xff09;。 输出 从小到大输出n的所有质因子&#xff0c;每两个数之间空一格。 样例输入 20 样例输出 2 2 5 提…

Golang defer 使用及面试常见的坑

前言 defer是Golang中一个常用的关键字&#xff0c;通常用来做一些收尾工作。比如开启了一个东西&#xff0c;就顺手defer中关闭。对于面试&#xff0c;defer也算一个高频考点&#xff0c;尤其是他的许多个坑&#xff0c;因此本文主要复习一下defer的用法。 多个defer的执行顺…

查询分组功能:发布不同班级查询,支持独立分组链接和二维码

期末考试已经陆续开始&#xff0c;老师如果负责多个班级或年级&#xff0c;如何在一个账号中实现不同班级单独一个查询页呢&#xff1f; 易查分的【查询分组功能】就可实现&#xff0c;本次就来介绍如何使用此功能。 &#x1f4d6;案例&#xff1a;负责相关工作的老师&#xff…