如何通过Android NFC传输纯文本? [英] How to transfer plain text via Android NFC?

查看:115
本文介绍了如何通过Android NFC传输纯文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android NFC的新手,正在android中开发NFC应用程序.我的想法是设备A需要向设备B发送纯文本.在Android NFC中可以吗?我刚刚在Reader和Writer上尝试了Tag Dispatcher(enableForegroundDispatch,disableForegroundDispatch).我的读者端代码是:

I am new to Android NFC and developing NFC application in android. My idea is Device A need to send a plain text to Device B. Is it possible in Android NFC? I just tried with Tag Dispatcher (enableForegroundDispatch , disableForegroundDispatch) on both Reader and Writer. My Reader side code is :

nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilters, techList)
override fun onNewIntent(intent: Intent?) {
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)?.also { rawMessages ->
            val messages: List<NdefMessage> = rawMessages.map { it as NdefMessage }

            for (message in messages) {
                for (record in message.records) {
                    println(" ${record.toString()}")
                }
            }
        }
}

我的Writer辅助代码为:

My Writer side code is:

nfcAdapter.enableForegroundDispatch(
        this, pendingIntent, intentFilters, techList)
override fun onNewIntent(intent: Intent?) {
if (action.equals(NfcAdapter.EXTRA_TAG)) {
        val tagFromIntent = intent.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG)
        if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
            || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
        ) {
            println("testing=============== tag discovered ")
            writeNdefMessage(tagFromIntent!!, "This is my first app")
        }
    }}

private fun writeNdefMessage(tag: Tag, message: String) {
    val record: NdefRecord = newTextRecord(message, Locale.ENGLISH, true)!!
    val ndefMessage = NdefMessage(arrayOf(record))
    try {
        if (isExist(tag.techList, NdefFormatable::class.java.name)) {
            val ndefFormatable = NdefFormatable.get(tag)
            try {
                if (!ndefFormatable.isConnected) {
                    ndefFormatable.connect()
                }
                ndefFormatable.format(ndefMessage)
            } finally {
                ndefFormatable.close()
            }
        } else if (isExist(tag.techList, Ndef::class.java.name)) {
            val ndef = Ndef.get(tag)
            try {
                if (!ndef.isConnected) {
                    ndef.connect()
                }
                if (ndef.isWritable) {
                    ndef.writeNdefMessage(ndefMessage)
                }
            } finally {
                ndef.close()
            }
        }
    } catch (e: FormatException) {
        println("Format failed exception")
    } catch (e: IOException) {
        println("")
    }
}

当我扫描Tag时启动了应用程序(通过AndroidManifest.xml详细信息).但是我无法通过NFC发送纯文本.我不知道我做错了什么.我不知道这种方法是对还是错.请帮助我进行此操作.

Application is launched when I scan the Tag (via AndroidManifest.xml details). But I am not able to send plain text via NFC. I don't know what I did wrong. I don't know whether the approach is right or wrong. Please help me to proceed this.

谢谢.

推荐答案

因此,从API 29开始,Android中的对等NFC(设备对设备)(也称为Android Beam)已被弃用

So in Android peer to peer NFC (Device to Device) also called Android Beam has been deprecated as of API 29

请参见 https://developer.android.com/reference/android/nfc/NfcAdapter#setNdefPushMessage(android.nfc.NdefMessage,%20android.app.Activity,%20android.app.Activity...)

您使用错误的方法在较旧的Android版本中使用Android Beam.参见 https://developer.android.com/guide/topics/connectivity/nfc/nfc#p2p 了解有关如何使用它的更多详细信息.(您正在使用写入NFC卡而不是其他设备的方法)

You are using the wrong methods to use Android Beam in older Android Versions. See https://developer.android.com/guide/topics/connectivity/nfc/nfc#p2p for more details of actually how to use it. (You are using methods for writing to a NFC card not another Device)

请注意,通过NFC进行点对点仅适用于Android,iOS不支持它,因此已弃用蓝牙/Wifi Direct

Note Peer to Peer via NFC is Android only, iOS does not support it and it is depreciated in favour of Bluetooth/Wifi Direct

请注意,仍然有可能使一个Android设备使用主机卡仿真来模拟上面带有NDEF消息的Type 4 NFC卡,但这很难实现.

Note that it is still possible to have one Android Device use Host Card Emulation to Emulate a Type 4 NFC card with an NDEF messages on it but this is quite complicated to achieve.

更新:

链接到主机卡模拟 https://developer.android.com/guide/topics/connectivity/nfc/hce 和Type 4卡规范 http://apps4android.org/nfc-specifications/NFCForum-TS-Type-4-Tag_2.0.pdf

Link to Host Card Emulation https://developer.android.com/guide/topics/connectivity/nfc/hce and Type 4 card spec http://apps4android.org/nfc-specifications/NFCForum-TS-Type-4-Tag_2.0.pdf

这篇关于如何通过Android NFC传输纯文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