Android之NFC近距離無線通訊技術

首先要弄清楚NFC是什麼? 

 

NFC是Near Field Communication縮寫,即近距離無線通訊技術。由飛利浦公司和索尼公司共同開發的NFC是一種非接觸式識別和互聯技術,可以在移動設備、消費類電子產品、PC 和智能控件工具間進行近距離無線通信。NFC 提供了一種簡單、觸控式的解決方案,可以讓消費者簡單直觀地交換信息、訪問內容與服務。 

NFC 將非接觸讀卡器、非接觸卡和點對點(Peer-to-Peer)功能整合進一塊單芯片,爲消費者的生活方式開創了不計其數的全新機遇。這是一個開放接口平臺,可以對無線網絡進行快速、主動設置,也是虛擬連接器,服務於現有蜂窩狀網絡、藍牙和無線 802.11 設備。 
  NFC可兼容索尼公司的FeliCaTM卡以及已廣泛建立的非接觸式智能卡架構,該架構基於ISO 14443 A,使用飛利浦的MIFARE?技術。 
  爲了推動 NFC 的發展和普及,飛利浦、索尼和諾基亞創建了一個非贏利性的行業協會——NFC 論壇,促進 NFC 技術的實施和標準化,確保設備和服務之間協同合作。目前,NFC 論壇在全球擁有 70 多個成員,包括:萬事達卡國際組織、松下電子工業有限公司、微軟公司、摩托羅拉公司、NEC 公司、瑞薩科技公司、三星公司、德州儀器製造公司和 Visa 國際組織。 
  編者注:NFC全球最早的商用發佈: * 德國,美因茨交通公司(RMV) 2006年4月19日,飛利浦、諾基亞、Vodafone公司及德國法蘭克福美因茨地區的公交網絡運營商美因茨交通公司(Rhein-Main Verkehrsverbund)宣佈,在成功地進行爲期10個月的現場試驗後,近距離無線通信(NFC)技術即將投入商用。目前,Nokia 3220手機已集成了NFC技術,可以用作電子車票,還可在當地零售店和旅遊景點作爲折扣忠誠卡使用。哈瑙市的大約95.000位居民現在只需輕鬆地刷一下兼容手機,就能享受NFC式公交移動售票帶來的便利。 

  與RFID一樣,NFC信息也是通過頻譜中無線頻率部分的電磁感應耦合方式傳遞,但兩者之間還是存在很大的區別。首先,NFC是一種提供輕鬆、安全、迅速的通信的無線連接技術,其傳輸範圍比RFID小,RFID的傳輸範圍可以達到幾米、甚至幾十米,但由於NFC採取了獨特的信號衰減技術,相對於 RFID來說NFC具有距離近、帶寬高、能耗低等特點。 其次,NFC與現有非接觸智能卡技術兼容,目前已經成爲得到越來越多主要廠商支持的正式標準。再次,NFC還是一種近距離連接協議,提供各種設備間輕鬆、安全、迅速而自動的通信。與無線世界中的其他連接方式相比,NFC是一種近距離的私密通信方式。最後,RFID更多的被應用在生產、物流、跟蹤、資產管理上,而NFC則在門禁、公交、手機支付等領域內發揮着巨大的作用。 
  同時,NFC還優於紅外和藍牙傳輸方式。作爲一種面向消費者的交易機制,NFC比紅外更快、更可靠而且簡單得多,不用向紅外那樣必須嚴格的對齊才能傳輸數據。與藍牙相比,NFC面向近距離交易,適用於交換財務信息或敏感的個人信息等重要數據;藍牙能夠彌補NFC通信距離不足的缺點,適用於較長距離數據通信。因此,NFC和藍牙互爲補充,共同存在。事實上,快捷輕型的NFC協議可以用於引導兩臺設備之間的藍牙配對過程,促進了藍牙的使用。 
  NFC手機內置NFC芯片,組成RFID模塊的一部分,可以當作RFID無源標籤使用———用來支付費用;也可以當作RFID讀寫器——— 用作數據交換與採集。NFC技術支持多種應用,包括移動支付與交易、對等式通信及移動中信息訪問等。通過NFC手機,人們可以在任何地點、任何時間,通過任何設備,與他們希望得到的娛樂服務與交易聯繫在一起,從而完成付款,獲取海報信息等。NFC設備可以用作非接觸式智能卡、智能卡的讀寫器終端以及設備對設備的數據傳輸鏈路,其應用主要可分爲以下四個基本類型:用於付款和購票、用於電子票證、用於智能媒體以及用於交換、傳輸數據。 

