Android NFC APDU

来源:互联网 发布:手写笔绘画软件 编辑:程序博客网 时间:2024/06/06 23:16

本文来自http://blog.csdn.net/hellogv/

最近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?在CODE上查看代码片派生到我的代码片
  1. public final class CardReader {  
  2.     private static final String TAG = "LoyaltyCardReader";  
  3.     // AID for our loyalty card service.  
  4.     private static final String SAMPLE_LOYALTY_CARD_AID = "F222222222";  
  5.     // ISO-DEP command HEADER for selecting an AID.  
  6.     // Format: [Class | Instruction | Parameter 1 | Parameter 2]  
  7.     private static final String SELECT_APDU_HEADER = "00A40400";  
  8.     // "OK" status word sent in response to SELECT AID command (0x9000)  
  9.     private static final byte[] SELECT_OK_SW = {(byte0x90, (byte0x00};  
  10.   
  11.     public static String[][] TECHLISTS;  
  12.     public static IntentFilter[] FILTERS;  
  13.   
  14.     static {  
  15.         try {  
  16.             //the tech lists used to perform matching for dispatching of the ACTION_TECH_DISCOVERED intent  
  17.             TECHLISTS = new String[][] { { IsoDep.class.getName() },  
  18.                     { NfcV.class.getName() }, { NfcF.class.getName() }, };  
  19.   
  20.             FILTERS = new IntentFilter[] { new IntentFilter(  
  21.                     NfcAdapter.ACTION_TECH_DISCOVERED, "*/*") };  
  22.         } catch (Exception e) {  
  23.         }  
  24.     }  
  25.       
  26.     static public String tagDiscovered(Tag tag) {  
  27.         Log.i(TAG, "New tag discovered");  
  28.         // Android's Host-based Card Emulation (HCE) feature implements the  
  29.         // ISO-DEP (ISO 14443-4)  
  30.         // protocol.  
  31.         //  
  32.         // In order to communicate with a device using HCE, the discovered tag  
  33.         // should be processed  
  34.         // using the IsoDep class.  
  35.         IsoDep isoDep = IsoDep.get(tag);  
  36.         if (isoDep != null) {  
  37.             try {  
  38.                 // Connect to the remote NFC device  
  39.                 isoDep.connect();  
  40.                 // Build SELECT AID command for our loyalty card service.  
  41.                 // This command tells the remote device which service we wish to  
  42.                 // communicate with.  
  43.                 Log.i(TAG, "Requesting remote AID: " + SAMPLE_LOYALTY_CARD_AID);  
  44.                 byte[] command = BuildSelectApdu(SAMPLE_LOYALTY_CARD_AID);  
  45.                 // Send command to remote device  
  46.                 Log.i(TAG, "Sending: " + ByteArrayToHexString(command));  
  47.                 byte[] result = isoDep.transceive(command);  
  48.                 // If AID is successfully selected, 0x9000 is returned as the  
  49.                 // status word (last 2  
  50.                 // bytes of the result) by convention. Everything before the  
  51.                 // status word is  
  52.                 // optional payload, which is used here to hold the account  
  53.                 // number.  
  54.                 int resultLength = result.length;  
  55.                 byte[] statusWord = { result[resultLength - 2],  
  56.                         result[resultLength - 1] };  
  57.                 byte[] payload = Arrays.copyOf(result, resultLength - 2);  
  58.                 if (Arrays.equals(SELECT_OK_SW, statusWord)) {  
  59.                     // The remote NFC device will immediately respond with its  
  60.                     // stored account number  
  61.                     String accountNumber = new String(payload, "UTF-8");  
  62.                     Log.i(TAG, "Received: " + accountNumber);  
  63.                     // Inform CardReaderFragment of received account number  
  64.                     return accountNumber;  
  65.                 }  
  66.             } catch (IOException e) {  
  67.                 Log.e(TAG, "Error communicating with card: " + e.toString());  
  68.             }  
  69.         }  
  70.         return null;  
  71.     }  
  72.   
  73.     public static String load(Parcelable parcelable) {  
  74.         // 从Parcelable筛选出各类NFC标准数据  
  75.         final Tag tag = (Tag) parcelable;  
  76.         return tagDiscovered(tag);  
  77.     }  
  78.   
  79.   
  80.     /**  
  81.      * Build APDU for SELECT AID command. This command indicates which service a reader is  
  82.      * interested in communicating with. See ISO 7816-4.  
  83.      *  
  84.      * @param aid Application ID (AID) to select  
  85.      * @return APDU for SELECT AID command  
  86.      */  
  87.     public static byte[] BuildSelectApdu(String aid) {  
  88.         // Format: [CLASS | INSTRUCTION | PARAMETER 1 | PARAMETER 2 | LENGTH | DATA]  
  89.         return HexStringToByteArray(SELECT_APDU_HEADER + String.format("%02X", aid.length() / 2) + aid);  
  90.     }  
  91.   
  92.     /** 
  93.      * Utility class to convert a byte array to a hexadecimal string. 
  94.      * 
  95.      * @param bytes Bytes to convert 
  96.      * @return String, containing hexadecimal representation. 
  97.      */  
  98.     public static String ByteArrayToHexString(byte[] bytes) {  
  99.         final char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};  
  100.         char[] hexChars = new char[bytes.length * 2];  
  101.         int v;  
  102.         for ( int j = 0; j < bytes.length; j++ ) {  
  103.             v = bytes[j] & 0xFF;  
  104.             hexChars[j * 2] = hexArray[v >>> 4];  
  105.             hexChars[j * 2 + 1] = hexArray[v & 0x0F];  
  106.         }  
  107.         return new String(hexChars);  
  108.     }  
  109.   
  110.     /** 
  111.      * Utility class to convert a hexadecimal string to a byte string. 
  112.      * 
  113.      * <p>Behavior with input strings containing non-hexadecimal characters is undefined. 
  114.      * 
  115.      * @param s String containing hexadecimal characters to convert 
  116.      * @return Byte array generated from input 
  117.      */  
  118.     public static byte[] HexStringToByteArray(String s) {  
  119.         int len = s.length();  
  120.         byte[] data = new byte[len / 2];  
  121.         for (int i = 0; i < len; i += 2) {  
  122.             data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)  
  123.                     + Character.digit(s.charAt(i+1), 16));  
  124.         }  
  125.         return data;  
  126.     }  


