【问题标题】:Reading the contents of a NFC Tag读取 NFC 标签的内容
【发布时间】:2012-05-02 17:47:41
【问题描述】:

我正在尝试读取三星 Galaxy Nexus S 上的 NFC 标签。

    @Override
protected void onResume() {
    super.onResume();
    String nachricht = "";
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        Parcelable[] rawMsgs = getIntent()
                .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawMsgs != null) {
            NdefMessage[] msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
                nachricht = nachricht + " " + msgs;
            }

            Log.e("WriteTagApp", nachricht );
        }
    }

}

使用 Log.e("WriteTagApp", nachricht );我的应用程序将标签的消息写入调试器。 它看起来像这样:[Landroid.nfc.NdefMessage;@4184ce18

我似乎无法理解这里发生了什么以及如何获取消息的实际内容。 我该怎么做?

【问题讨论】:

    标签: android tags nfc


    【解决方案1】:

    NDefMessage 不是字符串,它是一个对象。通过连接它,您只需添加其“toString”方法的输出。相反,您需要查看 NdefMessage 中的 NdefRecords 并从其中的有效负载字段中提取数据。

    【讨论】:

    • 啊,谢谢。添加了一个 EditText 字段,我在其中输入有效载荷mNote.setText(new String(messages[0].getRecords()[0].getPayload())); 这样您就可以读取 Text 所在的 Tag 的第一条记录。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    相关资源
    最近更新 更多