Android的NfcV保持安静命令 [英] Android NfcV Stay Quiet Command

查看:363
本文介绍了Android的NfcV保持安静命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个Android手机(的Nexus 4)不同NfcV的命令。目前,我使用这些标签。根据数据表,就应该支持安静指令。

不幸的是,我不能让下面的源$ C ​​$ C才能正常工作。我期望的库存和读单块命令后,保持安静命令失败,因为这两个命令都没有得到解决,这在安静状态的标签应该忽略。
但我仍然得到含有UID或有效载荷来自标签的正确答案。

另外,我得到一个通过执行保持安静命令标签丢失的异常。然而,根据ISO 15693-3标签应该给这个命令,这可能导致此异常,反正没有任何反应。

不过,我猜的东西的毛病保持安静命令,在此code,但我想不出什么。

任何帮助是AP preciated。

来源$ C ​​$ C:

  NfcV nfcv = NfcV.get(MTAG);
                尝试{
                    nfcv.connect(); //连接
                }赶上(IOException异常五){
                    mUIDs = mUIDs +\\ nConnection错误1;
                }
                字节[] = response1 {(字节)为0x00};
                尝试{
                    字节[] = UIDreq1 InventoryRequest(); //库存请求
                    response1 = nfcv.transceive(UIDreq1);
                }赶上(IOException异常五){
                    mUIDs = mUIDs +\\ nInventory错误1;
                }
                mUIDs = mUIDs +\\ n+ bytesToHex(response1);
                字节[] UID = response2UID(response1);
                mUIDs = mUIDs +\\ n+ bytesToHex(UID);
                尝试{
                    字节[] =命令ReadSingleBlockUnadressed((字节)0×04); //读取单段
                    字节[]数据= nfcv.transceive(命令);
                    mUIDs = mUIDs +\\ n+ bytesToHex(数据);
                }赶上(IOException异常五){
                    Log.e(读单块,e.getMessage());
                    mUIDs = mUIDs +\\ nReading错误;
                }
                尝试{
                    尝试{
                        字节[] =命令StayQuiet(UID); //保持安静,应该失败
                        nfcv.transceive(命令);
                    }赶上(TagLostException E){
                        Log.e(保持安静,e.getMessage());
                        mUIDs = mUIDs +\\ nTag丢失了。
                    }
                    }赶上(IOException异常X){
                    mUIDs = mUIDs +\\ nStay安静错误;
                }
                尝试{
                    字节[] =命令ReadSingleBlockUnadressed((字节)0×04); //读取单段
                    字节[]数据= nfcv.transceive(命令);
                    mUIDs = mUIDs +\\ n+ bytesToHex(数据);
                }赶上(IOException异常五){
                    Log.e(读单块,e.getMessage());
                    mUIDs = mUIDs +\\ nReading错误;
                }                尝试{
                    字节[] = UIDreq2 InventoryRequest(); //库存请求,应该会失败
                    字节[] =响应2 nfcv.transceive(UIDreq2);
                    mUIDs = mUIDs +\\ n+ bytesToHex(响应2);
                }赶上(IOException异常五){
                    mUIDs = mUIDs +\\ nInventory错误2
                }
                尝试{
                    nfcv.close(); //断开
                }赶上(IOException异常X){
                    mUIDs = mUIDs +\\ nDisconnection错误1;
                }
                mTagTextView.setText(mUIDs);

具有以下功能:

 最终保护静态的char [] hexArray =0123456789ABCDEF.toCharArray();
