gpt4 book ai didi

android - 使用 NFC 将文本数据从 Android 发送到 IOS

转载 作者:行者123 更新时间:2023-11-29 16:39:19 24 4
gpt4 key购买 nike

<分区>

我知道目前 IOS 11 中没有 API 可以将数据写入 NFC 标签,但是可以从 NFC 标签读取数据并希望将文本从 Android 设备传递到 iPhone。我假设可以编写 NdefMessage 并且它将在 IOS 上接收,但它对我不起作用。当我在 IOS(使用 NfcActions 应用程序)上开始 NFC 扫描时,没有收到任何 Intent。

我的主要 Activity 的源代码:

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
}

我还在主 list 中添加了以下权限和 Intent 过滤器

<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?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com