提问人:Tural Sufan 提问时间:11/4/2023 更新时间:11/6/2023 访问量:39
将 NDEF 记录写入 NDEF TAG KOTLIN
Write NDEF RECORDS to NDEF TAG KOTLIN
问:
我应该准备一个演示应用程序,它应该读取 Ndef 记录并将它们叠加到卡片上。
我有 3 个主要函数:readNdef、writeNdef 和 readTag。readNdef 和 readTag 工作正常。但是当我尝试编写新的 Ndef Records 时,它无法正常工作。
它写记录,但对它们进行一些更改。我使用方法。此方法进行一些转换。我想在没有任何更改的情况下编写令牌。NdefRecord.createTextRecord
private fun readNdef(tag: Tag?) {
val ndef = Ndef.get(tag)
try {
ndef.connect()
val inNdef = ndef.ndefMessage
Log.d("fdsfsdfsdf", inNdef.toString())
val inNdefRecords = ndef.ndefMessage.records
Log.d("fdsfsdfsdf", inNdefRecords.toString())
// convert the payload to string and drop 3 characters to get
// rid of the " en" prefix
val payload = inNdefRecords[0].payload
Log.d("fdsfsdfsdf", payload.toString())
// figure out if we need to take out the " en" at the beginning
val textEncoding = if (payload[0] and 128.toByte() == 0.toByte()) "UTF-8" else "UTF-16"
val langCodeLength = payload[0] and 63.toByte()
// create a string starting by skipping the first 3 characters
// based on the language code length
var inMessage = String(
payload,
langCodeLength + 1,
payload.count() - langCodeLength - 1,
charset(textEncoding)
)
// try to convert the message to json
try {
val json = inMessage
Log.d("fdsfsdfsdf", json)
//val json = JsonParser().parse(inMessage)
// ... use json or whatever here
} catch (error: Exception) {
Log.d(
"fdsfsdfsdf",
"NFC tag data seems to invalid:\n\n$inMessage\n\n${error.localizedMessage}"
)
}
// ... do whatever
} catch (error: Exception) {
Log.d("fdsfsdfsdf", "Error attempting to pull tag info: ${error.localizedMessage}")
} finally {
ndef.close()
}
}
fun readTag(tag: Tag?) {
Coroutines.default(this@MainViewModel) {
readNdef(tag)
Log.d(TAG, "readTag(${tag} ${tag?.techList})")
postNFCStatus(NFCStatus.Process)
val stringBuilder: StringBuilder = StringBuilder()
val id: ByteArray? = tag?.id
Log.d("fdsfsdfsdf", "$id")
stringBuilder.append("Tag ID (hex): ${bytesToHex(id!!)} \n")
stringBuilder.append("Tag ID (dec): ${getDec(id)} \n")
stringBuilder.append("Tag ID (reversed): ${getReversed(id)} \n")
stringBuilder.append("Technologies: ")
tag.techList.forEach { tech ->
stringBuilder.append(tech.substring(prefix.length))
stringBuilder.append(", ")
}
stringBuilder.delete(stringBuilder.length - 2, stringBuilder.length)
tag.techList.forEach { tech ->
if (tech.equals(MifareClassic::class.java.name)) {
stringBuilder.append('\n')
val mifareTag: MifareClassic = MifareClassic.get(tag)
val type: String
if (mifareTag.type == MifareClassic.TYPE_CLASSIC) type = "Classic"
else if (mifareTag.type == MifareClassic.TYPE_PLUS) type = "Plus"
else if (mifareTag.type == MifareClassic.TYPE_PRO) type = "Pro"
else type = "Unknown"
stringBuilder.append("Mifare Classic type: $type \n")
stringBuilder.append("Mifare size: ${mifareTag.size} bytes \n")
stringBuilder.append("Mifare sectors: ${mifareTag.sectorCount} \n")
stringBuilder.append("Mifare blocks: ${mifareTag.blockCount}")
}
if (tech.equals(MifareUltralight::class.java.name)) {
stringBuilder.append('\n');
val mifareUlTag: MifareUltralight = MifareUltralight.get(tag);
val type: String
if (mifareUlTag.type == MifareUltralight.TYPE_ULTRALIGHT) type =
"Ultralight"
else if (mifareUlTag.type == MifareUltralight.TYPE_ULTRALIGHT_C) type =
"Ultralight C"
else type = "Unkown"
stringBuilder.append("Mifare Ultralight type: ");
stringBuilder.append(type)
}
}
Log.d(TAG, "Datum: $stringBuilder")
Log.d(ContentValues.TAG, "dumpTagData Return \n $stringBuilder")
postNFCStatus(NFCStatus.Read)
liveTag.emit("${getDateTimeNow()} \n $stringBuilder")
}
}
fun writeNdef(tag: Tag?, context: Context) {
val ndef = Ndef.get(tag)
try {
ndef.connect()
val textRecord1 = NdefRecord.createTextRecord(
"en", // language code (ISO 639-1)
"00000111111222222333333444444555555AABBEF"
// context.getString(R.string.ndef_rcrd1) // text to send
)
val textRecord2 = NdefRecord.createTextRecord(
"en", // language code (ISO 639-1)
"00000111111222222333333444444555555AABBEF"
// context.getString(R.string.ndef_rcrd2) // text to send
)
val textRecord3 = NdefRecord.createTextRecord(
"en", // language code (ISO 639-1)
"00000111111222222333333444444555555AABBEF"
// context.getString(R.string.ndef_rcrd3) // text to send
)
val textRecord4 = NdefRecord.createTextRecord(
"en", // language code (ISO 639-1)
"00000111111222222333333444444555555AABBEF"
//context.getString(R.string.ndef_rcrd4) // text to send
)
val ndefMessage = NdefMessage(textRecord1, textRecord2, textRecord3, textRecord4)
ndef.writeNdefMessage(ndefMessage)
Coroutines.default(this@MainViewModel){
postNFCStatus(NFCStatus.Write)
}
Log.d("fdsfsdfsdf","Edited: ${ndef.ndefMessage}")
} catch (e: Exception) {
Log.d("fdsfsdfsdf", "Error write: ${e.localizedMessage}")
} finally {
ndef.close()
}
}
我想在没有任何转换的情况下编写新的 REcords。
答:
0赞
Andrew
11/5/2023
#1
不要使用 Ndef 文本记录格式,因为这会将语言编码添加到记录中。
将 MimeType 记录与 createMime 一起使用
例如
// Create Record
val textRecord1 = NdefRecord.createMime(
"text/plain" // a standard mimeType
"00000111111222222333333444444555555AABBEF".toByteArray()
)
// Get String
val payloadString = inNdefRecords[0].payload.toString()
您可以使用标准的 MIME 类型,例如或自定义的 MIME 类型,例如text/plain
yourapp/text
认为您可能应该对有效载荷进行一些检查。
例如,检查getTnf()
NdefRecord.TNF_MIME_MEDIA
和
inNdefRecords[0].toMimeType()
是您选择使用的 MIME 类型
评论
0赞
Tural Sufan
11/6/2023
但是“NdefRecord.createMime()”需要数据作为 byteArray。'必需:ByteArray!”
0赞
Andrew
11/6/2023
由于 NFC 硬件仅存储字节数组,因此这是可能的最小转换。您将从 NFC 硬件获得相同的字节数组。
评论