乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Android NFC 開發(fā)實例

       Colin收藏 2012-05-24

      Android NFC 開發(fā)實例

      分類: Android應用開發(fā)系列教程Android應用開發(fā)技巧580人閱讀評論(0)收藏舉報

      作者:裘德超


      使用硬件:Google Nexus S,北京大學學生卡。(ps:筆者本想使用公交一卡通進行測試,發(fā)現(xiàn)手機不能正確識別)

      手機操作系統(tǒng):Android ICS 4.04。

      開發(fā)時,筆者從Google Play Store上下載了NFC TagInfo軟件進行對比學習。所以我們可以使用任意一張能被TagInfo軟件正確識別的卡做測試。

      在Android NFC 應用中,Android手機通常是作為通信中的發(fā)起者,也就是作為各種NFC卡的讀寫器。Android對NFC的支持主要在 android.nfc 和android.nfc.tech 兩個包中。

      android.nfc 包中主要類如下:

      NfcManager 可以用來管理Android設備中指出的所有NFCAdapter,但由于大部分Android設備只支持一個NFC Adapter,所以一般直接調(diào)用getDefaultAapater來獲取手機中的Adapter。

      NfcAdapter 相當于一個NFC適配器,類似于電腦裝了網(wǎng)絡適配器才能上網(wǎng),手機裝了NfcAdapter才能發(fā)起NFC通信。

       NDEF: NFC Data Exchange Format,即NFC數(shù)據(jù)交換格式。

      NdefMessage 和NdefRecord NDEF 為NFC forum 定義的數(shù)據(jù)格式。

      Tag 代表一個被動式Tag對象,可以代表一個標簽,卡片等。當Android設備檢測到一個Tag時,會創(chuàng)建一個Tag對象,將其放在Intent對象,然后發(fā)送到相應的Activity。

      android.nfc.tech 中則定義了可以對Tag進行的讀寫操作的類,這些類按照其使用的技術(shù)類型可以分成不同的類如:NfcA, NfcB, NfcF,以及MifareClassic 等。其中MifareClassic比較常見。

      在本次實例中,筆者使用北京大學學生卡進行數(shù)據(jù)讀取測試,學生卡的TAG類型為MifareClassic。


      AndroidManifest.xml:


      1. <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?>  
      2. <manifest xmlns:android="http://schemas./apk/res/android"  
      3.     package="org.reno"  
      4.     android:versionCode="1"  
      5.     android:versionName="1.0" >  
      6.     <uses-permission android:name="android.permission.NFC" />  
      7.     <uses-sdk android:minSdkVersion="14" />  
      8.     <uses-feature android:name="android.hardware.nfc" android:required="true" />  
      9.     <application  
      10.         android:icon="@drawable/ic_launcher"  
      11.         android:label="@string/app_name" >  
      12.         <activity  
      13.             android:name="org.reno.Beam"  
      14.             android:label="@string/app_name"  
      15.             android:launchMode="singleTop" >  
      16.             <intent-filter>  
      17.                 <action android:name="android.intent.action.MAIN" />  
      18.   
      19.                 <category android:name="android.intent.category.LAUNCHER" />  
      20.             </intent-filter>  
      21.             <intent-filter>  
      22.                 <action android:name="android.nfc.action.TECH_DISCOVERED" />  
      23.             </intent-filter>  
      24.             <meta-data  
      25.                 android:name="android.nfc.action.TECH_DISCOVERED"  
      26.                 android:resource="@xml/nfc_tech_filter" />  
      27.         </activity>  
      28.     </application>  
      29. </manifest>  
      30. </span>  


      res/xml/nfc_tech_filter.xml:


      <resourcesxmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

          <tech-list>

             <tech>android.nfc.tech.MifareClassic</tech>

          </tech-list>

      </resources>


      <uses-permission android:name="android.permission.NFC"/>

      <uses-feature android:name="android.hardware.nfc"android:required="true"/>


      表示會使用到硬件的NFC功能。并且當用戶在Google Play Store中搜索時,只有帶有NFC功能的手機才能夠搜索到本應用。

       

      當手機開啟了NFC,并且檢測到一個TAG后,TAG分發(fā)系統(tǒng)會自動創(chuàng)建一個封裝了NFC TAG信息的intent。如果多于一個應用程序能夠處理這個intent的話,那么手機就會彈出一個框,讓用戶選擇處理該TAG的Activity。TAG分發(fā)系統(tǒng)定義了3中intent。按優(yōu)先級從高到低排列為:

      NDEF_DISCOVERED, TECH_DISCOVERED, TAG_DISCOVERED

      當Android設備檢測到有NFC Tag靠近時,會根據(jù)Action申明的順序給對應的Activity 發(fā)送含NFC消息的 Intent。

      此處我們使用的intent-filter的Action類型為TECH_DISCOVERED從而可以處理所有類型為ACTION_TECH_DISCOVERED并且使用的技術(shù)為nfc_tech_filter.xml文件中定義的類型的TAG。

       

      詳情可查看http://developer./guide/topics/nfc/nfc.html說明。下圖為當手機檢測到一個TAG時,啟用Activity的匹配過程。




      res/layout/main.xml


      1. <?xml version="1.0" encoding="utf-8"?>  
      2. <LinearLayout xmlns:android="http://schemas./apk/res/android"  
      3.     android:layout_width="fill_parent"  
      4.     android:layout_height="fill_parent"  
      5.     android:orientation="vertical" >  
      6.   
      7.     <ScrollView  
      8.         android:id="@+id/scrollView"  
      9.         android:layout_width="fill_parent"  
      10.         android:layout_height="fill_parent"  
      11.         android:background="@android:drawable/edit_text" >  
      12.   
      13.         <TextView  
      14.             android:id="@+id/promt"  
      15.             android:layout_width="fill_parent"  
      16.             android:layout_height="wrap_content"  
      17.             android:scrollbars="vertical"  
      18.             android:singleLine="false"  
      19.             android:text="@string/info" />  
      20.     </ScrollView>  
      21.   
      22. </LinearLayout>  

      定義了Activity的布局:只有一個帶有滾動條的TextView用于顯示從TAG中讀取的信息。

      res/values/strings.xml


      1. <?xml version="1.0" encoding="utf-8"?>  
      2. <resources>  
      3.     <string name="app_name">NFC測試</string>  
      4.     <string name="info">掃描中。。。</string>  
      5. </resources>  

      src/org/reno/Beam.java

      1. package org.reno;  
      2.   
      3. import android.app.Activity;  
      4. import android.content.Intent;  
      5. import android.nfc.NfcAdapter;  
      6. import android.nfc.Tag;  
      7. import android.nfc.tech.MifareClassic;  
      8. import android.os.Bundle;  
      9. import android.widget.TextView;  
      10.   
      11. public class Beam extends Activity {  
      12.     NfcAdapter nfcAdapter;  
      13.     TextView promt;  
      14.     @Override  
      15.     public void onCreate(Bundle savedInstanceState) {  
      16.         super.onCreate(savedInstanceState);  
      17.         setContentView(R.layout.main);  
      18.         promt = (TextView) findViewById(R.id.promt);  
      19.         // 獲取默認的NFC控制器  
      20.         nfcAdapter = NfcAdapter.getDefaultAdapter(this);  
      21.         if (nfcAdapter == null) {  
      22.             promt.setText("設備不支持NFC!");  
      23.             finish();  
      24.             return;  
      25.         }  
      26.         if (!nfcAdapter.isEnabled()) {  
      27.             promt.setText("請在系統(tǒng)設置中先啟用NFC功能!");  
      28.             finish();  
      29.             return;  
      30.         }  
      31.     }  
      32.   
      33.     @Override  
      34.     protected void onResume() {  
      35.         super.onResume();  
      36.         //得到是否檢測到ACTION_TECH_DISCOVERED觸發(fā)  
      37.         if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) {  
      38.             //處理該intent  
      39.             processIntent(getIntent());  
      40.         }  
      41.     }  
      42.     //字符序列轉(zhuǎn)換為16進制字符串  
      43.     private String bytesToHexString(byte[] src) {  
      44.         StringBuilder stringBuilder = new StringBuilder("0x");  
      45.         if (src == null || src.length <= 0) {  
      46.             return null;  
      47.         }  
      48.         char[] buffer = new char[2];  
      49.         for (int i = 0; i < src.length; i++) {  
      50.             buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F16);  
      51.             buffer[1] = Character.forDigit(src[i] & 0x0F16);  
      52.             System.out.println(buffer);  
      53.             stringBuilder.append(buffer);  
      54.         }  
      55.         return stringBuilder.toString();  
      56.     }  
      57.   
      58.     /** 
      59.      * Parses the NDEF Message from the intent and prints to the TextView 
      60.      */  
      61.     private void processIntent(Intent intent) {  
      62.         //取出封裝在intent中的TAG  
      63.         Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  
      64.         for (String tech : tagFromIntent.getTechList()) {  
      65.             System.out.println(tech);  
      66.         }  
      67.         boolean auth = false;  
      68.         //讀取TAG  
      69.         MifareClassic mfc = MifareClassic.get(tagFromIntent);  
      70.         try {  
      71.             String metaInfo = "";  
      72.             //Enable I/O operations to the tag from this TagTechnology object.  
      73.             mfc.connect();  
      74.             int type = mfc.getType();//獲取TAG的類型  
      75.             int sectorCount = mfc.getSectorCount();//獲取TAG中包含的扇區(qū)數(shù)  
      76.             String typeS = "";  
      77.             switch (type) {  
      78.             case MifareClassic.TYPE_CLASSIC:  
      79.                 typeS = "TYPE_CLASSIC";  
      80.                 break;  
      81.             case MifareClassic.TYPE_PLUS:  
      82.                 typeS = "TYPE_PLUS";  
      83.                 break;  
      84.             case MifareClassic.TYPE_PRO:  
      85.                 typeS = "TYPE_PRO";  
      86.                 break;  
      87.             case MifareClassic.TYPE_UNKNOWN:  
      88.                 typeS = "TYPE_UNKNOWN";  
      89.                 break;  
      90.             }  
      91.             metaInfo += "卡片類型:" + typeS + "\n共" + sectorCount + "個扇區(qū)\n共"  
      92.                     + mfc.getBlockCount() + "個塊\n存儲空間: " + mfc.getSize() + "B\n";  
      93.             for (int j = 0; j < sectorCount; j++) {  
      94.                 //Authenticate a sector with key A.  
      95.                 auth = mfc.authenticateSectorWithKeyA(j,  
      96.                         MifareClassic.KEY_DEFAULT);  
      97.                 int bCount;  
      98.                 int bIndex;  
      99.                 if (auth) {  
      100.                     metaInfo += "Sector " + j + ":驗證成功\n";  
      101.                     // 讀取扇區(qū)中的塊  
      102.                     bCount = mfc.getBlockCountInSector(j);  
      103.                     bIndex = mfc.sectorToBlock(j);  
      104.                     for (int i = 0; i < bCount; i++) {  
      105.                         byte[] data = mfc.readBlock(bIndex);  
      106.                         metaInfo += "Block " + bIndex + " : "  
      107.                                 + bytesToHexString(data) + "\n";  
      108.                         bIndex++;  
      109.                     }  
      110.                 } else {  
      111.                     metaInfo += "Sector " + j + ":驗證失敗\n";  
      112.                 }  
      113.             }  
      114.             promt.setText(metaInfo);  
      115.         } catch (Exception e) {  
      116.             e.printStackTrace();  
      117.         }  
      118.     }  
      119. }  


      關(guān)于MifareClassic卡的背景介紹:數(shù)據(jù)分為16個區(qū)(Sector) ,每個區(qū)有4個塊(Block) ,每個塊可以存放16字節(jié)的數(shù)據(jù)。

      每個區(qū)最后一個塊稱為Trailer ,主要用來存放讀寫該區(qū)Block數(shù)據(jù)的Key ,可以有A,B兩個Key,每個Key 長度為6個字節(jié),缺省的Key值一般為全FF或是0. 由 MifareClassic.KEY_DEFAULT 定義。

      因此讀寫Mifare Tag 首先需要有正確的Key值(起到保護的作用),如果鑒權(quán)成功

      然后才可以讀寫該區(qū)數(shù)據(jù)。

      執(zhí)行效果:








      參考聯(lián)接:

      http://developer./guide/topics/nfc/nfc.html

      http://developer./reference/android/nfc/tech/MifareClassic.html

      http://www./wordpress/?p=2822


        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多