没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Android实现读取实现读取NFC卡卡号示例卡卡号示例
本篇文章主要介绍了Android实现读取NFC卡卡号示例,小编觉得挺不错的,现在分享给大家,也给大家做个参
考。一起跟随小编过来看看吧
Android实现读取NFC卡卡号示例,具体如下:
1.权限
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
2.注册(静态)
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<data android:mimeType="text/plain" />
</intent-filter>
3.Activity
初始化
//初始化NfcAdapter
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
// 初始化PendingIntent,当有NFC设备连接上的时候,就交给当前Activity处理
pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
启动
@Override
protected void onResume() {
super.onResume();
mNfcAdapter.enableForegroundDispatch(this, pi, null, null); //启动
}
获取数据
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// 当前app正在前端界面运行,这个时候有intent发送过来,那么系统就会调用onNewIntent回调方法,将intent传送过来
// 我们只需要在这里检验这个intent是否是NFC相关的intent,如果是,就调用处理方法
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
processIntent(intent);
}
}
解析
/**
* Parses the NDEF Message from the intent and prints to the TextView
*/
private void processIntent(Intent intent) {
//取出封装在intent中的TAG
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String CardId =ByteArrayToHexString(tagFromIntent.getId());
}
private String ByteArrayToHexString(byte[] inarray) {
int i, j, in;
String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
"B", "C", "D", "E", "F" };
String out = "";
for (j = 0; j < inarray.length; ++j) {
in = (int) inarray[j] & 0xff;
i = (in >> 4) & 0x0f;
out += hex[i];
i = in & 0x0f;
out += hex[i];
}
return out;
}
资源评论
- skyredni2020-09-24我以为啥有用的,就是全网免费的东西。没必要下载
weixin_38706197
- 粉丝: 2
- 资源: 979
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 徐州市区shp边界文件
- 简易数字频率计设计(原理图、PCB、源码、分析报告)
- 20225207038罗丽文财管专2201班.rar
- Golang:通过Gin框架+Redis+责任链,实现一个简单的钉钉机器人,进行消息处理 ps:多应用版
- 基于cnn和AlexNet的铝材缺陷检测内含数据集和demo原型2种方法
- 2023年各城市PM2.5, PM10, SO2, NO2等环境空气质量数据
- go 语言 实现的 使用modbus 协议 通过DTU tcp 链接 采集数据
- SHELL脚本(全是干货)
- Windows屏幕录屏软件
- 2022年各城市PM2.5, PM10, SO2, NO2等环境空气质量数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功