NFC外部记录返回的格式错误? [英] NFC External record is returning in wrong format?

查看:149
本文介绍了NFC外部记录返回的格式错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功将外部记录写入NFC标签.当我使用第三方标签读取器评估所写的外部记录时,会看到适当的值,该值是单个正整数.

I've successfully written an external record to an NFC tag. When I use a 3rd party tag reader to evaluate the external record that was written, I see the appropriate value, which is a single, positive integer.

但是,当我运行我的代码(如下)以查看有效载荷(外部记录)的值在标签上(使用Toast)以便将该值合并到"if"语句中时,我得到了不同的结果价值观.到目前为止,我已经看到了以下内容:

However, when I run my code (below) to see what the value of the payload (external record) is on the tag (using a Toast) in order to incorporate that value into an "if" statement, I get different values. So far, I've seen the following:

B @ 41fb4278或B @ 41fb1190.

B@41fb4278 or B@41fb1190.

这时,外部记录的值仅为"2".我怎样才能简单地返回/写入2?

At this point, the value of the external record is just "2". How can I just return/write simply 2?

protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);


    if(intent.hasExtra(NfcAdapter.EXTRA_TAG))

    {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        byte[] payload = "2".getBytes();  ///this is where the ID (payload) for the tag is assigned.

        NdefRecord[] ndefRecords = new NdefRecord[2];
        ndefRecords[0] = NdefRecord.createExternal("com.example.bmt_admin", "externaltype", payload);
        ndefRecords[1] = NdefRecord.createApplicationRecord("com.example.bmt_01");
        NdefMessage ndefMessage = new NdefMessage(ndefRecords);
        writeNdefMessage(tag, ndefMessage);

        Toast.makeText(this, "NFC Scan: " + payload, Toast.LENGTH_SHORT).show();
    }

}

感谢您的帮助!

推荐答案

payload定义为byte [].在toast()语句中使用payload时,将其用作指向该数组的指针.因此,您看到的是数组的地址.当您要获取一个字节的字符串表示形式[]时,可以使用例如:

payload is defined as byte[]. When you use payload in your toast() statment, you use it a pointer to that array. Therefore what you see is the address of the array. When you want to get a string representation of a byte[], you can use for example:

String s = new String(payload);

这篇关于NFC外部记录返回的格式错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