从NFC标签读取数据(IsoDep)

Mel*_*lon 14 android nfc android-intent

我是Android NFC API的新手.

目前,我有一个NFC标签,我正在制作一个Android应用程序来从中读取数据.当我的手机足够接近NFC标签时,我的简单应用就会启动.但我不知道如何读取NFC标签内的数据.该标签使用IsoDep技术.

我目前的代码:

@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?
Run Code Online (Sandbox Code Playgroud)

我在互联网上搜索,我注意到人们正在发送命令以IsoDep获取NFC标签的响应,我想从响应中,我们可以解析标签中的数据,我看到人们这样做:

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

但是,这个命令只是一个byte新手,很难理解发生了什么.我不知道如何定义读取数据的命令?有人可以向我解释一下吗?或者是否有我可以了解该命令的文件?

一般来说,我需要一些关于如何定义命令以及如何从响应中解析数据的指导,我想读取存储在Tag中的数据并在UI元素中以String格式显示数据(例如TextView).

*和***

我对这些配置没有问题(例如AnroidManifest.xml),请不要指导我如何配置:)

Dom*_*nik 17

IsoDep允许您通过ISO-14443-4连接与transceive操作进行通信.通过该协议,交换应用数据单元(APDU).指定格式,您可以在维基百科上找到说明.

例如,要在具有特定应用程序标识符(AID)的智能卡上选择应用程序,您将执行以下APDU命令.结果只表示ok(9000)或错误.

    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");
Run Code Online (Sandbox Code Playgroud)

选择应用程序后,您可以执行特定于应用程序的命令.这些程序通常用JavaCard编写,遵循GlobalPlatorm规范.以下示例在上面选择的应用程序上执行方法4(0x04),该方法返回最多11个字节的字节数组.然后将此结果转换为字符串.

    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();
Run Code Online (Sandbox Code Playgroud)