NFC交互实现--hce读卡器

匆匆过客 提交于 2019-12-04 13:36:46

目前NFC应用的大的框架上的理解:

使用API LEVEL 19及以上,支持的API有三个:

android.nfc,android.nfc.cardemulator,android.nfc.tech

NFC在手机上的应用大体分为两类:读卡器和卡

android.nfc.cardemulator接口是为NFC作为卡应用提供的接口,在较低版本的API上是没有的android.nfc.techandroid.nfc接口是为NFC作为读卡器应用提供的接口

 

 

NFC数据过滤

 

NFC有三种过滤器分别是ACTION_NDEF_DISCOVERED,ACTION_TECH_DISCOVERED,ACTION_TAG_DISCOVERED。

 

1. ACTION_NDEF_DISCOVERED

 

当扫描到的tag中包含有NDEF载荷且为已知类型,该intent将用来启动Activity。该intent的优先级最高,tag分发系统总是先于其他intent用该intent来启动Activity。

 

2. ACTION_TECH_DISCOVERED

 

如果manifest中没有注册处理ACTION_NDEF_DISCOVERED类型的intent,该intent将被用以启动Activity。如果tag中没有包含可以映射到MIME或者URI类型的数据,或者虽然没有包含NDEF数据,但是已知的tag技术,则该intent也会被直接启用。

 

3. ACTION_TAG_DISCOVERED

 

如果以上两个intent都没人疼,那么该intent就会启动。

 

过滤器的作用是过滤掉杂质,剩下的就是我们需要的了。这三种过滤方式可同时配置,可以比方成从上到下三层,只要是符合某一层过滤器要求的,过滤完就停止往下一层。

 

本案例使用ACTION_TECH_DISCOVERED

读卡器实现

代码实现:

AndroidManifest.xml 中 配置activity,因为作为读卡器实现的话,需要监听intent

 

<activity android:name=".MainActivity"          android:label="@string/app_name"          android:launchMode="singleTop">    <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />    </intent-filter>    <!-- NFC-related intent filter. Allows application to handle messages from any         NFC-A devices discovered. Other Android devices are required to support NFC-A.         See: res/xml/nfc_tech_filter.xml -->    <intent-filter>        <action android:name="android.nfc.action.TECH_DISCOVERED" />    </intent-filter>    <meta-data        android:name="android.nfc.action.TECH_DISCOVERED"        android:resource="@xml/nfc_tech_filter" /></activity>       

res/xml/nfc_tech_filter.xml 中配置tech-list

 

<?xml version="1.0" encoding="utf-8"?><!-- This file is used as part of the filter for incoming NFC TECH_DISCOVERED intents. --><resources xmlns:android="http://schemas.android.com/apk/res/android">    <!-- Android's host card emulation feature only supports the IsoDep protocol. -->    <tech-list>        <tech>android.nfc.tech.IsoDep</tech>    </tech-list></resources>

 

配置文件完成后编写activity的处理方法:主要是enable disable NFC的ReaderMode

 

public static int READER_FLAGS =
NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK;
public LoyaltyCardReader mLoyaltyCardReader;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLoyaltyCardReader = new LoyaltyCardReader(this);

 

// Disable Android Beam and register our card reader callback
enableReaderMode();
}

 

@Override
public void onPause() {
super.onPause();
disableReaderMode();
}

 

@Override
public void onResume() {
super.onResume();
enableReaderMode();
}

 

private void enableReaderMode() {
Log.i(TAG, "Enabling reader mode");
NfcAdapter nfc = NfcAdapter.getDefaultAdapter(activity);
if (nfc != null) {
nfc.enableReaderMode(activity, mLoyaltyCardReader, READER_FLAGS, null);
}
}

 

private void disableReaderMode() {
Log.i(TAG, "Disabling reader mode");
NfcAdapter nfc = NfcAdapter.getDefaultAdapter(activity);
if (nfc != null) {
nfc.disableReaderMode(activity);
}
}

 

当TECH_DICOVERED触发时,LoyaltyCardReader的onTagDiscovered,通过tag获取到对应的IsoDep

IsoDep isoDep = IsoDep.get(tag);

后续就可以通过IsoDep跟卡端建立通讯连接,连接建立的最重要信息时aid

