NFC读取卡号学习

本文介绍了如何在Android应用中使用NFC技术读取卡号。首先,需要在AndroidManifest.xml中添加必要的权限和类声明。接着,定义响应的卡片类型,并在XML文件中列出。然后,在读卡类中重写onNewIntent()方法,通过Tag对象获取 IsoDep 对象,连接并执行查询语句来读取卡号。最后,解析响应数据,提取卡号信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

马克.

1.权限,读卡权限

    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.NFC"/>
    <uses-feature android:name="android.hardware.nfc" />

懒得找大概这些把,网络上大把大把的.

和类申明

<activity
            android:name="com.cpu.charge.MainActivity"
            android:configChanges="keyboardHidden|orientation"
            android:launchMode="singleTask"
            android:screenOrientation="portrait"
            android:windowBackground="@null"
            android:windowSoftInputMode="adjustUnspecified|stateAlwaysHidden" >
            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
                <action android:name="android.nfc.action.TAG_DISCOVERED" />
            </intent-filter>
            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
        </activity>

2.卡类型,需要在res文件目录下创建xml文件夹,并创建一个xml文件,定义你想要响应的卡片类型.

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
  <tech-list>
  <tech>android.nfc.tech.IsoDep</tech>
  <tech>android.nfc.tech.NfcA</tech>
  <tech>android.nfc.tech.NfcB</tech>
  <tech>android.nfc.tech.NfcF</tech>
  <tech>android.nfc.tech.NfcV</tech>
  <tech>android.nfc.tech.Ndef</tech>
  <tech>android.nfc.tech.NdefFormatable</tech>
  <tech>android.nfc.tech.MifareClassic</tech>
  <tech>android.nfc.tech.MifareUltralight</tech>
  </tech-list>
</resources>

3.写你的读卡的类:其中,重写protected void onNewIntent(Intent intent) 方法.

然后使用Parcelable parcelable = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Tag tag = (Tag) parcelable;
IsoDep isodep = IsoDep.get(tag);
iso_tag = new Iso7816.Tag(isodep);
iso_tag.connect();

获取响应的tag

再然后写好查询的语句....不同的卡规格不一样,你要得到卡的结构类型然后再去写语句

byte[] SELECT_FILE2 = { (byte) 0x00, (byte) 0xA4,
(byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x3F, (byte) 0x00 };// select
// 根目录MF

byte[] SELECT_FILE_DR2 = { (byte) 0x00, (byte) 0xA4,
(byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0xDF, (byte) 0x04 };// select
// DF
byte[] SELECT_FILE3 = { (byte) 0x00, (byte) 0xA4,
(byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x15, (byte) 0x1E};// select
//公共基本数据文件

byte[] SELECT_FILE_INFO2 = { (byte) 0x00, (byte) 0xB0,
(byte) 0x95, (byte) 0x0A, (byte) 0x00};// select
// info
byte[] SELECT_FILE_INFO3 = { (byte) 0x00, (byte) 0xB0,
(byte) 0x96, (byte) 0x16, (byte) 0x00};// select

然后了就用你的到的tag去获取你想要的东西

Response result_info2 = tag.executeCmd(SELECT_FILE_INFO3);// 查询info

if (result_info.isOkey()) {//判断一下获得是否正确

然后解析就好了 返回的东西和你想要的肯定有差别后面就是截取数据了
final String cardInfo2 = result_info2.toString();
String cardNumber = cardInfo2.substring(30, 64);
String cardNum  = "";
for (int i = 0; i < cardNumber.length()/2; i++) {
String str = cardNumber.substring(2*i,2*(i+1));
cardNum = cardNum+str.substring(1,2);
}


好了卡号获取到了


暂时还没有写入的需求....也就懒得看了....读取的差不多就这些步骤,马克一下.

以下是一个简单的安卓 NFC 读取卡号的示例代码: 首先,确保在 AndroidManifest.xml 文件中添加了以下权限: ``` <uses-permission android:name="android.permission.NFC" /> ``` 然后,在相应的 Activity 中添加以下代码: ```java public class MainActivity extends Activity { private NfcAdapter nfcAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取 NFC 适配器 nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter == null) { Toast.makeText(this, "设备不支持 NFC", Toast.LENGTH_SHORT).show(); finish(); return; } // 检查 NFC 是否开启 if (!nfcAdapter.isEnabled()) { Toast.makeText(this, "请在系统设置中先启用 NFC 功能", Toast.LENGTH_SHORT).show(); finish(); return; } // 创建 PendingIntent 对象,并在检测到卡片时调用 onNewIntent 方法 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); IntentFilter[] filters = new IntentFilter[]{filter}; nfcAdapter.enableForegroundDispatch(this, pendingIntent, filters, null); } @Override protected void onResume() { super.onResume(); // 检查 NFC 是否开启 if (nfcAdapter != null && !nfcAdapter.isEnabled()) { Toast.makeText(this, "请在系统设置中先启用 NFC 功能", Toast.LENGTH_SHORT).show(); finish(); return; } } @Override protected void onPause() { super.onPause(); // 关闭前台调度 if (nfcAdapter != null) { nfcAdapter.disableForegroundDispatch(this); } } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); String cardNumber = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)); Toast.makeText(this, "卡号: " + cardNumber, Toast.LENGTH_SHORT).show(); } /** * 将字节数组转换为十六进制字符串 * @param bytes 字节数组 * @return 十六进制字符串 */ private String ByteArrayToHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte aByte : bytes) { String hex = Integer.toHexString(aByte & 0xFF); if (hex.length() == 1) { sb.append('0'); } sb.append(hex); } return sb.toString().toUpperCase(); } } ``` 在 AndroidManifest.xml 文件中,需要添加以下声明: ```xml <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.TAG_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> ``` 以上代码中,主要实现了以下功能: 1. 获取 NFC 适配器,并检查设备是否支持 NFC。 2. 检查 NFC 是否开启。 3. 创建 PendingIntent 对象,并在检测到卡片时调用 onNewIntent 方法。 4. 将字节数组转换为十六进制字符串,获取卡号并显示在 Toast 中。 实际使用时,还需要根据具体的需求进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值