使用NFC发送来自Android的文本数据IOS [复制]

问题描述 投票:1回答:2

这个问题已经在这里有一个答案:

我知道,在目前存在IOS 11没有API将数据写入NFC标签,但它可以读取NFC标签的数据,并希望通过从Android设备的文本到iPhone。我认为这是可以写NdefMessage,它会在IOS被接收,但它只是不工作对我来说。有没有当我开始NFC扫描的IOS(使用NfcActions应用)接受意向。

我的主要活动的源代码如下:

class MainActivity : AppCompatActivity() {

var nfc: NfcAdapter? = null

@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)


    hintLabel.text = "Initializing..."
    nfc = NfcAdapter.getDefaultAdapter(this)

    if (nfc == null) hintLabel.text = "NFC is not available on this device"
    else hintLabel.text = "NFC initialized"
}

override fun onResume() {
    super.onResume()

    nfc?.enableNFCInForeground(this, javaClass)
}

override fun onPause() {
    super.onPause()
    nfc?.disableForegroundDispatch(this)
}

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    val pathPrefix = "/testuser:nfctest"
    val isSuccess = createNFCMessage(pathPrefix, "Hello World", intent)
    if(isSuccess)
        hintLabel.text = "Successfully wrote data"
    else
        hintLabel.text = "Couldnt write any data"

}

fun createNFCMessage(pathPrefix: String, payload: String, intent: Intent?) : Boolean {

    val nfcRecord = NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, pathPrefix.toByteArray(), ByteArray(0), payload.toByteArray())
    val nfcMessage = NdefMessage(arrayOf(nfcRecord))
    intent?.let {
        val tag = it.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG)
        return writeMessageToTag(nfcMessage, tag)
    }
    return false
}


fun <T>NfcAdapter.enableNFCInForeground(activity: Activity, classType: Class<T>) {

    val pendingIntent = PendingIntent.getActivity(activity, 0,
            Intent(activity,classType).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0)

    val filters = arrayOf(IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED))
    val techList = arrayOf(arrayOf(Ndef::class.java.name), arrayOf(NdefFormatable::class.java.name))

    this.enableForegroundDispatch(activity, pendingIntent, filters, techList)
}

private fun writeMessageToTag(nfcMessage: NdefMessage, tag: Tag?): Boolean {

    // This functions writes given NdefMessage to the tag, if it's possible
    // and returns whether it was successful or not
}

而且我还添加了以下权限和意图过滤器主清单

<uses-permission android:name="android.permission.NFC" />
<uses-feature
    android:name="android.hardware.nfc"
    android:required="false" />
<application ...>
    <activity ...>     
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:host="ext"
                android:pathPrefix="/testuser:nfctest"
                android:scheme="vnd.android.nfc"
                android:mimeType="text/plain"/>
        </intent-filter>
    </activity>
</application>

我在做什么错误,以及如何使用NFC正确传递来自Android设备的数据到iPhone?

android ios nfc hce nfc-p2p
2个回答
2
投票

显然,通常对-2等方法,从一个手机发送NDEF消息2个Android设备之间的另一只作品。从Android将消息发送到iOS则需要使用主机卡模拟。基本上,Android手机需要模拟一个标签4类型(基于NDEF论坛规格),针对iPhone阅读的内容。希望让你在正确的轨道上...


1
投票

对于任何人谁被困在这个问题上,我已阅读NFCForum-TS-Type-4-Tag其中提出由@迈克尔罗兰。整个想法是正确的。所有你需要的是模拟发送过程和接收到的命令字节数组转换为NDEF消息。我创建了两个仓库,一个总结有关转换字符串到NDEF message,另一个全包是验证的Android HCE是否正确,不是iOS Reader NDEF TAG

所以,祝你好运!

另请response by Michael RolandNDEF Message with HCE Android

© www.soinside.com 2019 - 2024. All rights reserved.