android读取手机nfc卡号,Android -- 读取NFC卡号

本文介绍如何在Android应用中读取NFC卡片的ID。首先在manifest文件中添加NFC权限,然后在Activity中初始化NFC Adapter,并使用PendingIntent。在onResume()中启动前台调度,onNewIntent()中获取Tag信息。通过ByteArrayToHexString()和flipHexStr()辅助函数将Tag ID转换为十六进制字符串并翻转顺序,从而获取卡号。
摘要由CSDN通过智能技术生成

1. menifest文件中需要添加:

android:name="android.hardware.nfc"

android:required="true" />

在activity中添加:

2. 初始化时代码:

Intent nfcIntent = new Intent(this, getClass());

nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

mPendingIntent =

PendingIntent.getActivity(this, 0,nfcIntent , 0);

mAdapter = NfcAdapter.getDefaultAdapter(this);

if (mAdapter == null) {

Toast.makeText(getApplicationContext(), "NFC feature is supported on this device.", Toast.LENGTH_SHORT).show();

return;

}

需要Override的函数:

private NfcAdapter mAdapter;

private PendingIntent mPendingIntent;

@TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)

protected void onResume() {

super.onResume();

mAdapter.enableForegroundD

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值