公共静态字符串bytesToHex(字节[]字节){
    的char [] = hexChars新的char [bytes.length * 2];
    INT伏;
    对于(INT J = 0; J< bytes.length; J ++){
        V =字节[J]放大器; 0xFF的;
        hexChars [J * 2] = hexArray [V>>> 4〕;
        hexChars [J * 2 + 1] = hexArray [V&安培;为0x0F];
    }
    返回新的String(hexChars);
}
私人字节[] response2UID(字节[]响应){
    字节[] UID =新的字节[response.length-2];
    的for(int i = 2; I< response.length;我++){
        UID [I-2] =反应[I];
    }
    返回UID;
}私人字节[] ReadSingleBlockUnadressed(字节blocknumber){
    返回新的字节[] {(字节)为0x00(字节)为0x20,blocknumber};
}
私人字节[] StayQuiet(字节[] UID){
    字节[] = beQuiet新的字节[] {(字节)0x04的(字节)0X02};
    字节[] =命令结合(beQuiet,UID);
    返回的命令;
}私人字节[] InventoryRequest(){
    返回新的字节[] {(字节)0X24(字节)为0x01(字节)为0x00};
}


解决方案

当你发送的STAY QUITE命令你把标签从选择未选中状态。

然后你用一个0x00的标志值送出读单块命令。

根据ISO / IEC 15693-3标准,这意味着选择标志(位5)是零。该标准定义了该位的用法如下:

 位5 = 0:请求应以任何VICC执行根据
寻址标志的设置位5 = 1:请求只应在选定的国家VICC执行

寻址标志在这里被定义(其值为零):

 位6 = 0:请求不解决。 UID字段不为present。它应是
任何VICC执行。位6 = 1:请求被处理。 UID字段是present。它应是
只有那些UID的UID匹配的VICC执行
在请求中指定。

随着你已指示标记,即使不选择它来回答这个问题你已经在你读单块命令使用的标志。这就是为什么它的工作原理。

有关您的使用,如果你想选择标志(第5位)是一个和寻址标志为零。

我建议你看一看在ISO / IEC 15693-3标准。它的拷贝可以在网上找到这里: http://www.waazaa.org /download/fcd-15693-3.pdf

顺便说一句,你得到的标签被丢失异常,因为NFC服务不断检查标签的presence。在ISO15693的情况下,它可能不会因此受到库存,因为你的背后发出存货的命令是所有ISO15693标签了解几个命令之一。

您没有这个控制,所以很可能会与你的尝试干扰沉默了标记。

I'm trying to implement different NfcV-commands on an Android phone (Nexus 4). Currently, I'm using these tags. According to the datasheet, they should support the "Be Quiet"-command.

Unfortunately, I can't get the source code below to work correctly. I'd expect the "Inventory" and "Read Single Block" commands after the "Stay Quiet" command to fail because both commands are unaddressed, which a tag in the Quiet state should ignore. But I still get the correct answer containing UID or payload from the tag.

Also, I get a "Tag was lost" exception by executing the "Stay Quiet" command. However, according to ISO 15693-3 the tag should give no response to this command, which could lead to this exception, anyway.

Still, I'm guessing something's wrong with the "Stay Quiet" command in this code but I can't figure out what.

Any help is appreciated.

Source Code:

            NfcV nfcv = NfcV.get(mtag);


                try{
                    nfcv.connect();                                                             //Connect
                } catch(IOException e){
                    mUIDs = mUIDs + "\nConnection Error 1";
                }


                byte[] response1 = {(byte) 0x00};
                try{
                    byte[] UIDreq1 = InventoryRequest();                                        //Inventory Request
                    response1 = nfcv.transceive(UIDreq1);
                } catch(IOException e){
                    mUIDs = mUIDs + "\nInventory Error 1";
                }
                mUIDs = mUIDs +"\n" + bytesToHex(response1);
                byte[] UID = response2UID(response1);
                mUIDs = mUIDs + "\n" + bytesToHex(UID);


                try{
                    byte[] command = ReadSingleBlockUnadressed((byte) 0x04);                    //Reading Single Block
                    byte[] data = nfcv.transceive(command);
                    mUIDs = mUIDs +"\n"+bytesToHex(data);
                }catch(IOException e){
                    Log.e("Reading Single Block", e.getMessage());
                    mUIDs = mUIDs + "\nReading Error";
                }


                try{
                    try{
                        byte[] command = StayQuiet(UID);                                        //Stay Quiet, SHOULD FAIL
                        nfcv.transceive(command);
                    }catch(TagLostException e){
                        Log.e("Stay Quiet", e.getMessage());
                        mUIDs = mUIDs + "\nTag was lost.";
                    }
                    }catch(IOException x){
                    mUIDs = mUIDs + "\nStay Quiet Error";
                }


                try{
                    byte[] command = ReadSingleBlockUnadressed((byte) 0x04);                    //Reading Single Block
                    byte[] data = nfcv.transceive(command);
                    mUIDs = mUIDs +"\n"+bytesToHex(data);
                }catch(IOException e){
                    Log.e("Reading Single Block", e.getMessage());
                    mUIDs = mUIDs + "\nReading Error";
                }

                try{
                    byte[] UIDreq2 = InventoryRequest();                                        //Inventory Request, SHOULD FAIL
                    byte[] response2 = nfcv.transceive(UIDreq2);
                    mUIDs = mUIDs +"\n" + bytesToHex(response2);
                } catch(IOException e){
                    mUIDs = mUIDs + "\nInventory Error 2";
                }


                try{
                    nfcv.close();                                                               //Disconnect
                } catch(IOException x){
                    mUIDs = mUIDs + "\nDisconnection Error 1";
                }


                mTagTextView.setText(mUIDs);

With the following functions:

final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
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);
}
private byte[] response2UID(byte[] response){
    byte[] UID = new byte[response.length-2];
    for(int i = 2; i < response.length; i++){
        UID[i-2]=response[i];
    }
    return UID;
}

private byte[] ReadSingleBlockUnadressed(byte blocknumber){
    return new byte[] {(byte) 0x00, (byte) 0x20, blocknumber};
}
private byte[] StayQuiet(byte[] UID){
    byte[] beQuiet = new byte[] {(byte) 0x04, (byte) 0x02}; 
    byte[] command = combine(beQuiet, UID);
    return command;
}

private byte[] InventoryRequest(){
    return new byte[] { (byte) 0x24, (byte) 0x01, (byte) 0x00};
}

解决方案

When you're sending the STAY QUITE command you bring the tag from selected to unselected state.

Then you're sending out the READ SINGLE BLOCK command with a flag value of 0x00.

According to the ISO/IEC 15693-3 standard this means that the Select_flag (Bit 5) is zero. The standard defines the usage of this bit as follows:

Bit 5 = 0: Request shall be executed by any VICC according to the
setting of Address_flag

Bit 5 = 1: Request shall be executed only by VICC in selected state

The Address_flag is defined here (it value is zero):

Bit 6 = 0: Request is not addressed. UID field is not present. It shall be
executed by any VICC.

Bit 6 = 1: Request is addressed. UID field is present. It shall be
executed only by the VICC whose UID matches the UID
specified in the request.

With the flags that you've used in your READ SINGLE BLOCK command you have instructed the tag to answer even if it is not selected. That's why it works.

For your use-case you want the Select_flag (Bit 5) to be one and the Address_flag zero.

I suggest that you take a look at the ISO/IEC 15693-3 standard. A copy of it can be found online here: http://www.waazaa.org/download/fcd-15693-3.pdf

Btw, you get the "Tag was lost" exception because the NFC Service is constantly checking for the presence of the tag. In the case of ISO15693 it probably does so by sending out an INVENTORY command behind your back since INVENTORY is one of the few commands that all ISO15693 tags understand.

You have no control about this, so it is likely that it will interfere with your tries to silence out the tag.

这篇关于Android的NfcV保持安静命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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