Android Nfc 示例演示 - 仅从标签读取虚假信息

发布于 2024-10-18 08:14:22 字数 186 浏览 2 评论 0原文

我刚刚从 google 安装了 Nfc 演示,但它没有读取标签中的信息。->它只是提供一些 fakeTag 信息。有人知道我可以在哪里更改示例以从 nfc 标签读取吗?或者有人有 Nexus 的可用 NFC 演示吗?

如果我们可以将 nfc 演示带到工作中,那么许多人就有可能自己开发一个 nfc 演示。

此致 亚历山大

I just installed the Nfc Demo from google, but it doesn´t read the information from the Tag.-> It just provides some fakeTag information. Has anybody an idea, where I can change the sample to read from the nfc Tag? Or has somebody a working nfc demo for the nexus?

If we could bring a nfc demo to work, many people would have the possibility to develop a nfc demo on their own.

Best regards
Alexander

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

彻夜缠绵 2024-10-25 08:14:22

我在获取标签 ID 时遇到了同样的问题。我屏幕上显示了一些 B@2346323143 样式的数据。我让它像这样工作:

byte[] byte_id =intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);

您需要将 byte[] 转换为十六进制字符串。例如使用以下方法。

private static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1',
        (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6',
        (byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B',
        (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F' };

public static String getHexString(byte[] raw, int len) {
    byte[] hex = new byte[2 * len];
    int index = 0;
    int pos = 0;

    for (byte b : raw) {
        if (pos >= len)
            break;

        pos++;
        int v = b & 0xFF;
        hex[index++] = HEX_CHAR_TABLE[v >>> 4];
        hex[index++] = HEX_CHAR_TABLE[v & 0xF];
    }

    return new String(hex);
}

I had the same problem getting tag id. I got some B@2346323143 style data to screen. I got it to work like this:

byte[] byte_id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);

You need to convert byte[] to hex string. For example using following method.

private static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1',
        (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6',
        (byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B',
        (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F' };

public static String getHexString(byte[] raw, int len) {
    byte[] hex = new byte[2 * len];
    int index = 0;
    int pos = 0;

    for (byte b : raw) {
        if (pos >= len)
            break;

        pos++;
        int v = b & 0xFF;
        hex[index++] = HEX_CHAR_TABLE[v >>> 4];
        hex[index++] = HEX_CHAR_TABLE[v & 0xF];
    }

    return new String(hex);
}
小嗲 2024-10-25 08:14:22

NfcDemo 有两个部分。有检测器活动,它响应 NFC 标签意图,然后有 FakeTag 活动,它允许您向第一部分发送假标签意图。但只要启用了 NFC,第一部分也会检测真正的 NFC 标签。检查“设置”->“无线查看 NFC 是否已打开。如果是,并且您安装了 NfcDemo,您应该能够检测到 NFC 标签。但是,NfcDemo 仅配置为检测 NDEF 标签,因此如果您有其他类型的 NFC 标签(例如 Mifare Classic),您将需要获取另一个应用程序,或者修改 NfcDemo 以处理其他 NFC 标签类型。

There are two parts to the NfcDemo. There is the detector activity, which responds to NFC tag intents, then there is the FakeTag activity which allows you to send fake tag intents to the first part. But the first part will detect real NFC tags too, as long as NFC is enabled. Check under Settings -> Wireless to see if NFC is turned on. If it is and you have the NfcDemo installed, you should be able to detect NFC tags. However, the NfcDemo is only configured to detect NDEF tags, so if you have some other type of NFC tag (e.g., Mifare Classic), you'll either need to get another app, or modify NfcDemo to handle the other NFC tag types.

笛声青案梦长安 2024-10-25 08:14:22

我编写了一个具有一些基本 NFC 功能的类,希望它可以帮助其他人获得读取某些 NFC 标签的解决方案。

public class StatusMessage extends Activity {

/**
 * Returns the Status of the Nfc Device with a String "enabled" or "disabled"
 * \return Status NfcDevice
 * @author Falkenstein
 * 
 */
public static String getStatusNfcDevice () {
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter();
if (nfcAdapter.isEnabled()) { 
    String status = "enabled";

return status;
    }
    else {
        String status = "disabled";
        return status;


    }

}
/**
 * Returns the TagId. Needs an Intent. So you have to get you intent from your "main" activity and give it to the method -> just add the following   *lines in your "main class"
     *Intent intent =new Intent();
    *System.out.println(com.example.StatusMessage.getNfcAdapterExtraID(intent));
 *@author Falkenstein
 */ 
public static String getNfcAdapterExtraID (Intent intent) {
    byte[] byte_id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
    return byte_id.toString();
}


/**
 * Converts a byte to a String.
 * @param input
 * @return byte
 */
public String byteToStr(byte[] input) {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < input.length; i++)
        if (input[i] != 0) {
            buffer.append( new Character((char)input[i]).toString());
        }
    return buffer.toString();
}



/**
 * Converts a String to a Byte
 * @param input
 * @return
 */ 
public byte[] strToByte(String input) {

    byte[] buffer = new byte[(input.length()+1)*2];
    for (int i = 0; i < buffer.length-2; i = i+2) {

        buffer[i] = (byte)input.charAt(i/2);
        buffer[i+1] = 0;
    }
    buffer[buffer.length-2] = 0;
    buffer[buffer.length-1] = 0;


    return buffer;
}

I have written a class with some basic NFC function, hopefully it helps someone else to get a solution of reading some NFC Tag.

public class StatusMessage extends Activity {

/**
 * Returns the Status of the Nfc Device with a String "enabled" or "disabled"
 * \return Status NfcDevice
 * @author Falkenstein
 * 
 */
public static String getStatusNfcDevice () {
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter();
if (nfcAdapter.isEnabled()) { 
    String status = "enabled";

return status;
    }
    else {
        String status = "disabled";
        return status;


    }

}
/**
 * Returns the TagId. Needs an Intent. So you have to get you intent from your "main" activity and give it to the method -> just add the following   *lines in your "main class"
     *Intent intent =new Intent();
    *System.out.println(com.example.StatusMessage.getNfcAdapterExtraID(intent));
 *@author Falkenstein
 */ 
public static String getNfcAdapterExtraID (Intent intent) {
    byte[] byte_id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
    return byte_id.toString();
}


/**
 * Converts a byte to a String.
 * @param input
 * @return byte
 */
public String byteToStr(byte[] input) {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < input.length; i++)
        if (input[i] != 0) {
            buffer.append( new Character((char)input[i]).toString());
        }
    return buffer.toString();
}



/**
 * Converts a String to a Byte
 * @param input
 * @return
 */ 
public byte[] strToByte(String input) {

    byte[] buffer = new byte[(input.length()+1)*2];
    for (int i = 0; i < buffer.length-2; i = i+2) {

        buffer[i] = (byte)input.charAt(i/2);
        buffer[i+1] = 0;
    }
    buffer[buffer.length-2] = 0;
    buffer[buffer.length-1] = 0;


    return buffer;
}
勿忘心安 2024-10-25 08:14:22

我为 Android 上的 NFC 编写了一些工具,其中包括一个可用的示例项目供阅读并编写真实的标签。我还做了一个简单的重写您可能感兴趣的 NFCDemo 项目的

我还向 NFC Developer 应用程序,以便更多人可以使用 NFC,即无需 NFC 设备。

I've written some tools for NFC on Android, including a working example project for reading and writing real tags. I've also done a simple rewrite of the NFCDemo project you might be interested in.

I've also added broadcast send / receive capability to the NFC Developer app so that more people can play with NFC, i.e. without needing an NFC device.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文