无法从 NdefFormatable 对象获取 Ndef 消息

Cannot get Ndef Message from NdefFormatable object

下面readData方法的objective是returnNDEF报文,标签是否支持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 标签中获取。这怎么可能?

因为 ndefFormatable 标签的状态不适合存储 ndefMessages,需要对其进行格式化以存储 ndefMessages。

无法从 ndefFormatable 标签中读取 ndefMessage,它基本上是一张空白卡片,表明它可以存储 ndefMessage 如果它被设置为存储他们。

你所能做的就是format然后write一个ndefMessage