单地说:

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,而不是卡与读卡器的关系。

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public final class NFCard extends Activity {  
  2.     private NfcAdapter nfcAdapter;  
  3.     private PendingIntent pendingIntent;  
  4.     private EditText board;  
  5.     private int sysVersion;  
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.cardreader);  
  10.         board = (EditText) this.findViewById(R.id.editText1);  
  11.   
  12.         nfcAdapter = NfcAdapter.getDefaultAdapter(this);  
  13.         pendingIntent = PendingIntent.getActivity(this0new Intent(this,  
  14.                 getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);  
  15.   
  16.         //区分系统版本  
  17.         sysVersion = Integer.parseInt(VERSION.SDK);  
  18.         if(sysVersion<19)  
  19.             onNewIntent(getIntent());  
  20.     }  
  21.   
  22.   
  23.     @Override  
  24.     protected void onPause() {  
  25.         super.onPause();  
  26.   
  27.         if (nfcAdapter != null){  
  28.             nfcAdapter.disableForegroundDispatch(this);  
  29.             disableReaderMode();  
  30.         }  
  31.     }  
  32.   
  33.     @Override  
  34.     protected void onResume() {  
  35.         super.onResume();  
  36.   
  37.         if (nfcAdapter != null){  
  38.             nfcAdapter.enableForegroundDispatch(this, pendingIntent,  
  39.                     CardReader.FILTERS, CardReader.TECHLISTS);  
  40.             enableReaderMode();  
  41.         }  
  42.         Log.e("NFC----", IsoDep.class.getName());  
  43.         refreshStatus();  
  44.     }  
  45.   
  46.     @Override  
  47.     protected void onNewIntent(Intent intent) {  
  48.         super.onNewIntent(intent);  
  49.   
  50.         final Parcelable p = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  
  51.         Log.d("NFCTAG", intent.getAction());  
  52.         board.setText((p != null) ? CardReader.load(p) : null);  
  53.     }  
  54.   
  55.   
  56.     @TargetApi(19)   
  57.     private void enableReaderMode() {  
  58.         if(sysVersion<19)  
  59.             return;  
  60.           
  61.         int READER_FLAGS = NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK;  
  62.         if (nfcAdapter != null) {  
  63.             nfcAdapter.enableReaderMode(thisnew MyReaderCallback(), READER_FLAGS, null);  
  64.         }  
  65.     }  
  66.   
  67.   
  68.     @TargetApi(19)   
  69.     private void disableReaderMode() {  
  70.         if(sysVersion<19)  
  71.             return;  
  72.           
  73.         if (nfcAdapter != null) {  
  74.             nfcAdapter.disableReaderMode(this);  
  75.         }  
  76.     }  
  77.   
  78.       
  79.     @TargetApi(19)   
  80.     public class MyReaderCallback implements NfcAdapter.ReaderCallback {  
  81.   
  82.         @Override  
  83.         public void onTagDiscovered(final Tag arg0) {  
  84.             NFCard.this.runOnUiThread(new Runnable() {  
  85.                 @Override  
  86.                 public void run() {  
  87.                       
  88.                     board.setText(CardReader.tagDiscovered(arg0));  
  89.                 }  
  90.             });  
  91.         }  
  92.     }  
  93.   
  94.       
  95.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  96.         refreshStatus();  
  97.     }  
  98.   
  99.     private void refreshStatus() {  
  100.   
  101.         final String tip;  
  102.         if (nfcAdapter == null)  
  103.             tip = this.getResources().getString(R.string.tip_nfc_notfound);  
  104.         else if (nfcAdapter.isEnabled())  
  105.             tip = this.getResources().getString(R.string.tip_nfc_enabled);  
  106.         else  
  107.             tip = this.getResources().getString(R.string.tip_nfc_disabled);  
  108.   
  109.         final StringBuilder s = new StringBuilder(  
  110.                 this.getResources().getString(R.string.app_name));  
  111.   
  112.         s.append("  --  ").append(tip);  
  113.         setTitle(s);  
  114.     }  
0 0
原创粉丝点击