APDU指令学习:如何读取NFC手机钱包的余额


// select PSF (1PAY.SYS.DDF01)

/*--------------------------------------------------------------*/
beijing.Util.log("select file: tag.selectByName(DFN_PSE)");
if (tag.selectByName(DFN_PSE).isOkey()) {


protected final static byte[] DFN_PSE = { (byte) '1', (byte) 'P',
(byte) 'A', (byte) 'Y', (byte) '.', (byte) 'S', (byte) 'Y',
(byte) 'S', (byte) '.', (byte) 'D', (byte) 'D', (byte) 'F',
(byte) '0', (byte) '1', }; //1PAY.SYS.DDF01


name = DFN_PSE;

ByteBuffer buff = ByteBuffer.allocate(name.length + 6);
buff.put((byte) 0x00); // CLA Class
buff.put((byte) 0xA4); // INS Instruction
buff.put((byte) 0x04); // P1 Parameter 1 SELECT by NAME
buff.put((byte) 0x00); // P2 Parameter 2
buff.put((byte) name.length); // Lc
buff.put(name); buff.put((byte) 0x00); // Le

SELECT命令参考
第1部分电子钱包电子存折应用卡片规范 之 
  表33 SELECT 命令报文
  表34 SELECT 命令引用控制参数


如果返回结果结尾是 0x9000  ,就SELECT成功


==============================================
SFI_EXTRA_LOG = 4;
// read card info file, binary (4)
/*--------------------------------------------------------------*/
INFO = tag.readBinary(SFI_EXTRA_LOG);
==============================================
SFI_EXTRA_CNT = 5;
// read card operation file, binary (5)
/*--------------------------------------------------------------*/
CNT = tag.readBinary(SFI_EXTRA_CNT);

public Response readBinary(int sfi) {
final byte[] cmd = { (byte) 0x00, // CLA Class
(byte) 0xB0, // INS Instruction
(byte) (0x00000080 | (sfi & 0x1F)), // P1 Parameter 1
(byte) 0x00, // P2 Parameter 2
(byte) 0x00, // Le
};
CNT = tag.readBinary(SFI_EXTRA_CNT);


参考:
第1部分电子钱包电子存折应用卡片规范 之 
表25 READ BINARY 命令报文
==============================================
读余额之前的操作
// select Main Application  
byte[] DFI_EP = { (byte) 0x10, (byte) 0x01 };
/*--------------------------------------------------------------*/
if (tag.selectByID(DFI_EP).isOkey()) {


public Response selectByID(byte[] name) {
ByteBuffer buff = ByteBuffer.allocate(name.length + 6);
buff.put((byte) 0x00); // CLA Class
buff.put((byte) 0xA4); // INS Instruction
buff.put((byte) 0x00); // P1 Parameter 1 SELECT by ID
buff.put((byte) 0x00); // P2 Parameter 2
buff.put((byte) name.length); // Lc
buff.put(name); buff.put((byte) 0x00); // Le


return new Response(transceive(buff.array()));
==============================================
读余额
CASH = tag.getBalance(true);


public Response getBalance(boolean isEP) {
final byte[] cmd = { (byte) 0x80, // CLA Class
(byte) 0x5C, // INS Instruction
(byte) 0x00, // P1 Parameter 1
(byte) (isEP ? 2 : 1), // P2 Parameter 2
(byte) 0x04, // Le
};


参考:
http://blog.csdn.net/jennyvenus/article/details/4183994
//Get Balance                       (defined by PBOC)
80 5C 00 02 04


PBOC 第2部分(电子钱包_电子存折应用规范)
表2 命令的类别字节和指令字节
读余额(GET BALANCE)
‘80’ ‘5C’ ‘00’ ‘0X’ 
阅读更多

更多精彩内容