安卓Android studio读写EM4305卡源码

本示例使用的发卡器: https://item.taobao.com/item.htm?id=718720660087&spm=a1z10.5-c.w4002-21818769070.15.57dc6f89txUhXE

<?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=".EM4305Activity"><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="EM4305卡测试页  "android:textColor="@color/white"android:textSize="16sp"android:gravity="center_horizontal|right|center"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="@+id/TextViewlabelDispleft"app:layout_constraintTop_toTopOf="parent" /></androidx.appcompat.widget.Toolbar><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="5dp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/toolbar"app:layout_constraintBottom_toTopOf="@+id/sample_text"android:scrollbars="horizontal"><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="3dp"><CheckBoxandroid:id="@+id/chkPwdEM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="带密码操作"android:textSize="12sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent" /><EditTextandroid:id="@+id/editTextPwdEM4305"android:layout_width="80dp"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:background="@drawable/shape4border"android:digits="0123456789ABCDEFabcdef"android:gravity="center"android:inputType="textCapCharacters"android:maxLength="8"android:maxLines="1"android:text="00000000"android:textSize="14sp"app:layout_constraintBottom_toBottomOf="@+id/chkPwdEM4305"app:layout_constraintLeft_toRightOf="@+id/chkPwdEM4305"app:layout_constraintTop_toTopOf="@+id/chkPwdEM4305"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:text="(十六进制)"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/chkPwdEM4305"app:layout_constraintLeft_toRightOf="@+id/editTextPwdEM4305"app:layout_constraintTop_toTopOf="@+id/chkPwdEM4305" /><CheckBoxandroid:id="@+id/chkUidNeedEM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="仅操作指定卡号的卡"android:textSize="12sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/chkPwdEM4305" /><EditTextandroid:id="@+id/editTextUidEM4305"android:layout_width="80dp"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:background="@drawable/shape4border"android:digits="0123456789ABCDEFabcdef"android:gravity="center"android:inputType="textCapCharacters"android:maxLength="8"android:maxLines="1"android:text="00000000"android:textSize="14sp"app:layout_constraintBottom_toBottomOf="@+id/chkUidNeedEM4305"app:layout_constraintLeft_toRightOf="@+id/chkUidNeedEM4305"app:layout_constraintTop_toTopOf="@+id/chkUidNeedEM4305"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:text="(十六进制)"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/chkUidNeedEM4305"app:layout_constraintLeft_toRightOf="@+id/editTextUidEM4305"app:layout_constraintTop_toTopOf="@+id/chkUidNeedEM4305" /><TextViewandroid:id="@+id/TextViewlabel6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="返回数据:"android:textSize="12sp"app:layout_constraintBottom_toTopOf="@+id/TextViewRetDataEM4305"app:layout_constraintLeft_toLeftOf="parent" /><TextViewandroid:id="@+id/TextViewRetDataEM4305"android:layout_width="fill_parent"android:layout_height="wrap_content"android:background="@drawable/shape4border"android:padding="3dp"android:text=""android:textSize="12sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/btnRetDataClrEM4305"/><Buttonandroid:id="@+id/btnRetDataClrEM4305"android:layout_width="wrap_content"android:layout_height="40dp"android:onClick="textclear4305"android:text="清空数据"android:textSize="11sp"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@+id/editTextUidEM4305" /><Buttonandroid:id="@+id/btnPwdChangeEM4305"android:layout_width="wrap_content"android:layout_height="40dp"android:onClick="changepwd4305"android:text="修改卡密码"android:textSize="11sp"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@+id/TextViewRetDataEM4305" /><TextViewandroid:id="@+id/TextViewlabel7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="新密码:"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/btnPwdChangeEM4305"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="@+id/btnPwdChangeEM4305"/><EditTextandroid:id="@+id/editTextPwdNewEM4305"android:layout_width="80dp"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:background="@drawable/shape4border"android:digits="0123456789ABCDEFabcdef"android:gravity="center"android:inputType="textCapCharacters"android:maxLength="8"android:maxLines="1"android:text="00000000"android:textSize="14sp"app:layout_constraintBottom_toBottomOf="@+id/btnPwdChangeEM4305"app:layout_constraintLeft_toRightOf="@+id/TextViewlabel7"app:layout_constraintTop_toTopOf="@+id/btnPwdChangeEM4305" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:text="(十六进制)"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/editTextPwdNewEM4305"app:layout_constraintLeft_toRightOf="@+id/editTextPwdNewEM4305"app:layout_constraintTop_toTopOf="@+id/editTextPwdNewEM4305" /><CheckBoxandroid:id="@+id/chkB0EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块0"android:textSize="11sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/btnPwdChangeEM4305" /><CheckBoxandroid:id="@+id/chkB1EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块1(UID)"android:textSize="11sp"app:layout_constraintBottom_toBottomOf="@+id/chkB0EM4305"app:layout_constraintLeft_toRightOf="@+id/chkB0EM4305" /><CheckBoxandroid:id="@+id/chkB3EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块3"android:textSize="11sp"app:layout_constraintBottom_toBottomOf="@+id/chkB0EM4305"app:layout_constraintLeft_toRightOf="@+id/chkB1EM4305" /><CheckBoxandroid:id="@+id/chkB4EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块4(配置)"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/chkB0EM4305"app:layout_constraintLeft_toRightOf="@+id/chkB3EM4305" /><CheckBoxandroid:id="@+id/chkB5EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块5"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/chkB0EM4305"app:layout_constraintLeft_toRightOf="@+id/chkB4EM4305" /><CheckBoxandroid:id="@+id/chkB6EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块6"android:textSize="11sp"app:layout_constraintBottom_toBottomOf="@+id/chkB0EM4305"app:layout_constraintLeft_toRightOf="@+id/chkB5EM4305" /><CheckBoxandroid:id="@+id/chkB7EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块7"android:textSize="12sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/chkB0EM4305" /><CheckBoxandroid:id="@+id/chkB8EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块8"android:textSize="11sp"app:layout_constraintBottom_toBottomOf="@+id/chkB7EM4305"app:layout_constraintLeft_toRightOf="@+id/chkB7EM4305" /><CheckBoxandroid:id="@+id/chkB9EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块9"android:textSize="11sp"app:layout_constraintBottom_toBottomOf="@+id/chkB8EM4305"app:layout_constraintLeft_toRightOf="@+id/chkB8EM4305" /><CheckBoxandroid:id="@+id/chkB10EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块10"android:textSize="11sp"app:layout_constraintBottom_toBottomOf="@+id/chkB8EM4305"app:layout_constraintLeft_toRightOf="@+id/chkB9EM4305" /><CheckBoxandroid:id="@+id/chkB11EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块11"android:textSize="11sp"app:layout_constraintBottom_toBottomOf="@+id/chkB8EM4305"app:layout_constraintLeft_toRightOf="@+id/chkB10EM4305" /><CheckBoxandroid:id="@+id/chkB12EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块12"android:textSize="11sp"app:layout_constraintBottom_toBottomOf="@+id/chkB8EM4305"app:layout_constraintLeft_toRightOf="@+id/chkB11EM4305" /><CheckBoxandroid:id="@+id/chkB13EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块13"android:textSize="11sp"app:layout_constraintBottom_toBottomOf="@+id/chkB8EM4305"app:layout_constraintLeft_toRightOf="@+id/chkB12EM4305" /><CheckBoxandroid:id="@+id/chkB14EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块14"android:textSize="11sp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/chkB8EM4305" /><CheckBoxandroid:id="@+id/chkB15EM4305"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_marginLeft="2dp"android:checked="false"android:text="块15"android:textSize="11sp"app:layout_constraintBottom_toBottomOf="@+id/chkB14EM4305"app:layout_constraintLeft_toRightOf="@+id/chkB14EM4305" /><TextViewandroid:id="@+id/TextViewlabel10"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/chkB14EM4305" /><EditTextandroid:id="@+id/editTextDataEM4305"android:layout_width="0dp"android:layout_height="40dp"android:background="@drawable/shape4border"android:digits="0123456789ABCDEFabcdef"android:gravity="left"android:inputType="textCapCharacters"android:maxLength="128"android:padding="3dp"android:text="11111111"android:textSize="12sp"app:layout_constraintLeft_toLeftOf="@+id/TextViewRetDataEM4305"app:layout_constraintRight_toRightOf="@+id/TextViewRetDataEM4305"app:layout_constraintTop_toBottomOf="@+id/TextViewlabel10" /><Buttonandroid:id="@+id/btnReadEM4305"android:layout_width="wrap_content"android:layout_height="40dp"android:layout_marginRight="20dp"android:onClick="read4305"android:text="读卡"android:textSize="11sp"app:layout_constraintBottom_toBottomOf="@+id/btnWriteEM4305"app:layout_constraintRight_toLeftOf="@+id/btnWriteEM4305" /><Buttonandroid:id="@+id/btnWriteEM4305"android:layout_width="wrap_content"android:layout_height="40dp"android:onClick="write4305"android:text="写卡"android:textSize="11sp"app:layout_constraintBottom_toTopOf="@+id/editTextDataEM4305"app:layout_constraintRight_toRightOf="parent" /><TextViewandroid:id="@+id/TextViewlabel11"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="数据速率:"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/SpinnerDateRateEM4305"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="@+id/SpinnerDateRateEM4305" /><Spinnerandroid:id="@+id/SpinnerDateRateEM4305"android:layout_width="wrap_content"android:layout_height="wrap_content"android:entries="@array/datarateEM4305"android:spinnerMode="dropdown"android:theme="@style/my_spinner_style"app:layout_constraintLeft_toRightOf="@+id/TextViewlabel11"app:layout_constraintTop_toBottomOf="@+id/editTextDataEM4305"></Spinner><TextViewandroid:id="@+id/TextViewlabel12"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="调制模式:"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/SpinnerDateRateEM4305"app:layout_constraintRight_toLeftOf="@+id/SpinnerEncoderEM4305"app:layout_constraintTop_toTopOf="@+id/SpinnerDateRateEM4305" /><Spinnerandroid:id="@+id/SpinnerEncoderEM4305"android:layout_width="wrap_content"android:layout_height="wrap_content"android:entries="@array/EncoderEM4305"android:spinnerMode="dropdown"android:theme="@style/my_spinner_style"app:layout_constraintBottom_toBottomOf="@+id/SpinnerDateRateEM4305"app:layout_constraintRight_toRightOf="parent"></Spinner><Spinnerandroid:id="@+id/SpinnerLoginModeEM4305"android:layout_width="0dp"android:layout_height="wrap_content"android:entries="@array/loginmodeEM4305"android:spinnerMode="dropdown"android:theme="@style/my_spinner_style"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@+id/SpinnerEncoderEM4305"></Spinner><TextViewandroid:id="@+id/TextViewlabel13"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="自动发送最大块:"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/editTextMaxBlockEM4305"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="@+id/editTextMaxBlockEM4305" /><EditTextandroid:id="@+id/editTextMaxBlockEM4305"android:layout_width="25dp"android:layout_height="wrap_content"android:background="@drawable/shape4border"android:digits="0123456789"android:gravity="left"android:inputType="textCapCharacters"android:maxLength="2"android:paddingLeft="3dp"android:text="6"android:textSize="12sp"app:layout_constraintLeft_toRightOf="@+id/TextViewlabel13"app:layout_constraintTop_toBottomOf="@+id/SpinnerLoginModeEM4305" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="范围:5-13块,时序从5块至最大块"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/editTextMaxBlockEM4305"app:layout_constraintLeft_toRightOf="@+id/editTextMaxBlockEM4305"app:layout_constraintTop_toTopOf="@+id/editTextMaxBlockEM4305" /><TextViewandroid:id="@+id/TextViewlabel14"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="配置值:"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/editTextConfigWordEM4305"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="@+id/editTextConfigWordEM4305" /><EditTextandroid:id="@+id/editTextConfigWordEM4305"android:layout_width="68dp"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:background="@drawable/shape4border"android:digits="0123456789ABCDEFabcdef"android:gravity="left"android:inputType="textCapCharacters"android:maxLength="8"android:paddingLeft="3dp"android:text="5F800100"android:textSize="12sp"app:layout_constraintLeft_toRightOf="@+id/TextViewlabel14"app:layout_constraintTop_toBottomOf="@+id/editTextMaxBlockEM4305" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:text="(十六进制)"android:textSize="12sp"app:layout_constraintBottom_toBottomOf="@+id/editTextConfigWordEM4305"app:layout_constraintLeft_toRightOf="@+id/editTextConfigWordEM4305"app:layout_constraintTop_toTopOf="@+id/editTextConfigWordEM4305" /><Buttonandroid:id="@+id/btnDefalutEM4305"android:layout_width="wrap_content"android:layout_height="40dp"android:layout_marginRight="20dp"android:onClick="default4305"android:text="← 默认值"android:textSize="11sp"app:layout_constraintBottom_toBottomOf="@+id/btnInitEM4305"app:layout_constraintRight_toLeftOf="@+id/btnInitEM4305" /><Buttonandroid:id="@+id/btnInitEM4305"android:layout_width="wrap_content"android:layout_height="40dp"android:onClick="init4305"android:text="设定配置"android:textSize="12sp"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@+id/TextViewlabel14" /></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.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;import com.reader.ouridr;
import com.reader.ourmifare;
public class EM4305Activity extends AppCompatActivity {private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_em4305);androidx.appcompat.widget.Toolbar toolbar=findViewById(R.id.toolbar);setSupportActionBar(toolbar);tv = findViewById(R.id.sample_text);tv.setText("操作结果");//增加输入监听事件// EM4305((EditText)findViewById(R.id.editTextMaxBlockEM4305)).setOnFocusChangeListener(new View.OnFocusChangeListener(){@Overridepublic void onFocusChange(View v,boolean FocusState) {if(FocusState){//进入焦点}else{//离开焦点updateset4305();}}});//数据速率Spinner spls;spls = findViewById(R.id.SpinnerDateRateEM4305);spls.setSelection(2);//RF/16spls.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {updateset4305();}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}});//调制模式((Spinner)findViewById(R.id.SpinnerEncoderEM4305)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {updateset4305();}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}});//加密模式((Spinner)findViewById(R.id.SpinnerLoginModeEM4305)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {updateset4305();}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}});}@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();}public void textclear4305(View view){TextView tvls;tvls = findViewById(R.id.TextViewRetDataEM4305);tvls.setText("");}public void changepwd4305(View view){byte i;byte j;byte status;//存放返回值byte myctrlword;//控制字byte[] oldpicckey = new byte[4];//旧密码byte[] mypiccserial = new byte[4];//卡序列号byte[] newpicckey = new byte[4];//新密码myctrlword = 0; //NEEDSERIAL:需要只对指定系列号的卡操作,NEEDKEY:需要用密码认证,LOCKBIT:锁定块,KEYENABLE:启用本卡的密码功能String strls;CheckBox cbls;EditText etls;cbls = findViewById(R.id.chkPwdEM4305);if(!cbls.isChecked()) {//本次操作需要密码验证tv.setText("EM4305卡修改卡密码必须带密码操作");return;}etls = findViewById(R.id.editTextPwdEM4305);strls = etls.getText().toString().trim();if(strls.length() < 8){tv.setText("旧密码长度不足8位");return;}for (i = 0; i < 4; i++) {oldpicckey[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误}myctrlword += ouridr.NEEDKEY;cbls = findViewById(R.id.chkUidNeedEM4305);if(cbls.isChecked()) {//仅操作指定卡号的卡etls = findViewById(R.id.editTextUidEM4305);strls = etls.getText().toString().trim();if(strls.length() < 8){tv.setText("卡号长度不足8位");return;}for (i = 0; i < 4; i++) {mypiccserial[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误}myctrlword += ouridr.NEEDSERIAL;}etls = findViewById(R.id.editTextPwdNewEM4305);strls = etls.getText().toString().trim();if(strls.length() < 8){tv.setText("新密码长度不足8位!");return;}for (i = 0; i < 4; i++) {newpicckey[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误}status = ouridr.em4305changekey(myctrlword,mypiccserial,oldpicckey,newpicckey);if(status == 0){strls = "卡号[";strls += String.format("%02X%02X%02X%02X",mypiccserial[0],mypiccserial[1],mypiccserial[2],mypiccserial[3]);strls += "]";TextView tvls = findViewById(R.id.TextViewRetDataEM4305);tvls.setText(strls);strls = "EM4305卡修改卡密码成功";ouridr.beep(38);}else{if(status == 8){strls = "请将卡放在感应区";}else if(status == 23){strls = "错误代码:23,读卡器未插入";}else if(status == 1){strls = "错误代码:1,卡放得远 或 需要密码才能写卡";}else if(status == 12){strls = "错误代码:12,卡放得远 或 密码错误";}else{strls = "错误代码:" + Integer.toString(status);}}tv.setText(strls);}public void read4305(View view){byte i;byte status;//存放返回值byte myctrlword;//控制字byte[] oldpicckey = new byte[4];//密码byte[] mypiccserial = new byte[4];//卡序列号byte[] mypiccdata = new byte[64];//读卡数据缓冲:卡无线转输分频比、卡内容长度(字节数),及最多返回12块的数据byte[] mypiccblockflag = new byte[2];//指定读哪一块myctrlword = 0; //NEEDSERIAL:需要只对指定系列号的卡操作,NEEDKEY:需要用密码认证,LOCKBIT:锁定块,KEYENABLE:启用本卡的密码功能String strls;CheckBox cbls;EditText etls;cbls = findViewById(R.id.chkPwdEM4305);if(cbls.isChecked()) {//本次操作需要密码验证etls = findViewById(R.id.editTextPwdEM4305);strls = etls.getText().toString().trim();if(strls.length() < 8){tv.setText("密码长度不足8位");return;}for (i = 0; i < 4; i++) {oldpicckey[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误}myctrlword += ouridr.NEEDKEY;}cbls = findViewById(R.id.chkUidNeedEM4305);if(cbls.isChecked()) {//仅操作指定卡号的卡etls = findViewById(R.id.editTextUidEM4305);strls = etls.getText().toString().trim();if(strls.length() < 8){tv.setText("卡号长度不足8位");return;}for (i = 0; i < 4; i++) {mypiccserial[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误}myctrlword += ouridr.NEEDSERIAL;}//操作块标志mypiccblockflag[0] = 0;cbls = findViewById(R.id.chkB0EM4305);if(cbls.isChecked()) {mypiccblockflag[0] += 1;}cbls = findViewById(R.id.chkB1EM4305);if(cbls.isChecked()) {mypiccblockflag[0] += 2;}cbls = findViewById(R.id.chkB3EM4305);if(cbls.isChecked()) {mypiccblockflag[0] += 8;}cbls = findViewById(R.id.chkB4EM4305);if(cbls.isChecked()) {mypiccblockflag[0] += 16;}cbls = findViewById(R.id.chkB5EM4305);if(cbls.isChecked()) {mypiccblockflag[0] += 32;}cbls = findViewById(R.id.chkB6EM4305);if(cbls.isChecked()) {mypiccblockflag[0] += 64;}cbls = findViewById(R.id.chkB7EM4305);if(cbls.isChecked()) {mypiccblockflag[0] += 128;}mypiccblockflag[1] = 0;cbls = findViewById(R.id.chkB8EM4305);if(cbls.isChecked()) {mypiccblockflag[1] += 1;}cbls = findViewById(R.id.chkB9EM4305);if(cbls.isChecked()) {mypiccblockflag[1] += 2;}cbls = findViewById(R.id.chkB10EM4305);if(cbls.isChecked()) {mypiccblockflag[1] += 4;}cbls = findViewById(R.id.chkB11EM4305);if(cbls.isChecked()) {mypiccblockflag[1] += 8;}cbls = findViewById(R.id.chkB12EM4305);if(cbls.isChecked()) {mypiccblockflag[1] += 16;}cbls = findViewById(R.id.chkB13EM4305);if(cbls.isChecked()) {mypiccblockflag[1] += 32;}cbls = findViewById(R.id.chkB14EM4305);if(cbls.isChecked()) {mypiccblockflag[1] += 64;}cbls = findViewById(R.id.chkB15EM4305);if(cbls.isChecked()) {mypiccblockflag[1] += 128;}status = ouridr.em4305read(myctrlword,mypiccserial,oldpicckey,mypiccblockflag,mypiccdata);if(status == 0){strls = "卡无线转输分频比[" + Integer.toString(mypiccdata[0]) + "],卡号[";strls += String.format("%02X%02X%02X%02X",mypiccserial[0],mypiccserial[1],mypiccserial[2],mypiccserial[3]);strls += "],卡数据[";for (i = 0; i < mypiccdata[1]; i++) {strls += String.format("%02X",mypiccdata[i+2]);}strls += "]";TextView tvls = findViewById(R.id.TextViewRetDataEM4305);tvls.setText(strls);strls = "读EM4305卡成功";ouridr.beep(38);}else{if(status == 8){strls = "请将卡放在感应区";}else if(status == 23){strls = "错误代码:23,读卡器未插入";}else if(status == 4){strls = "错误代码:4,卡放得远 或 需要密码才能读卡";}else if(status == 12){strls = "错误代码:12,卡放得远 或 密码错误";}else{strls = "错误代码:" + Integer.toString(status);}}tv.setText(strls);}public void write4305(View view){byte i;byte j;byte status;//存放返回值byte myctrlword;//控制字byte[] oldpicckey = new byte[4];//密码byte[] mypiccserial = new byte[4];//卡序列号byte[] mypiccdata = new byte[64];//读卡数据缓冲:卡无线转输分频比、卡内容长度(字节数),最多12块的数据byte[] mypiccblockflag = new byte[2];//指定读哪一块myctrlword = 0; //NEEDSERIAL:需要只对指定系列号的卡操作,NEEDKEY:需要用密码认证,LOCKBIT:锁定块,KEYENABLE:启用本卡的密码功能String strls;CheckBox cbls;EditText etls;cbls = findViewById(R.id.chkPwdEM4305);if(cbls.isChecked()) {//本次操作需要密码验证etls = findViewById(R.id.editTextPwdEM4305);strls = etls.getText().toString().trim();if(strls.length() < 8){tv.setText("密码长度不足8位");return;}for (i = 0; i < 4; i++) {oldpicckey[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误}myctrlword += ouridr.NEEDKEY;}cbls = findViewById(R.id.chkUidNeedEM4305);if(cbls.isChecked()) {//仅操作指定卡号的卡etls = findViewById(R.id.editTextUidEM4305);strls = etls.getText().toString().trim();if(strls.length() < 8){tv.setText("卡号长度不足8位");return;}for (i = 0; i < 4; i++) {mypiccserial[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误}myctrlword += ouridr.NEEDSERIAL;}//操作块标志j = 0;mypiccblockflag[0] = 0;cbls = findViewById(R.id.chkB0EM4305);if(cbls.isChecked()) {mypiccblockflag[0] += 1;j++;}cbls = findViewById(R.id.chkB1EM4305);if(cbls.isChecked()) {tv.setText("块1为只读,不允许写");return;}cbls = findViewById(R.id.chkB3EM4305);if(cbls.isChecked()) {mypiccblockflag[0] += 8;j++;}cbls = findViewById(R.id.chkB4EM4305);if(cbls.isChecked()) {tv.setText("块4为配置区,只能在初始化函数中操作");return;}cbls = findViewById(R.id.chkB5EM4305);if(cbls.isChecked()) {mypiccblockflag[0] += 32;j++;}cbls = findViewById(R.id.chkB6EM4305);if(cbls.isChecked()) {mypiccblockflag[0] += 64;j++;}cbls = findViewById(R.id.chkB7EM4305);if(cbls.isChecked()) {mypiccblockflag[0] += 128;j++;}mypiccblockflag[1] = 0;cbls = findViewById(R.id.chkB8EM4305);if(cbls.isChecked()) {mypiccblockflag[1] += 1;j++;}cbls = findViewById(R.id.chkB9EM4305);if(cbls.isChecked()) {mypiccblockflag[1] += 2;j++;}cbls = findViewById(R.id.chkB10EM4305);if(cbls.isChecked()) {mypiccblockflag[1] += 4;j++;}cbls = findViewById(R.id.chkB11EM4305);if(cbls.isChecked()) {mypiccblockflag[1] += 8;j++;}cbls = findViewById(R.id.chkB12EM4305);if(cbls.isChecked()) {mypiccblockflag[1] += 16;j++;}cbls = findViewById(R.id.chkB13EM4305);if(cbls.isChecked()) {mypiccblockflag[1] += 32;j++;}cbls = findViewById(R.id.chkB14EM4305);if(cbls.isChecked()) {tv.setText("块14为锁定标志块,只能在初始化函数中操作");return;}cbls = findViewById(R.id.chkB15EM4305);if(cbls.isChecked()) {tv.setText("块15为锁定标志块,只能在初始化函数中操作");return;}//写卡数据准备if(j == 0){tv.setText("请先选择需要写入的块");return;}etls = findViewById(R.id.editTextDataEM4305);strls = etls.getText().toString().trim();if(strls.length() < (j*8)){tv.setText("写入数据长度不足,请补足数据!");return;}for (i = 0; i < (j*4); i++) {mypiccdata[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误}status = ouridr.em4305write(myctrlword,mypiccserial,oldpicckey,mypiccblockflag,mypiccdata);if(status == 0){strls = "卡号[";strls += String.format("%02X%02X%02X%02X",mypiccserial[0],mypiccserial[1],mypiccserial[2],mypiccserial[3]);strls += "]";TextView tvls = findViewById(R.id.TextViewRetDataEM4305);tvls.setText(strls);strls = "写EM4305卡成功";ouridr.beep(38);}else{if(status == 8){strls = "请将卡放在感应区";}else if(status == 23){strls = "错误代码:23,读卡器未插入";}else if(status == 1){strls = "错误代码:1,卡放得远 或 需要密码才能写卡";}else if(status == 12){strls = "错误代码:12,卡放得远 或 密码错误";}else{strls = "错误代码:" + Integer.toString(status);}}tv.setText(strls);}public void default4305(View view){EditText etls;Spinner spls;etls = findViewById(R.id.editTextConfigWordEM4305);etls.setText("5F800100");etls = findViewById(R.id.editTextMaxBlockEM4305);etls.setText("6");spls = findViewById(R.id.SpinnerDateRateEM4305);//数据速率spls.setSelection(2);spls = findViewById(R.id.SpinnerEncoderEM4305);//调制模式spls.setSelection(0);spls = findViewById(R.id.SpinnerLoginModeEM4305);//加密模式spls.setSelection(0);}public void updateset4305(){EditText etls;EditText etMaxBlock;Spinner spls;String strls;byte i;byte[] Configdata = new byte[4];//配置值etls = findViewById(R.id.editTextConfigWordEM4305);strls = etls.getText().toString().trim();if(strls.length() < 8){strls = "5F800100";Configdata[0] = 0x5F;Configdata[1] = (byte)0x80;Configdata[2] = 0x01;Configdata[3] = 0x00;}else {for (i = 1; i < 4; i++) {Configdata[i] = (byte)Integer.parseInt(strls.substring(i * 2, i * 2 + 2),16);//只有用Integer.parseInt才能杜绝大于128时的错误}}spls = findViewById(R.id.SpinnerDateRateEM4305);//数据速率i = (byte)(spls.getSelectedItemId());Configdata[0] = 0x00;if(i == 0){//RF/32Configdata[0] |= 0x0f;}else if(i == 1) {//RF/40Configdata[0] |= 0x13;}else {//RF/64或其他Configdata[0] |= 0x1f;}spls = findViewById(R.id.SpinnerEncoderEM4305);//调制模式i = (byte)(spls.getSelectedItemId());if(i == 0){//曼切斯特码Configdata[0] |= 0x40;}else if(i == 1) {//Biphase码Configdata[0] |= 0x80;}else {//其他}Configdata[1] &= 0x3c;Configdata[2] &= 0x80;//自动发送最大块etMaxBlock = findViewById(R.id.editTextMaxBlockEM4305);strls = etMaxBlock.getText().toString().trim();if(strls.length() < 1){i = 6;etMaxBlock.setText("6");}else {i = (byte)Integer.parseInt(strls,10);if((i < 5) || (i > 13)){i = 6;etMaxBlock.setText("6");}}Configdata[1] |=  ((i % 4) * 64);Configdata[2] |= ((i / 4) % 4);spls = findViewById(R.id.SpinnerLoginModeEM4305);//密码规则(也就是读登陆或写登陆)i = (byte)(spls.getSelectedItemId());if(i == 1){//0-1块任意读,3-15块需密码读,0-13块任意写,14-15块需密码写i = 0x01;}else if(i == 2) {//0-15任意读,0-15块需密码写i = 0x04;}else if(i == 3) {//0-1块任意读,3-15块需密码读,0-15块需密码写i = 0x05;}else {//0-15块任意读.0-13块任意写,14-15块需密码写i = 0x00;}Configdata[2] |= (i * 4);etls.setText(String.format("%02X%02X%02X%02X",Configdata[0],Configdata[1],Configdata[2],Configdata[3]));}public void init4305(View view){byte i;byte status;//存放返回值byte myctrlword;//控制字byte[] oldpicckey = new byte[4];//密码byte[] mypiccserial = new byte[4];//卡序列号byte[] mypiccdata = new byte[4];//写入数据缓冲myctrlword = 0; //NEEDSERIAL:需要只对指定系列号的卡操作,NEEDKEY:需要用密码认证,LOCKBIT:锁定块,KEYENABLE:启用本卡的密码功能String strls;CheckBox cbls;EditText etls;updateset4305();cbls = findViewById(R.id.chkPwdEM4305);if(cbls.isChecked()) {//本次操作需要密码验证etls = findViewById(R.id.editTextPwdEM4305);strls = etls.getText().toString().trim();if(strls.length() < 8){tv.setText("密码长度不足8位");return;}for (i = 0; i < 4; i++) {oldpicckey[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误}myctrlword += ouridr.NEEDKEY;}cbls = findViewById(R.id.chkUidNeedEM4305);if(cbls.isChecked()) {//仅操作指定卡号的卡etls = findViewById(R.id.editTextUidEM4305);strls = etls.getText().toString().trim();if(strls.length() < 8){tv.setText("卡号长度不足8位");return;}for (i = 0; i < 4; i++) {mypiccserial[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误}myctrlword += ouridr.NEEDSERIAL;}//配置值:etls = findViewById(R.id.editTextConfigWordEM4305);strls = etls.getText().toString().trim();if(strls.length() < 8){tv.setText("配置值输入长度不足8位");return;}for (i = 0; i < 4; i++) {mypiccdata[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误}status = ouridr.em4305init(myctrlword,mypiccserial,oldpicckey,mypiccdata);if(status == 0){strls = "卡号[";strls += String.format("%02X%02X%02X%02X",mypiccserial[0],mypiccserial[1],mypiccserial[2],mypiccserial[3]);strls += "]";TextView tvls = findViewById(R.id.TextViewRetDataEM4305);tvls.setText(strls);strls = "配置EM4305卡成功";ouridr.beep(38);}else{if(status == 8){strls = "卡不在感应区 或 密码不正确";}else if(status == 23){strls = "错误代码:23,读卡器未插入";}else if(status == 1){strls = "错误代码:1,卡放得远 或 需要密码才能写卡";}else if(status == 12){strls = "错误代码:12,卡放得远 或 密码错误";}else{strls = "错误代码:" + Integer.toString(status);}}tv.setText(strls);}}

