Android Studio安卓读取EM4100 TK4100卡卡号源码

本文详细描述了在Android应用中使用读卡器进行ID卡和HID卡操作的示例,包括读卡、仅读一次卡的功能实现以及错误处理,展示了如何集成RFID/NFC功能并使用ConstraintLayout和ScrollView布局。
摘要由CSDN通过智能技术生成

本示例使用的读卡器:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-21818769070.35.44005b43nb1q2h&id=562957272162

<?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=".IdCardActivity">
    <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="ID卡、HID卡测试页  "
            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>

    <TextView
        android:id="@+id/sample_text"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:padding="3dp"
        android:text="操作结果"
        android:textSize="12sp"
        android:background="@drawable/shape4border"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"  />

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

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

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

            <Button
                android:id="@+id/btnReadID"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:padding="3dp"
                android:onClick="idr_read"
                android:text="读卡"
                app:layout_constraintStart_toStartOf="@+id/btnReadOnceID"
                app:layout_constraintEnd_toEndOf="@+id/btnReadOnceID"
                app:layout_constraintTop_toTopOf="parent" />


            <Button
                android:id="@+id/btnReadOnceID"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="idr_read_once"
                android:text="仅读一次,重新取放卡才能读到第二次"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/btnReadID"/>

            
        </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.TextView;

import com.reader.ouridr;

public class IdCardActivity extends AppCompatActivity {
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_id_card);
        androidx.appcompat.widget.Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        tv = findViewById(R.id.sample_text);
        tv.setText("操作结果");
    }
    @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();
    }

    //读ID卡
    public void idr_read(View view)
    {
        byte status;//存放返回值
        byte[] idserial = new byte[5];

        String strls;
        status = ouridr.read(idserial);
        if(status == 0)
        {
            ouridr.beep(38);
            strls = "读卡成功!16进制卡号为:";
            String strls1 = "0"+Integer.toHexString(idserial[0]);
            strls = strls + strls1.substring(strls1.length()-2) +"-";
            strls1 = "0"+Integer.toHexString(idserial[1]);
            strls = strls + strls1.substring(strls1.length()-2) +"-";
            strls1 = "0"+Integer.toHexString(idserial[2]);
            strls = strls + strls1.substring(strls1.length()-2) +"-";
            strls1 = "0"+Integer.toHexString(idserial[3]);
            strls = strls + strls1.substring(strls1.length()-2) +"-";
            strls1 = "0"+Integer.toHexString(idserial[4]);
            strls = strls + strls1.substring(strls1.length()-2);

            long cardhao;
            cardhao = idserial[1] & 0xff;
            cardhao *= 256;
            cardhao += idserial[2] & 0xff;
            cardhao *= 256;
            cardhao += idserial[3] & 0xff;
            cardhao *= 256;
            cardhao += idserial[4] & 0xff;
            String card8h10d = "000000000"+Long.toString(cardhao);//0305887634  123B7992
            card8h10d=card8h10d.substring(card8h10d.length()-10,card8h10d.length());
            strls=strls+"\n转8H10D码:"+card8h10d;

            String WG341="00000"+Integer.toString((idserial[3]& 0xff)*256+(idserial[4]& 0xff));
            WG341=WG341.substring(WG341.length()-5,WG341.length());
            String WG342="00000"+Integer.toString((idserial[1]& 0xff)*256+(idserial[2]& 0xff));
            WG342=WG342.substring(WG342.length()-5,WG342.length());
            strls=strls+"\n转韦根34码:"+WG341+WG342;

            String WG261="000"+Integer.toString(idserial[2]& 0xff);
            WG261=WG261.substring(WG261.length()-3,WG261.length());
            String WG262="00000"+Integer.toString((idserial[3]& 0xff)*256+(idserial[4]& 0xff));
            WG262=WG262.substring(WG262.length()-5,WG262.length());
            strls=strls+"\n转韦根26码:"+WG261+WG262;

            tv.setText(strls);
        } else {
            PrintErrInf(status);
        }
    }

    //仅读一次ID卡,重新取放卡才能读到第二次
    public void idr_read_once(View view)
    {
        byte status;//存放返回值
        byte[] idserial = new byte[5];

        String strls;
        status = ouridr.readonce(idserial);
        if(status == 0)
        {
            ouridr.beep(38);
            strls = "读卡成功!16进制卡号为:";
            String strls1 = "0"+Integer.toHexString(idserial[0]);
            strls = strls + strls1.substring(strls1.length()-2) +"-";
            strls1 = "0"+Integer.toHexString(idserial[1]);
            strls = strls + strls1.substring(strls1.length()-2) +"-";
            strls1 = "0"+Integer.toHexString(idserial[2]);
            strls = strls + strls1.substring(strls1.length()-2) +"-";
            strls1 = "0"+Integer.toHexString(idserial[3]);
            strls = strls + strls1.substring(strls1.length()-2) +"-";
            strls1 = "0"+Integer.toHexString(idserial[4]);
            strls = strls + strls1.substring(strls1.length()-2);

            long cardhao;
            cardhao = idserial[1] & 0xff;
            cardhao *= 256;
            cardhao += idserial[2] & 0xff;
            cardhao *= 256;
            cardhao += idserial[3] & 0xff;
            cardhao *= 256;
            cardhao += idserial[4] & 0xff;
            String card8h10d = "000000000"+Long.toString(cardhao);//0305887634  123B7992
            card8h10d=card8h10d.substring(card8h10d.length()-10,card8h10d.length());
            strls=strls+"\n转8H10D码:"+card8h10d;

            String WG341="00000"+Integer.toString((idserial[3]& 0xff)*256+(idserial[4]& 0xff));
            WG341=WG341.substring(WG341.length()-5,WG341.length());
            String WG342="00000"+Integer.toString((idserial[1]& 0xff)*256+(idserial[2]& 0xff));
            WG342=WG342.substring(WG342.length()-5,WG342.length());
            strls=strls+"\n转韦根34码:"+WG341+WG342;

            String WG261="000"+Integer.toString(idserial[2]& 0xff);
            WG261=WG261.substring(WG261.length()-3,WG261.length());
            String WG262="00000"+Integer.toString((idserial[3]& 0xff)*256+(idserial[4]& 0xff));
            WG262=WG262.substring(WG262.length()-5,WG262.length());
            strls=strls+"\n转韦根26码:"+WG261+WG262;

            tv.setText(strls);
        } else {
            PrintErrInf(status);
        }
    }

    public void PrintErrInf(byte errcode) {
        String dispstr;
        switch(errcode){
            case 8:
                dispstr="错误代码:8,未寻到卡,请重新将卡放在ID卡读卡器感应区!";
                break;
            case 22:
                dispstr="错误代码:22,动态库或驱动程序异常!";
                break;
            case 23:
                dispstr="错误代码:23,发卡器未连接!";
                break;
            case 24:
                dispstr="错误代码:24,读卡器可能没有该功能!";
                break;
            default:
                dispstr="未知错误,错误代码:"+Integer.toString(errcode);
                break;
        }
        tv.setText(dispstr);
    }

}

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

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vx_13822155058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值