下面readData方法的目的是返回NDEF消息,无论标签支持NDEF格式还是"NDEF可格式化“。
class WritableTag (tag: Tag) {
private val NDEF = Ndef::class.java.canonicalName
private val NDEF_FORMATABLE = NdefFormatable::class.java.canonicalName
private val ndef: Ndef?
private val ndefFormatable: NdefFormatable?
val tagId: String?
get() {
if (ndef != null) {
return Tools.byteArrayToHex(ndef.tag.id)
} else if (ndefFormatable != null) {
return Tools.byteArrayToHex(ndefFormatable.tag.id)
}
return null
}
init {
val technologies = tag.techList
val tagTechs = Arrays.asList(*technologies)
if (tagTechs.contains(NDEF)) {
Log.i("WritableTag", "contains ndef")
ndef = Ndef.get(tag)
ndefFormatable = null
} else if (tagTechs.contains(NDEF_FORMATABLE)) {
Log.i("WritableTag", "contains ndef_formatable")
ndefFormatable = NdefFormatable.get(tag)
ndef = null
} else {
throw FormatException("Tag doesn't support ndef")
}
}
fun readData(): NdefMessage {
if (ndef != null) {
ndef.connect()
if (ndef.isConnected) {
return ndef.ndefMessage
}
} else if (ndefFormatable != null) {
ndefFormatable.connect()
if (ndefFormatable.isConnected) {
return ndefFormatable.ndefMessage // Unresolved reference: ndefMessage
}
}
throw Exception("Cannot read ndef message")
}
}我可以从ndef标记中获取ndefMessage,但不能从ndefFormatable标记中获取。这怎麽可能?
发布于 2021-06-03 23:43:49
由于ndefFormatable标记未处于存储ndefMessage%s的正确状态,因此需要格式化它以存储ndefMessage%s。
不可能从ndefFormatable标签中读取ndefMessage,它基本上是一张空白卡片,表示如果设置为存储ndefMessage,它可以存储它们。
您所能做的就是format它,然后write一个ndefMessage
https://stackoverflow.com/questions/67823172
复制相似问题