源码下载:AndroidstudioRFIDNFC读写源码资源-CSDN文库

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

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

相关文章

【大数据】Flink 测试利器:DataGen

Flink 测试利器&#xff1a;DataGen 1.什么是 FlinkSQL &#xff1f;2.什么是 Connector &#xff1f;3.DataGen Connector3.1 Demo3.2 支持的类型3.3 连接器属性 4.DataGen 使用案例4.1 场景一&#xff1a;生成一亿条数据到 Hive 表4.2 场景二&#xff1a;持续每秒生产 10 万条…

element-ui的el-upload组件实现上传拖拽排序图片顺序(sortablejs)

<template><!-- 省略其他配置 --><el-upload ref"upload" :file-list.sync"fileList"></el-upload></template><script>import Sortable from sortablejs;export default {data() {return {fileList: []};},mounted()…

Cacti 前台SQL注入漏洞复现(CVE-2023-39361)

0x01 产品简介 Cacti 是一套基于 PHP,MySQL,SNMP 及 RRDTool 开发的网络流量监测图形分析工具。 0x02 漏洞概述 该漏洞存在于graph_view.php文件中。默认情况下,访客用户无需身份验证即可访问graph_view.php,在启用情况下使用时会导致SQL注入漏洞。 攻击者可能利用此漏洞…

spark dateformat源码排错

背景 有一个任务 yyyy写成了YYYY&#xff0c;导致年份不对触发告警 select from_unixtime(unix_timestamp(),YYYY-MM-dd HH:mm:ss) 第一时间用spark dateformat搜索下看看官网&#xff0c;发现spark 官网也没有描述YYYY的信息 Datetime patterns - Spark 3.5.0 Documentati…

ELK日志分析

目录 1.ELK概述 ELK简述 ElasticSearch Kiabana Logstash 可添加其他的组件 Filebeat filebeat结合logstash带来好处 缓存/消息队列&#xff08;redis、kafka、RabbitMQ等&#xff09; Fluentd ELK的好处 完整日志系统基本特征 ELK的工作原理 2.ELK Elasticsearc…

【计算机网络】OSI七层模型与TCP/IP四层模型的对应与各层介绍

1 OSI七层模型与TCP/IP四层模型对应 2 OSI七层模型介绍 OSI&#xff08;Open Systems Interconnection&#xff09;模型是一个由国际标准化组织&#xff08;ISO&#xff09;定义的七层网络体系结构&#xff0c;用于描述计算机网络中的通信协议。每一层都有特定的功能&#xff…

JOSEF约瑟 SS-17B时间继电器 柜内安装,板前接线带底座

系列型号&#xff1a; SS-17B时间继电器&#xff1b; SS-17C时间继电器&#xff1b; SS-17C;SS-17B时间继电器 一、用途 SS-17系列时间继电器用于直流电路中&#xff0c;可在时间保护系统中作时间测量元件。适用于时间测量精度要求高&#xff0c;配合时间级差小 的场合&…

web前端项目-中国象棋【附源码】

中国象棋 【中国象棋】是一款历史悠久、深受人们喜爱的策略类游戏。在Web前端技术中&#xff0c;我们可以使用HTML、CSS和JavaScript等语言来制作一款中国象棋游戏。玩家使用棋子&#xff08;帅/相/士/炮/马/车/炮/卒&#xff09;在棋盘上相互对弈&#xff0c;将对手的“帅”棋…

2024年回炉计划之排序算法(一)

算法是计算机科学和信息技术中的重要领域&#xff0c;涉及到问题求解和数据处理的方法。要学习算法&#xff0c;你可能需要掌握以下一些基本知识&#xff1a; 基本数据结构&#xff1a; 了解和熟练使用各种数据结构&#xff0c;如数组、链表、栈、队列、树和图等。数据结构是算…

深度学习中Numpy的一些注意点(多维数组;数据类型转换、数组扁平化、np.where()、np.argmax()、图像拼接、生成同shape的图片)

文章目录 1多维数组压缩维度扩充维度 2numpy类型转换深度学习常见的float32类型。 3数组扁平化4np.where()的用法5np.argmax()6图像拼接7生成同shape的图片&#xff0c;指定数据类型 1多维数组 a.shape(3,2);既数组h3&#xff0c;w2 a.shape(2,3,2);这里第一个2表示axis0维度上…

Docker容器添加映射端口

方式一 简单粗暴&#xff08;需要等一段时间&#xff09; 直接给现在容器停了&#xff08;当然你要不想停也可以&#xff0c;只是打包会慢一点&#xff0c;当然我是没出意外&#xff0c;如果你怕出现特殊情况&#xff0c;那就先把容器停了&#xff09;&#xff0c;然后把这个容…

SQL Povit函数使用及实例

PIVOT函数常用于数据的行转列&#xff0c;同时也可以用此函数实现类似于Excel中的数据透视表的效果。 PIVOT函数 PIVOT 函数的基本语法如下&#xff1a; -- PIVOT 语法 SELECT <非透视的列>,[第一个透视的列] AS <列名称>,[第二个透视的列] AS <列名称>,.…