Android NFC一般使用方法
Android NFC一般使用方法
- Android NFC官网文档地址:[官网文档1](https://developer.android.google.cn/guide/topics/connectivity/nfc )、[官网地址2](https://developer.android.google.cn/reference/kotlin/android/nfc/package-summary?hl=en)
- 一般使用方法:
- 1、 AndroidManifest配置(可不配置):
- 2、 NfcAdapter.getDefaultAdapter(this)获取nfc adapter实列,isEnabled可判断nfc是否可用;
- 3、 Activity的onNewIntent会回调nfc读取的相应intent内容, NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action判断来自什么类型的vfc内容。
- 4、 val pendingIntent = PendingIntent.getActivity( this, PENDING_RESULT, intent, PendingIntent.FLAG_UPDATE_CURRENT)
- 5、 支持的标签技术:
- 可选择支持的标签技术:
- 6、代码相关:
Android NFC官网文档地址:官网文档1、官网地址2
一般使用方法:
1、 AndroidManifest配置(可不配置):
<action android:name="android.nfc.action.TAG_DISCOVERED" />
- 1> (ACTION_NDEF_DISCOVERED:如果扫描到包含 NDEF 负载的标签,并且可识别其类型,则使用此 Intent 启动 Activity。这是优先级最高的 Intent,标签调度系统会尽可能尝试使用此 Intent 启动 Activity,在行不通时才会尝试使用其他 Intent。
- 2> ACTION_TECH_DISCOVERED:如果没有登记要处理 ACTION_NDEF_DISCOVERED Intent 的 Activity,则标签调度系统会尝试使用此 Intent 来启动应用。此外,如果扫描到的标签包含无法映射到 MIME 类型或 URI 的 NDEF 数据,或者该标签不包含 NDEF 数据,但它使用了已知的标签技术,那么也会直接启动此 Intent(无需先启动 ACTION_NDEF_DISCOVERED)。
- 3> ACTION_TAG_DISCOVERED:如果没有处理 ACTION_NDEF_DISCOVERED 或者 ACTION_TECH_DISCOVERED Intent 的 Activity,则使用此 Intent 启动 Activity。
2、 NfcAdapter.getDefaultAdapter(this)获取nfc adapter实列,isEnabled可判断nfc是否可用;
3、 Activity的onNewIntent会回调nfc读取的相应intent内容, NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action判断来自什么类型的vfc内容。
4、 val pendingIntent = PendingIntent.getActivity( this, PENDING_RESULT, intent, PendingIntent.FLAG_UPDATE_CURRENT)
NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch –使用前台调度,这种方式不用AndroidManifest注册intent-filter
5、 支持的标签技术:
类 | 说明 |
---|---|
TagTechnology | 这是所有标签技术类都必须实现的接口。 |
NfcA | 提供对 NFC-A (ISO 14443-3A) 属性和 I/O 操作的访问权限。 |
NfcB | 提供对 NFC-B (ISO 14443-3B) 属性和 I/O 操作的访问权限。 |
NfcF | 提供对 NFC-F (JIS 6319-4) 属性和 I/O 操作的访问权限。 |
NfcV | 提供对 NFC-V (ISO 15693) 属性和 I/O 操作的访问权限。 |
IsoDep | 提供对 ISO-DEP (ISO 14443-4) 属性和 I/O 操作的访问权限。 |
Ndef | 提供对 NDEF 格式的 NFC 标签上的 NDEF 数据和操作的访问权限。 |
NdefFormatable | 为可设置为 NDEF 格式的标签提供格式化操作。 |
可选择支持的标签技术:
类 | 说明 |
---|---|
MifareClassic | 提供对 MIFARE Classic 属性和 I/O 操作的访问权限(如果此 Android 设备支持 MIFARE)。 |
MifareUltralight | 提供对 MIFARE Ultralight 属性和 I/O 操作的访问权限(如果此 Android 设备支持 MIFARE)。 |
6、代码相关:
- 1>当前是否支持NFC功能:
AndroidManifest(permission):
<uses-permission android:name="android.permission.NFC" /><uses-permission android:name="android.permission.NFC_PREFERRED_PAYMENT_INFO" />
// Initialize the NFC adapterprivate fun initNfc() {// Gets the default NFC adapter for the systemmNfcAdapter = NfcAdapter.getDefaultAdapter(this)textBtn?.apply {text = if (mNfcAdapter == null) {"The current phone does not support NFC"} else if (!mNfcAdapter?.isEnabled!!) {"Please enable NFC in System Settings first"} else {"Mobile phones currently support NFC"}}}
- 2>注册当前页面接收的NFC类型(IsoDep、NfcA、NfcB…):
override fun onResume() {super.onResume()Log.d("$TAG==", "onResume")mNfcAdapter?.let {if (!it.isEnabled) {return}val intent =Intent(this, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)val pendingIntent = PendingIntent.getActivity(this,PENDING_RESULT,intent,PendingIntent.FLAG_UPDATE_CURRENT)//It can define more than one here, but pay attention to the collocation, or there will be no callbacksval techLists = arrayOf(arrayOf(IsoDep::class.java.name
// ,
// NfcA::class.java.name,
// NfcB::class.java.name,
// NfcF::class.java.name,
// Ndef::class.java.name,
// NdefFormatable::class.java.name))try {val filters = arrayOf(IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED, "*/*"))it.enableForegroundDispatch(this, pendingIntent, filters, techLists)} catch (e: Exception) {e.printStackTrace()}}}
- 3>接收当前页面感应到的NFC信息,通过intent接收传递的信息,action来区分:
override fun onNewIntent(intent: Intent) {Log.d("$TAG== onNewIntent==","$TAG ${intent.action} " +" ${intent.extras} " +" ${intent.flags} ")super.onNewIntent(intent)Toast.makeText(this, "$TAG ${intent.action} $intent", Toast.LENGTH_LONG).show()Log.d("$TAG== onNewIntent==","NfcAdapter.ACTION_NDEF_DISCOVERED => ${NfcAdapter.ACTION_NDEF_DISCOVERED}")if (NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action) {intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)?.also { rawMessages ->val messages: List<NdefMessage> = rawMessages.map { it as NdefMessage }// Process the messages array.Log.d("==nfc message==", messages.toString())}} else if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action) {val extraTag = intent.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG)Log.d("$TAG==", "${intent.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG)}"+ ",ACTION_NDEF_DISCOVERED==${intent.getParcelableArrayExtra(NfcAdapter.ACTION_NDEF_DISCOVERED)}"+ ",EXTRA_NDEF_MESSAGES==${intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)}")extraTag?.let {Log.d("$TAG==", "id:${extraTag.id},techList:${extraTag.techList}")}}}
- 4>发送NFC信息example:
//nfcv processprivate fun processTagNfcV(tag: Tag?) {val nfcV = NfcA.get(tag)Log.d("$TAG==NfcA ", "${nfcV}")if (nfcV != null) {// Create a connectionnfcV.connect()val data = ByteArray(20)// Send a messageval response: ByteArray = nfcV.transceive(data)Log.d("$TAG==response ", "${response.let {NdefRecord(TNF_ABSOLUTE_URI,"https://developer.android.com/index.html".toByteArray(Charset.forName("US-ASCII")),it,it)}}")// close junctionnfcV.close()}}
- 4.1>读写NFC信息(官网示例):
class MifareUltralightTagTester {//write fun writeTag(tag: Tag, tagText: String) {MifareUltralight.get(tag)?.use { ultralight ->ultralight.connect()Charset.forName("US-ASCII").also { usAscii ->ultralight.writePage(4, "abcd".toByteArray(usAscii))ultralight.writePage(5, "efgh".toByteArray(usAscii))ultralight.writePage(6, "ijkl".toByteArray(usAscii))ultralight.writePage(7, "mnop".toByteArray(usAscii))}}fun readTag(tag: Tag): String? {return MifareUltralight.get(tag)?.use { mifare ->mifare.connect()val payload = mifare.readPages(4)String(payload, Charset.forName("US-ASCII"))}}}}
- 5>当前页面停止接收:
override fun onPause() {super.onPause()//forbidden nfcmNfcAdapter?.isEnabled.takeIf { it != true }?: mNfcAdapter?.disableForegroundDispatch(this)Log.d("$TAG", "onPause")}
end
Android NFC一般使用方法相关推荐
- c语言读写nfc,Android NFC M1卡读写芯片卡读写(CPU卡读写)(RFID读写)
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/sgn5200/article/deta ...
- android nfc(一)
2019独角兽企业重金招聘Python工程师标准>>> 转载自 http://blog.sina.com.cn/s/blog_67d95f4001011uiv.html 啥是NFC你 ...
- Android NFC开发
由于工作关系,需要做智能卡(JavaCard Applet)和Android NFC方面的开发. NFC主要有3种模式:读卡器模式, 卡模拟模式, 点到点模式. 本文主要参考了Android官方Car ...
- Android NFC读取电子标签
2019独角兽企业重金招聘Python工程师标准>>> NFC,什么是NFC?想必大家都听过,就是近场通信,接下来我想给大家展示一个简单的demo, 这个demo是我在公司工作中开发 ...
- android nfc ndef mifareclassic,Android NFC开发-实践篇
Android NFC开发-实践篇 https://blog..net/_GYG/article/details/72899417 在Android NFC开发-理论篇中,我们了解了在Android中 ...
- Android NFC开发-实践篇
Android NFC开发-实践篇 在Android NFC开发-理论篇中,我们了解了在Android中开发NFC的一些理论知识,这篇我们继续应用我们上一篇学到的知识,实现对NDEF格式标签和Mifa ...
- Android NFC开发-理论篇
Android NFC开发-理论篇 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/CSDN_GYG/article/details/72884849 ...
- Android NFC开发详细总结
Android NFC开发详细总结 Near Field Communication (NFC) 为一短距离无线通信技术,通常有效通讯距离为4厘米以内.NFC工作频率为13.65 兆赫兹,通信速率为1 ...
- Android NFC的应用
NFC是一种近距离无线通讯技术 p13.56MHz频率运行于20厘米距离内.其传输速度有106 Kbit/秒.212 Kbit/秒或者424 Kbit/秒三种.目前近场通信已通过成为ISO/IEC I ...
最新文章
- AFNetworking和ASIHTTPRequest的比较
- 数据挖掘与数据化运营实战. 3.10 信用风险模型
- [PAT乙级]1004 成绩排名
- 手游建筑美术资源_建筑商和机械手
- Uvaoj 10048 - Audiophobia(Floyd算法变形)
- windows 7 等 公文包 功能 作用 使用说明
- python 正则表达式语法
- fullcalendar 只保留周_周末大利好!重磅全球指数加仓,210亿资金涌向A股「投资周历来了」...
- Haar特征分类器和AdaBoost算法
- 异步方法中取消异步操作
- 数学建模常用算法—层次分析法(AHP)
- 全息投影图片合成-(matlab)(将4个视角图合成为一张)
- 多行文本垂直居中的三种方法
- 计算机滴滴响重启,电脑发出滴滴声是什么原因(常见的4个故障和解决方法)...
- 计算机函数公式的英文表达式,Houdini Expression functions 函数表达式中英文对照(二)...
- 你在日常的测试工作中遇到过哪些困境呢?
- Word图文混排中图片的高级处理技巧
- 打印机打印出来的条码是歪的怎么办
- rpm -q和rpm -qa|grep
- 默认ip_各品牌路由器登录网址大全 路由器默认用户名/密码