2011-10-11 17 views
1

我有代碼來初始化我的NFC適配器,但我不知道如何在onCreate期間初始化變量。無論NFC標籤是否接近,該應用都可以初始化,即。如果有人只是打開應用程序。所以,當我回到這一行NfcV nfcMessage = NfcV.get(new TagGet().getTag());它崩潰,因爲它是空的,如果你只是自己加載應用程序,那裏沒有標籤。我如何在這裏檢查!= null我不確定要檢查哪個部分或如何構造此代碼。Android NFC,在onCreate做空檢查?

結果是我想讀標籤存在的標籤IFF的內容。否則,只需加載佈局並等待NfcV標籤進行掃描。

// Setup an intent filter for all MIME based dispatches 
    IntentFilter nfcv = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); 
    try { 
     nfcv.addDataType("*/*"); 
    } catch (MalformedMimeTypeException e) { 
     throw new RuntimeException("fail", e); 
    } 
    mFilters = new IntentFilter[] { 
      nfcv, 
    }; 

    // Setup a tech list for all NfcF tags 
    mTechLists = new String[][] { new String[] { NfcV.class.getName() } }; 

    //mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists); 

    NfcV nfcMessage = NfcV.get(new TagGet().getTag()); 

    byte[] data = new byte[2048]; //tag length can't be any larger 
    String value = ""; 
    try { 
     data = nfcMessage.transceive(new byte[2048]); 
     value = new String(data); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
+0

你做的方法 「公共無效onNewIntent(意向意圖)」 在周圍的類?這樣當你的應用程序運行並且在NewIntent運行時發現標籤時。然後你可以讓你的標籤知道標籤在那裏。然後,您需要啓動另一個線程來處理您的應用和標籤之間的通信。你雖然朝着正確的方向前進! –

回答

2

聽到是我製作第一個Nfc應用程序的基本方法的代碼大綱。我試圖替換值(我用nfcA而不是nfcV),以便更好地閱讀它。這是一個非常基本的結構,你需要用你自己的代碼填寫部分,但它應該給你一個關於NFC應用程序的結構如何工作的好主意。

public class Android_nfc_ibox extends Activity implements Runnable { 




NfcAdapter mNfcAdapter; 
private String[][] mTechLists; 
PendingIntent pendingIntent; 
Tag tag; 
NfcA mTag; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Initialize the NFC adapter 
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
    if (mNfcAdapter != null) { 
     dialog_text.append("Tap an NFC tag for access\n\r"); 
    } else { 
     dialog_text.append("This phone is not NFC enabled\n\r"); 
    } 

    // Create the PendingIntent object which will contain the details of the tag that has been scanned 
    pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

    // Setup a tech list for all desired tag types 
    mTechLists = new String[][] { new String[] { NfcA.class.getName() } }; 

} 

    /** Re-enable the tag dispatch if the app is in the foreground */ 
    @Override 
    public void onResume() { 
     super.onResume(); 
     if (mNfcAdapter != null) mNfcAdapter.enableForegroundDispatch(this, pendingIntent, null, mTechLists); 
    } 

    /** Disable the tag dispatch when the app is no longer in the foreground */ 
    @Override 
    public void onPause() { 
     super.onPause(); 
     if (mNfcAdapter != null) mNfcAdapter.disableForegroundDispatch(this); 
    } 

    /** A tag has been discovered */ 
    @Override 
    public void onNewIntent(Intent intent){ 

     // get the tag object for the discovered tag 
     Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 

     // try and get the MifareUltralight instance for this tag 
     mTag = NfcV.get(tag); 

     // if null then this wasn't a NfcV tag so wait for next time 
     if(mTag == null){ 
      dialog_text.append("Not a Nfc V tag\n\r"); 
     } 

      // Start the tag communications thread 
      Thread myThread = new Thread(this); 
      myThread.start(); 

     } 
    } 

    // (we could create other threads for other types of tags) 
    public void run(){ 
     // try to connect to the Nfc V tag 
     try{ 

      mTag.connect(); 
     }catch(IOException e){ 
      //handle the error here 
     } 

     //this will send raw data 
     //send the values you want in the byte[] 
     //just add the raw hex values with commas 
     //pageBuffer is an array that will hold the response 
     try{ 
      pageBuffer = mTag.transceive(new byte[] {0x11, 0x24, 0x11}); 
     }catch(IOException e){ 
      //handle error here 
     } 
    } 

}

+0

謝謝本,這非常有幫助! 'mTag.transceive(new byte [] {0x11,0x24,0x11});'這不適用於我發送到我的NfcV標籤讀取(我猜它是錯誤的十六進制)爲什麼你使用這些十六進制值和你是怎麼決定使用這些的,我怎麼才能找到適合我的NfcV設備的「讀取」命令? – CQM

+0

對不起,它主要是隨機的廢話,我只是想顯示十六進制命令將去的地方。我相信你想要的命令是0x20,然後是塊號,所以(0x20,0x06)讀取塊6.但是,從短的谷歌搜索它可能是(0x00,0x20,0x06) –

+0

寫將是0×21。每個塊看起來有4個字節,編寫一個塊的代碼看起來是(0x40,0x21,0x06,0x01,0x02,0x03,0x04)第一個字節是標誌,第二個是命令(讀或寫)第三個是塊編號,接下來的四個是您正在編寫的數據。 –