This article is from http://blog.csdn.net/hellogv/, the citation must be noted!
Recent NFC payment is quite fire, while the National Day home, learning under the Android card simulation (host-based card Emulation). HCE is characterized by analog intelligent IC cards (ISO 7816-4), which can be used in financial and industrial applications, and accordingly, the CardReader example uses ISODEP.
The Intelligent IC card itself is a microcomputer, common for the Java Card platform, especially the multi-function card (such as joint card), Java card than J2ME more limited hardware. The Java card can run one or more Java applets, which are card applications, such as a bank card that can be used to swipe a bus, which may contain 2 applets. Each applet has a aid, processing terminal (swipe device) through the aid to find the corresponding card application ( receiving terminal to send a SELECT command to the card), the processing terminal to find the corresponding card application can be interactive data, interactive data is generally ciphertext, not online decryption, Using symmetric algorithms, the asymmetric and symmetric algorithms can be used to decrypt the words on-line.
HCE is a software simulation of the Intelligent IC card, so there will be aid. This article cardemulation to register only one aid. the code of this article was changed from Android 4.4 Sample, without altering cardemulation, simplifying CardReader and supporting Android 2.3 system, adapting to the low version of the NFC phone to do the card reading of the virtual card. use Nubian Z7 Max to do cardemulation, millet 2A for CardReader. The code for this article can be downloaded to http://pan.baidu.com/share/link?shareid=3865907609&uk=1765623201. Personally feel that reading the CardReader core code is a better way to understand the process of interacting with 2 of people:
Public final class CardReader {private static final String TAG = "Loyaltycardreader"; AID for our loyalty card service. private static final String Sample_loyalty_card_aid = "F222222222"; ISO-DEP command HEADER for selecting an AID. Format: [Class | instruction | Parameter 1 | Parameter 2] private static final String Select_apdu_header = "00a40400"; "OK" status word sent in response to SELECT AID command (0x9000) private static final byte[] select_ok_sw = {(byte) 0x90, (Byte) 0x00};p ublic static string[][] techlists;public static intentfilter[] Filters;static {try {//the tech lists U Sed to perform matching for dispatching of the action_tech_discovered intenttechlists = new string[][] {{IsoDep.class.ge Tname ()},{NfcV.class.getName ()}, {NfcF.class.getName ()},}; FILTERS = new intentfilter[] {new Intentfilter (nfcadapter.action_tech_discovered, "*/*")};} catch (Exception e) {}} public static String load (parcelable parcelable, Resources res) {//filter out all types of NFC standard data from parcelable final tag tag = (tag) parcelable;final isodep ISODEP = isodep.get (tag); LOG.E ("Nfctag ID", Util.tohexstring (Tag.getid (), 0, Tag.getid (). Length)),//isodep.transceive ("$". GetBytes ()). ToString ()); if (ISODEP = = null) {return null;} try {//Connect to the remote NFC device isodep.connect (); Build SELECT AID Command for our loyalty card service. This command tells the remote device which service, we wish to communicate with. LOG.E (TAG, "Requesting remote AID:" + sample_loyalty_card_aid); byte[] Command = BUILDSELECTAPDU (Sample_loyalty_card_aid); Send command to remote device LOG.E (TAG, "sending:" + bytearraytohexstring (command)); Byte[] result = isodep.transceive (command); If AID is successfully selected, 0x9000 are returned as the status word (last 2//bytes of the result) by Co Nvention. Everything before the status word is Optional payload, which is used this account number. int resultlength = Result.length; Byte[] Statusword = {Result[resultlength-2], result[resultlength-1]}; byte[] Payload = arrays.copyof (result, resultLength-2); if (Arrays.equals (SELECT_OK_SW, Statusword)) {//The remote NFC device would immediately respond with its s tored Account Number String accountnumber = new String (Payload, "UTF-8"); LOG.E (TAG, "Received:" + accountnumber); return accountnumber; }} catch (IOException e) {log.e (TAG, "Error Communicating with Card:" + e.tostring ()); } return null; /** * Build APDU for SELECT AID command. This command indicates which service a reader was * interested in communicating with. See ISO 7816-4. * * @param aid Application ID (AID) to select * @return APDU for SELECT Aid Command */ public static byte[] BUILDSELECTAPDU (String aid) {//Format: [CLASS | instruction | PARAMETER 1 | PARAMETER 2 | LENGTH | DATA] Return Hexstringtobytearray (Select_apdu_header + String.Format ("%02x", Aid.length ()/2) + aid); }/** * Utility class to convert a byte array to a hexadecimal string. * * @param bytes bytes to convert * @return String, containing hexadecimal representation. */public static String bytearraytohexstring (byte[] bytes) {final char[] Hexarray = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '}; 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); }/** * Utility class to convert a hexadecimal string to a byte string. * * ≪p>behavior with input strings containing non-hexadecimal characters is undefined. * * @param s String containing hexadecimal characters to convert * @return Byte array generated from input */ public static byte[] Hexstringtobytearray (String s) {int len = s.length (); byte[] data = new BYTE[LEN/2]; for (int i = 0; i < len; i + = 2) {DATA[I/2] = (byte) ((Character.digit (S.charat (i), +) << 4) + Character.digit (S.charat (i+1), 16)); } return data; }}
To put it simply:
1. CardReader Use Sample_loyalty_card_aid + select_apdu_header generate SELECT APDU;
2. After CardReader sends select APDU to Cardemulation, Cardemulation returns SELECT_OK_SW + AccountNumber.
among them Sample_loyalty_card_aid,select_apdu_header,select_ok_sw must be the CardReader and the cardemulation both define well.
Basic use of Android HCE