如何读取 Android 上 NFC 标签的唯一 ID?
Tag myTag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Log.i("tag ID", myTag.getId().toString());
这给了我一个像“[B@40521c40”这样的ID,但这个ID每次读取都会改变。
任何帮助将不胜感激。
谢谢。
Tag myTag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Log.i("tag ID", myTag.getId().toString());
This gives me an ID like "[B@40521c40" but this ID changes every read.
Any help would be greatly appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您仍然需要将字节转换为字符串:
you still need to convert the byte to string:
我遇到了类似的问题并且可以解决它。问题是转换为字符串。
myTag.getId() 返回字节数组。您应该将这些字节转换为十六进制字符串。我使用了在 stackoverflow.com 中找到的以下函数
I ran in to similar issue and could resolve it. The issue is the conversion to string.
myTag.getId() returns byte array. You should convert these bytes to hex string. I used the following function that I found here in stackoverflow.com
Kotlin
我通过将字节数组 ID 传递给以下函数解决了这个问题:
Java 中此函数的来源:
https://gist.github.com/luixal/5768921
Kotlin
I solved this by passing the byte-array ID to following function:
Source of this function in java:
https://gist.github.com/luixal/5768921
}
}
该解决方案比我发现的所有解决方案更高效且更具可读性:
This solution is more efficient and readable than all the ones I found: