萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> Android HCE的基本使用詳解

Android HCE的基本使用詳解

這是我們為大家提供的一篇關於Android HCE的基本使用詳解的文章,接下來就讓我們一起來了解一下吧!

最近NFC支付挺火的,趁國慶宅在家,學習下Android 卡模擬(Host-based Card Emulation)。HCE的特點是模擬智能IC卡(ISO 7816-4),可用於金融和行業應用,相應地,CardReader例子中使用IsoDep。

智能IC卡本身是一個微型計算機,常見為Java Card平台,特別是多功能集於一身的卡(如聯名卡),Java Card比J2ME更加硬件受限。Java Card可以運行一到多個Java Applet,這些Applet也就是卡應用,例如一張能刷公交的銀行卡可能就包含了2個Applet。每個Applet都有一個AID,受理終端(刷卡設備)通過AID來找到對應的卡應用(受理終端向卡發送SELECT命令),受理終端找到對應的卡應用後就可以進行數據交互,交互的數據一般是密文,不聯機解密的話,用對稱算法,聯機解密的話,用非對稱和對稱算法都行。

HCE是軟件模擬的智能IC卡,所以也會有AID。本文CardEmulation只注冊一個AID。本文的代碼改自Android 4.4 Sample,沒改動CardEmulation,簡化CardReader並支持Android 2.3系統,適應低版本的NFC手機做虛擬卡的卡讀取了。使用努比亞Z7 MAX做CardEmulation,小米 2A作CardReader。本文的代碼可以到http://pan.baidu.com/s/1o6JnXr0下載。個人覺得閱讀CardReader核心代碼更能了解2者交互的過程:

[java] view plaincopyprint?

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};

public static String[][] TECHLISTS;

public static IntentFilter[] FILTERS;

static {

try {

//the tech lists used to perform matching for dispatching of the ACTION_TECH_DISCOVERED intent

TECHLISTS = new String[][] { { IsoDep.class.getName() },

{ NfcV.class.getName() }, { NfcF.class.getName() }, };

FILTERS = new IntentFilter[] { new IntentFilter(

NfcAdapter.ACTION_TECH_DISCOVERED, "*/*") };

} catch (Exception e) {

}

}

static public String tagDiscovered(Tag tag) {

Log.i(TAG, "New tag discovered");

// Android's Host-based Card Emulation (HCE) feature implements the

// ISO-DEP (ISO 14443-4)

// protocol.

//

// In order to communicate with a device using HCE, the discovered tag

// should be processed

// using the IsoDep class.

IsoDep isoDep = IsoDep.get(tag);

if (isoDep != 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.i(TAG, "Requesting remote AID: " + SAMPLE_LOYALTY_CARD_AID);

byte[] command = BuildSelectApdu(SAMPLE_LOYALTY_CARD_AID);

// Send command to remote device

Log.i(TAG, "Sending: " + ByteArrayToHexString(command));

byte[] result = isoDep.transceive(command);

// If AID is successfully selected, 0x9000 is returned as the

// status word (last 2

// bytes of the result) by convention. Everything before the

// status word is

// optional payload, which is used here to hold the 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 will immediately respond with its

// stored account number

String accountNumber = new String(payload, "UTF-8");

Log.i(TAG, "Received: " + accountNumber);

// Inform CardReaderFragment of received account number

return accountNumber;

}

} catch (IOException e) {

Log.e(TAG, "Error communicating with card: " + e.toString());

}

}

return null;

}

public static String load(Parcelable parcelable) {

// 從Parcelable篩選出各類NFC標准數據

final Tag tag = (Tag) parcelable;

return tagDiscovered(tag);

}

/**

* Build APDU for SELECT AID command. This command indicates which service a reader is

* 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);

}

copyright © 萬盛學電腦網 all rights reserved