读取NFC标签数据(IsoDep) [英] Read data from NFC tag (IsoDep)

查看:832
本文介绍了读取NFC标签数据(IsoDep)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的新手的 NFC API。

I am newbie in Android NFC API.

目前,我有一个NFC标签,我想提出一个Android应用程序从中读取数据。当我的手机接近足够的NFC标签我的简单的应用程序将启动。但我不知道如何读取NFC标签中的数据。标签使用的 IsoDep 的技术。

Currently, I have a NFC tag, I am making an Android app to read data from it. My simple App is launched when my phone get closer enough to the NFC Tag. But I have no idea how to read the data inside the NFC Tag. The tag uses IsoDep technology.

我目前的code:

@Override
protected void onResume (){
    super.onResume();

    Intent intent = getIntent();
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    IsoDep isoDep = IsoDep.get(tag);

    // How to read data from IsoDep instance?

我GOOGLE了互联网上,我注意到人们发送命令到 IsoDep 得到NFC标签响应,我从响应假设,我们可以在标签解析数据,我看到人们这样做的:

I googled on internet, I notice people are sending commands to IsoDep to get response from NFC Tag, I suppose from the response, we can parse the data in the tag, I saw people doing this:

 //What is the 'command' ? How to define the command?
 //e.g.:
 byte command = (byte) 0x6A
 isoDep.transceive(command)

不过,命令只是一个字节,作为一个新手,实在很难理解发生了什么。我不知道如何来定义命令来读取数据?任何人都可以向我解释?或者是有一个文件,我可以了解命令?

But, the command is just a byte, as a newbie, it is too difficult to understand what is happening. I have no idea how to define the command to read data? Anyone can explain to me? or is there a document I can learn about the command?

一般情况下,我需要如何定义命令和放一些指导;如何解析从响应数据,我想读取存储在标签和放大器的数据;显示UI元素在字符串格式的数据(例如的TextView )。

Generally, I need some guidance on how to define commands & how to parse data from response, I would like to read the data stored in the Tag & show the data in String format in UI element (e.g. TextView).

的*和***

*AND***

我有那些配置(例如AnroidManifest.xml)没有任何问题,请不要引导我如何配置:)

I have no problem with those configurations(e.g. AnroidManifest.xml), please don't guide me on how to configure :)

推荐答案

IsoDep允许你通过使用收发操作的ISO-14443-4连接进行通信。在此协议的应用程序数据单元(APDU)交换。格式是指定的,你会发现在维基百科的描述。

IsoDep allows you to communicate over a ISO-14443-4 connection with the transceive operation. Over this protocol application data units (APDUs) are exchanged. The format is specified, you find a description on Wikipedia.

有关的exaple,要选择与特定应用程序标识符(AID)的智能卡上的应用程序,你可以执行以下命令APDU。结果简单地表示确定(9000)或错误。

For exaple, to select an application on a smart card with a particular application identifier (AID) you would execute the following APDU command. The result simply indicates ok (9000) or an error.

    byte[] SELECT = { 
        (byte) 0x00, // CLA Class           
        (byte) 0xA4, // INS Instruction     
        (byte) 0x04, // P1  Parameter 1
        (byte) 0x00, // P2  Parameter 2
        (byte) 0x0A, // Length
        0x63,0x64,0x63,0x00,0x00,0x00,0x00,0x32,0x32,0x31 // AID
    };

    Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    IsoDep tag = IsoDep.get(tagFromIntent);

    tag.connect();
    byte[] result = tag.transceive(SELECT);
    if (!(result[0] == (byte) 0x90 && result[1] == (byte) 0x00))
        throw new IOException("could not select applet");

该应用程序已被选中

之后,可以执行应用程序特定的命令。该程序通常写在下面的GlobalPlatorm规范JavaCard的。下面的例子执行上返回最多11字节一个字节数组上面所选应用程序methox 4(0×04)。然后,该结果被转换为字符串

After the application has been selected, you can execute application specific commands. The programs are typically written in JavaCard which follows the GlobalPlatorm spec. The following example executes on the above selected application the methox 4 (0x04) which returns a byte array of at most 11 bytes. This result is then converted to a string.

    byte[] GET_STRING = { 
        (byte) 0x80, // CLA Class        
        0x04, // INS Instruction
        0x00, // P1  Parameter 1
        0x00, // P2  Parameter 2
        0x10  // LE  maximal number of bytes expected in result
    };

    result = tag.transceive(GET_STRING);
    int len = result.length;
    if (!(result[len-2]==(byte)0x90&&result[len-1]==(byte) 0x00))
       throw new RuntimeException("could not retrieve msisdn");

    byte[] data = new byte[len-2];
    System.arraycopy(result, 0, data, 0, len-2);
    String str = new String(data).trim();

    tag.close();

这篇关于读取NFC标签数据(IsoDep)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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