LoyaltyCardReader实现代码:
package com.example.android.cardreader;import android.nfc.NfcAdapter;import android.nfc.Tag;import android.nfc.tech.IsoDep;import com.example.android.common.logger.Log;import java.io.IOException;import java.lang.ref.WeakReference;import java.util.Arrays;/** * Callback class, invoked when an NFC card is scanned while the device is running in reader mode. * <p> * Reader mode can be invoked by calling NfcAdapter */public class LoyaltyCardReader implements NfcAdapter.ReaderCallback {    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";    // Format: [Class | Instruction | Parameter 1 | Parameter 2]    private static final String GET_DATA_APDU_HEADER = "00CA0000";    // "OK" status word sent in response to SELECT AID command (0x9000)    private static final byte[] SELECT_OK_SW = {(byte) 0x90, (byte) 0x00};    private static final String WRITE_DATA_APDU_HEADER = "00DA0000";    private static final String READ_DATA_APDU_HEADER = "00EA0000";    private static final byte[] WRITE_DATA_APDU = BuildWriteDataApdu();    private static final byte[] READ_DATA_APDU = BuildReadDataApdu();    // Weak reference to prevent retain loop. mAccountCallback is responsible for exiting    // foreground mode before it becomes invalid (e.g. during onPause() or onStop()).    private WeakReference<AccountCallback> mAccountCallback;    public interface AccountCallback {        public void onAccountReceived(String account);    }    public LoyaltyCardReader(AccountCallback accountCallback) {        mAccountCallback = new WeakReference<AccountCallback>(accountCallback);    }    /**     * Callback when a new tag is discovered by the system.     *     * <p>Communication with the card should take place here.     *     * @param tag Discovered tag     */    @Override    public void onTagDiscovered(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();                Log.i(TAG, "Timeout = " + isoDep.getTimeout());                isoDep.setTimeout(3600);                Log.i(TAG, "Timeout = " + isoDep.getTimeout());                Log.i(TAG, "MaxTransceiveLength = " + isoDep.getMaxTransceiveLength());                // 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[] selCommand = BuildSelectApdu(SAMPLE_LOYALTY_CARD_AID);                // Send command to remote device                Log.i(TAG, "Sending: " + ByteArrayToHexString(selCommand));                byte[] result = isoDep.transceive(selCommand);                // 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)) {                    String accountNumber = new String(payload, "UTF-8");                    Log.i(TAG, "Received: " + accountNumber);                    //todo test sample                    setAPDUMsg(isoDep,"test");                    getAPDUMsg(isoDep);                }            } catch (IOException e) {                Log.e(TAG, "Error communicating with card: " + e.toString());            }        }    }    /**     * 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);    }    /**     * Build APDU for GET_DATA command. See ISO 7816-4.     *     * @return APDU for SELECT AID command     */    public static byte[] BuildGetDataApdu() {        // Format: [CLASS | INSTRUCTION | PARAMETER 1 | PARAMETER 2 | LENGTH | DATA]        return HexStringToByteArray(GET_DATA_APDU_HEADER + "0FFF");    }    /**     * 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), 16) << 4)                    + Character.digit(s.charAt(i + 1), 16));        }        return data;    }    public static byte[] BuildWriteDataApdu() {        // Format: [CLASS | INSTRUCTION | PARAMETER 1 | PARAMETER 2 | LENGTH | DATA]        return HexStringToByteArray(WRITE_DATA_APDU_HEADER + "0FFF");    }    public static byte[] BuildReadDataApdu() {        // Format: [CLASS | INSTRUCTION | PARAMETER 1 | PARAMETER 2 | LENGTH | DATA]        return HexStringToByteArray(READ_DATA_APDU_HEADER + "0FFF");    }    public static byte[] ConcatArrays(byte[] first, byte[]... rest) {        int totalLength = first.length;        for (byte[] array : rest) {            totalLength += array.length;        }        byte[] result = Arrays.copyOf(first, totalLength);        int offset = first.length;        for (byte[] array : rest) {            System.arraycopy(array, 0, result, offset, array.length);            offset += array.length;        }        return result;    }    private String getAPDUMsg(IsoDep isoDep) {        if (isoDep == null) {            return "";        }        String msg = null;        try {            Log.i(TAG, "Sending: " + ByteArrayToHexString(READ_DATA_APDU));            byte[] result = isoDep.transceive(READ_DATA_APDU);            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)) {                msg = new String(payload, "UTF-8");                Log.i(TAG, "Received msg: " + msg);            }        } catch (Exception e) {            Log.w(TAG, "getAPDUMsg Exception:" + e);        }        return msg;    }    private void setAPDUMsg(IsoDep isoDep, String msg) {        if (isoDep == null) {            return;        }        try {            Log.i(TAG, "write: " + WRITE_DATA_APDU_HEADER);            byte[] selCommand = ConcatArrays(WRITE_DATA_APDU, msg.getBytes());            Log.i(TAG, "Sending: " + ByteArrayToHexString(selCommand));            byte[] result = isoDep.transceive(selCommand);            int resultLength = result.length;            byte[] statusWord = {result[resultLength - 2], result[resultLength - 1]};            byte[] payload1 = 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(payload1, "UTF-8");                Log.i(TAG, "Received payload: " + accountNumber);            }        } catch (Exception e) {            Log.w(TAG, "setAPDUMsg Exception:" + e);        }        return;    }}

 

简单地说:

1、CardReader使用SAMPLE_LOYALTY_CARD_AID + SELECT_APDU_HEADER 生成 SELECT APDU;

2、CardReader发送SELECT APDU到CardEmulation后,CardEmulation返回SELECT_OK_SW + accountNumber。

其中SAMPLE_LOYALTY_CARD_AID ,SELECT_APDU_HEADER ,SELECT_OK_SW 都必须是CardReader与CardEmulation双方定义好的。

另外,CardReader在4.4和4.4以下系统的用法也略有不同,4.4以下系统用onNewIntent(),4.4系统就要通过enableReaderMode()方法,如果4.4系统用onNewIntent()的话,会导致一直P2P,而不是卡与读卡器的关系。

 

demo:git@github.com:zhan3080/NfcHCE-Reader-Emulation.git

 

 

 

参考链接:

https://blog.csdn.net/demon_w/article/details/50174737

https://blog.csdn.net/okletsgo007/article/details/46727831

apdu指令 

https://blog.csdn.net/wwt18811707971/article/details/85785354

https://blog.csdn.net/qq_36135335/article/details/82463179

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!