如何读取 Android 上 NFC 标签的唯一 ID?

发布于 2024-11-08 07:58:21 字数 214 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清音悠歌 2024-11-15 07:58:21

您仍然需要将字节转换为字符串:

private String bytesToHexString(byte[] src) {
    StringBuilder stringBuilder = new StringBuilder("0x");
    if (src == null || src.length <= 0) {
        return null;
    }

    char[] buffer = new char[2];
    for (int i = 0; i < src.length; i++) {
        buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);  
        buffer[1] = Character.forDigit(src[i] & 0x0F, 16);  
        System.out.println(buffer);
        stringBuilder.append(buffer);
    }

    return stringBuilder.toString();
}

you still need to convert the byte to string:

private String bytesToHexString(byte[] src) {
    StringBuilder stringBuilder = new StringBuilder("0x");
    if (src == null || src.length <= 0) {
        return null;
    }

    char[] buffer = new char[2];
    for (int i = 0; i < src.length; i++) {
        buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);  
        buffer[1] = Character.forDigit(src[i] & 0x0F, 16);  
        System.out.println(buffer);
        stringBuilder.append(buffer);
    }

    return stringBuilder.toString();
}
无声情话 2024-11-15 07:58:21

我遇到了类似的问题并且可以解决它。问题是转换为字符串。

myTag.getId() 返回字节数组。您应该将这些字节转换为十六进制字符串。我使用了在 stackoverflow.com 中找到的以下函数

    final protected static char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
public static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    int v;
    for ( int j = 0; j < bytes.length; j++ ) {
        v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}

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

    final protected static char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
public static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    int v;
    for ( int j = 0; j < bytes.length; j++ ) {
        v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}
情深已缘浅 2024-11-15 07:58:21

Kotlin

我通过将字节数组 ID 传递给以下函数解决了这个问题:

private fun ByteArrayToHexString(inarray: ByteArray): String? {
    var i: Int
    var j: Int
    var `in`: Int
    val hex = arrayOf(
        "0",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "A",
        "B",
        "C",
        "D",
        "E",
        "F"
    )
    var out = ""
    j = 0
    while (j < inarray.size) {
        `in` = inarray[j].toInt() and 0xff
        i = `in` shr 4 and 0x0f
        out += hex[i]
        i = `in` and 0x0f
        out += hex[i]
        ++j
    }
    return out
}

Java 中此函数的来源:
https://gist.github.com/luixal/5768921

Kotlin

I solved this by passing the byte-array ID to following function:

private fun ByteArrayToHexString(inarray: ByteArray): String? {
    var i: Int
    var j: Int
    var `in`: Int
    val hex = arrayOf(
        "0",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "A",
        "B",
        "C",
        "D",
        "E",
        "F"
    )
    var out = ""
    j = 0
    while (j < inarray.size) {
        `in` = inarray[j].toInt() and 0xff
        i = `in` shr 4 and 0x0f
        out += hex[i]
        i = `in` and 0x0f
        out += hex[i]
        ++j
    }
    return out
}

Source of this function in java:
https://gist.github.com/luixal/5768921

月亮坠入山谷 2024-11-15 07:58:21
public String getHexValue(final byte[] buffer) {
if (buffer == null || buffer.length == 0) {
    return ("0x" + "[none]");
}

final StringBuffer sb = new StringBuffer();
sb.append("0x");
for (int i = 0; i < buffer.length - 1; i++) {
    sb.append(String.format("%02X%s", buffer[i], ""));
}
sb.append(String.format("%02X", buffer[buffer.length - 1]));

return sb.toString();

}

public String getHexValue(final byte[] buffer) {
if (buffer == null || buffer.length == 0) {
    return ("0x" + "[none]");
}

final StringBuffer sb = new StringBuffer();
sb.append("0x");
for (int i = 0; i < buffer.length - 1; i++) {
    sb.append(String.format("%02X%s", buffer[i], ""));
}
sb.append(String.format("%02X", buffer[buffer.length - 1]));

return sb.toString();

}

苏别ゝ 2024-11-15 07:58:21

该解决方案比我发现的所有解决方案更高效且更具可读性:

private static final int HEX_RADIX = 16;

private String bytesToHexString(byte[] src) {
    // Check if the input array is null or empty
    if (src == null || src.length == 0) {
        return null;
    }

    // Create a StringBuilder with an initial capacity based on the length of the input array
    StringBuilder stringBuilder = new StringBuilder(src.length * 2);

    // Iterate through each byte in the input array using an enhanced for loop
    for (byte b : src) {
        // Use String.format to convert each byte to a two-digit lowercase hexadecimal representation
        // The format specifier "%02x" ensures leading zeros are added if needed
        stringBuilder.append(String.format("%02x", b));
    }

    // Convert the StringBuilder to a String and return the result
    return stringBuilder.toString();
}

This solution is more efficient and readable than all the ones I found:

private static final int HEX_RADIX = 16;

private String bytesToHexString(byte[] src) {
    // Check if the input array is null or empty
    if (src == null || src.length == 0) {
        return null;
    }

    // Create a StringBuilder with an initial capacity based on the length of the input array
    StringBuilder stringBuilder = new StringBuilder(src.length * 2);

    // Iterate through each byte in the input array using an enhanced for loop
    for (byte b : src) {
        // Use String.format to convert each byte to a two-digit lowercase hexadecimal representation
        // The format specifier "%02x" ensures leading zeros are added if needed
        stringBuilder.append(String.format("%02x", b));
    }

    // Convert the StringBuilder to a String and return the result
    return stringBuilder.toString();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文