问题描述
我的应用程序的AID是F239856324897348
,我已经为其构造了一个SelectAID APDU.现在,我如何实际将其发送到使用主机卡仿真的接收Android设备.
The AID of my app is F239856324897348
and I have constructed a SelectAID APDU for it. Now how do I actually send it to the receiving Android device which is using host card emulation.
我已经创建了我的HCE服务,以使用以下线程中的响应APDU进行响应:如何为以下项定义STORE DATA的APDU:主机卡仿真?
I have a created my HCE service to respond with a response APDU as in this thread: How to define an APDU for STORE DATA for Host Card Emulation?
public static byte[] SelectAID = new byte[]{
(byte) 0xF2, (byte) 0x39, (byte) 0x85, (byte) 0x63,
(byte) 0x24, (byte) 0x89, (byte) 0x73, (byte) 0x48};
private void commandAPDU(byte[] apdu){
//where do I go from here...
}
commandAPDU(SelectAID);
推荐答案
APDU的格式在ISO/IEC 7816-4中定义.一个典型的SELECT(通过AID)命令如下所示:
The format of APDUs is defined in ISO/IEC 7816-4. A typical SELECT (by AID) command looks like this:
+-----+-----+-----+-----+-----+-------------------------+-----+
| CLA | INS | P1 | P2 | Lc | DATA | Le |
+-----+-----+-----+-----+-----+-------------------------+-----+
| 00 | A4 | 04 | 00 | XX | AID | 00 |
+-----+-----+-----+-----+-----+-------------------------+-----+
您可以这样创建它:
private byte[] selectApdu(byte[] aid) {
byte[] commandApdu = new byte[6 + aid.length];
commandApdu[0] = (byte)0x00; // CLA
commandApdu[1] = (byte)0xA4; // INS
commandApdu[2] = (byte)0x04; // P1
commandApdu[3] = (byte)0x00; // P2
commandApdu[4] = (byte)(aid.length & 0x0FF); // Lc
System.arraycopy(aid, 0, commandApdu, 5, aid.length);
commandApdu[commandApdu.length - 1] = (byte)0x00; // Le
return commandApdu;
}
然后您可以将这样的APDU命令发送到通过阅读器模式API找到的标签/HCE设备:
You could then send such APDU commands to a tag/HCE device discovered through the reader-mode API:
public abstract void onTagDiscovered(Tag tag) {
IsoDep isoDep = IsoDep.get(tag);
if (isoDep != null) {
try {
isoDep.connect();
byte[] result = isoDep.transceive(selectApdu(SelectAID));
} except (IOException ex) {
} finally {
try {
isoDep.close();
} except (Exception ignored) {}
}
}
}
这篇关于如何发送命令APDU到HCE设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!