【发布时间】:2021-06-03 14:18:04
【问题描述】:
下面readData方法的目的是返回NDEF消息,标签是否支持NDEF格式或“NDEF Formatable”。
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 标记中获取。这怎么可能?
【问题讨论】: