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 adapter
private fun initNfc() {
// Gets the default NFC adapter for the system
mNfcAdapter = 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 callbacks
val 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 process
private fun processTagNfcV(tag: Tag?) {
val nfcV = NfcA.get(tag)
Log.d("$TAG==NfcA ", "${nfcV}")
if (nfcV != null) {
// Create a connection
nfcV.connect()
val data = ByteArray(20)
// Send a message
val 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 junction
nfcV.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 nfc
mNfcAdapter?.isEnabled.takeIf { it != true }
?: mNfcAdapter?.disableForegroundDispatch(this)
Log.d("$TAG", "onPause")
}
end