我們來看一下NFC在Android代碼中的位置: 
1.frameworks/base/core/java/android/nfc 
Java代碼  收藏代碼
  1. ErrorCodes.java  
  2.   
  3. This class defines all the error codes that can be returned by the service and producing an exception on the application level. These are needed since binders does not support exceptions.  
  4. 弄了個iserror方法 定義了一堆小於0的常量!等於0就success  
  5.     public static boolean isError(int code) {  
  6.         if (code < 0) {  
  7.             return true;  
  8.         } else {  
  9.             return false;  
  10.         }  
  11.     }  
  12.   
  13. TagLostException.java  
  14. FormatException.java  
  15.   
  16. 自定義的一個類型轉化異常!  
  17. public FormatException(String message) {  
  18.         super(message);  
  19.     }  
  20. 自己拋出異常  
  21.   
  22. LlcpPacket.java  
  23.   
  24. Represents a LLCP packet received in a LLCP Connectionless communication  
  25. LLCP –其在定義一個通訊協定,以管理架構在ISO 18092/NFC IP-1的NFC設備間的邏輯連結,當然這個通訊協定主要是為了Peer-to-Peer模式下,設備間通訊時使用。  
  26. private final int mRemoteSap;private final byte[] mDataBuffer;  
  27.   
  28. NdefMessage.java  
  29. Represents an NDEF (NFC Data Exchange Format) data message that contains one or more   
  30. 數據的數據交換格式  
  31. public NdefMessage(byte[] data) throws FormatException {  
  32.         mRecords = null;  // stop compiler complaints about final field  
  33.         if (parseNdefMessage(data) == -1) {  
  34.             throw new FormatException("Error while parsing NDEF message");  
  35.         }  
  36.     }  
  37. private native int parseNdefMessage(byte[] data);  
  38.   
  39. NdefRecord.java  
  40. 邏輯記錄數據交換的格式。  
  41.   
  42.     private final byte mFlags;  
  43.   
  44.     private static final byte FLAG_MB = (byte0x80;  
  45.     private static final byte FLAG_ME = (byte0x40;  
  46.     private static final byte FLAG_CF = (byte0x20;  
  47.     private static final byte FLAG_SR = (byte0x10;  
  48.     private static final byte FLAG_IL = (byte0x08;  
  49.   
  50.     private final short mTnf; 表示格式如何解讀型態欄位  
  51.     private final byte[] mType;描述了記錄格式  
  52.     private final byte[] mId;一個唯一的標識符備案  
  53.     private final byte[] mPayload;實際數據的有效載荷  
  54.   
  55.     private native int parseNdefRecord(byte[] data);  
  56.     private native byte[] generate(short flags, short tnf, byte[] type, byte[] id, byte[] data);  
  57.   
  58. NfcAdapter.java  
  59.   
  60. Represents the device's local NFC adapter.  
  61.   
  62. NfcManager.java  
  63.   
  64. High level manager used to obtain an instance of an  
  65. 其實只有一個getDefaultAdapter()說明NfcAdapter其實正扮演着manager的角色  
  66.   
  67. NfcSecureElement.java  
  68.   
  69. This class provides the primary API for managing all aspects Secure Element.Get an instance of this class by calling Context.getSystemService(Context.NFC_SERVICE).  
  70. 這個類提供了主要的API的管理所有安全方面的元素  
  71.   
  72. Tag.java  
  73. Represents a (generic) discovered tag.  
  74.     /*package*/ final byte[] mId;The tag identifier  
  75.     /*package*/ final int[] mTechList;  
  76.     /*package*/ final String[] mTechStringList;  
  77.     /*package*/ final Bundle[] mTechExtras;  
  78.     /*package*/ final int mServiceHandle;  // for use by NFC service, 0 indicates a mock  
  79.     /*package*/ final INfcTag mTagService;  
  80.     /*package*/ int mConnectedTechnology;  
  81.   
  82. public Tag(byte[] id, int[] techList, Bundle[] techListExtras, int serviceHandle,  
  83.             INfcTag tagService) {  
  84.         if (techList == null) {  
  85.             throw new IllegalArgumentException("rawTargets cannot be null");  
  86.         }  
  87.         mId = id;  
  88.         mTechList = Arrays.copyOf(techList, techList.length);  
  89.         mTechStringList = generateTechStringList(techList);  
  90.         // Ensure mTechExtras is as long as mTechList  
  91.         mTechExtras = Arrays.copyOf(techListExtras, techList.length);  
  92.         mServiceHandle = serviceHandle;  
  93.         mTagService = tagService;  
  94.   
  95.         mConnectedTechnology = -1;  
  96.     }  
  97.   
  98. TechListParcel.java  
  99. private String[][] mTechLists;  
  100.     public TechListParcel(String[]... strings) {  
  101.         mTechLists = strings;  
  102.     }  
  103.   
  104.   
  105. TransceiveResult.java  
  106.   
  107. Class used to pipe transceive result from the NFC service.  
  108.     private final boolean mTagLost;  
  109.     private final boolean mSuccess;  
  110.     private final byte[] mResponseData;  
  111.   
  112. ILlcpConnectionlessSocket.aidl  
  113. ILlcpServiceSocket.aidl  
  114. ILlcpSocket.aidl  
  115. INfcAdapter.aidl  
  116. INfcSecureElement.aidl  
  117. INfcTag.aidl  
  118. IP2pInitiator.aidl  
  119. IP2pTarget.aidl  
  120. LlcpPacket.aidl  
  121. NdefMessage.aidl  
  122. NdefRecord.aidl  
  123. Tag.aidl  
  124. TechListParcel.aidl  
  125. TransceiveResult.aidl  


2.frameworks/base/core/java/android/nfc/tech 
Java代碼  收藏代碼
  1. NFC Protocol Bindings –在Peer-to-Peer模式下,NFC Forum不自己另定高層的資料傳輸協定,而是直接使用其他既有的資料傳輸協定,如IP、OBEX等,來實現該模式下設備間資料的傳輸。   
  2.   
  3. BasicTagTechnology.java  
  4. A base class for tag technologies that are built on top of transceive().  
  5. 這個應該是最頂端的transceive()方法!IsoDep.java NfcA.javaNfcB.javaNfcF.javaNfcV.java全部調用此方法實現!  
  6.     /*package*/ final Tag mTag;  
  7.     /*package*/ boolean mIsConnected;  
  8.     /*package*/ int mSelectedTechnology;  
  9.   
  10. IsoDep.java  
  11.   
  12.  A low-level connection to a {@link Tag} using the ISO-DEP technology, also known as ISO1443-4.  
  13.   
  14. MifareClassic.java  
  15.   
  16. 即Philips的Mifare,現有悠遊卡即屬此類  
  17.   
  18. MifareUltralight.java  
  19.   
  20. Ndef.java  
  21.   
  22. NDEF – NFC Data Exchange Format (NDEF) 主要在定義一個簡潔與共同的資料格式,此資料格式將可作為NFC設備在讀取標籤(Tag)資料時的一個依據。此規格於2006年便完成制定並已公告。   
  23. 目前整個NFC相關技術規格標準的制定,主要是由NFC Forum負責  
  24. 下圖為NFC Forum所展示的NFC技術規格架構  
  25. [img]http://dl.iteye.com/upload/attachment/471075/a760e498-60e7-398c-8f23-e7bae2f0f83a.jpg[/img]  
  26.   
  27. A high-level connection to a {@link Tag} using one of the NFC type 123, or 4 technologies to interact with NDEF data. MiFare Classic cards that present NDEF data may also be used via this class. To determine the exact technology being used call  
  28.   
  29. NdefFormatable.java  
  30.   
  31. An interface to a {@link Tag} allowing to format the tag as NDEF.  
  32.   
  33. NfcA.java  
  34.   
  35. A low-level connection to a {@link Tag} using the NFC-A technology, also known as ISO1443-3A.  
  36.   
  37. NfcB.java  
  38.   
  39. A low-level connection to a {@link Tag} using the NFC-B technology, also known as ISO1443-3B.  
  40.   
  41. NfcF.java  
  42.   
  43. A low-level connection to a {@link Tag} using the NFC-F technology, also known as JIS6319-4.  
  44.   
  45. NfcV.java  
  46.   
  47. A low-level connection to a {@link Tag} using NFC vicinity technology, also known as ISO15693.  
  48.   
  49. TagTechnology.java  
  50. 最上層的接口這裏定義集中協議類型的標識 繼承closeable便於克隆  
  51. 擁有方法getTag connect reconnect close isConnected  
  52.    


這裏涉及到幾個協議一起來看一下 
 

 

 

 

 

Java代碼  收藏代碼
  1. 3.frameworks/base/core/java/com/android/internal/nfc  
  2.   
  3. LlcpConnectionlessSocket.java  
  4.   
  5.   
  6. LlcpException.java  
  7.   
  8. LlcpServiceSocket.java  
  9.   
  10. LlcpSocket.java  
  11.   
  12. NfcException.java  
  13.   
  14. P2pDevice.java  
  15. P2pDevice is the abstract base class for all supported P2P targets the  
  16. NfcManager can handle.  
  17.   
  18. P2pInitiator.java  
  19.   
  20. P2pInitiator represents the initiator in an NFC-IP1 peer-to-peer  
  21. communication.  
  22.   
  23. P2pTarget.java  
  24. P2pTarget represents the target in an NFC-IP1 peer-to-peer communication.  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章