2013-02-16 80 views
0

我有这个源代码,它应该读取一个NDEF nfc标签。除了扫描标签时,它不会执行任何操作。有任何想法吗 ?Android简易NFC阅​​读器应用程序源代码错误。它不会读取任何nfc标签?

该代码编译并没有错误。

它看起来不像函数 保护无效onNewIntent(意图意图)运行。

package com.tapwise.nfcreadtag; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.PendingIntent; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.nfc.NdefMessage; 
import android.nfc.NfcAdapter; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.os.PatternMatcher; 
import android.provider.Settings; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Toast; 

    public class MainActivity extends Activity { 
    private static final String TAG = "NFCReadTag"; 
    private NfcAdapter mNfcAdapter; 
    private IntentFilter[] mNdefExchangeFilters; 
    private PendingIntent mNfcPendingIntent; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 

    mNfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, 
       getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP 
       | Intent.FLAG_ACTIVITY_CLEAR_TOP), 0); 


    IntentFilter smartwhere = new IntentFilter (NfcAdapter.ACTION_NDEF_DISCOVERED); 
     smartwhere.addDataScheme("http"); 
     smartwhere.addDataAuthority("www.smartwhere.com", null); 
     smartwhere.addDataPath(".*", PatternMatcher.PATTERN_SIMPLE_GLOB); 

     mNdefExchangeFilters = new IntentFilter[] { smartwhere }; 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     if(mNfcAdapter != null) { 
      mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, 
       mNdefExchangeFilters, null); 
      if (!mNfcAdapter.isEnabled()){ 

       LayoutInflater inflater = getLayoutInflater(); 
       View dialoglayout = inflater.inflate(R.layout.nfc_settings_layout,(ViewGroup) findViewById(R.id.nfc_settings_layout)); 
       new AlertDialog.Builder(this).setView(dialoglayout) 
         .setPositiveButton("Update Settings", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface arg0, int arg1) { 
           Intent setnfc = new Intent(Settings.ACTION_WIRELESS_SETTINGS); 
           startActivity(setnfc); 
          } 
         }) 
         .setOnCancelListener(new DialogInterface.OnCancelListener() { 

          public void onCancel(DialogInterface dialog) { 
           finish(); // exit application if user cancels 
          }       
         }).create().show(); 

      } 
     } else { 
      Toast.makeText(getApplicationContext(), "Sorry, No NFC Adapter found.", Toast.LENGTH_SHORT).show(); 
     } 


    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     if(mNfcAdapter != null) mNfcAdapter.disableForegroundDispatch(this); 
    } 

    @Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent);  
     if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) { 
      NdefMessage[] messages = null; 
      Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 
      if (rawMsgs != null) { 
       messages = new NdefMessage[rawMsgs.length]; 
       for (int i = 0; i < rawMsgs.length; i++) { 
        messages[i] = (NdefMessage) rawMsgs[i]; 
       } 
      } 
      if(messages[0] != null) { 
       String result=""; 
       byte[] payload = messages[0].getRecords()[0].getPayload(); 
       // this assumes that we get back am SOH followed by host/code 
       for (int b = 1; b<payload.length; b++) { // skip SOH 
        result += (char) payload[b]; 
       } 
       Toast.makeText(getApplicationContext(), "Tag Contains " + result, Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 
} 
+0

代码如何使用更通用的过滤器(smartwhere)? – ThomasRS 2013-02-17 10:50:36

+0

您的标签是否包含http://www.smartwhere.com/*格式的网址? – 2013-10-14 13:54:19

+0

当您尝试扫描时,您的应用程序是否打开?如果是,请删除3条线,在'IntentFilter smartwhere'上设置属性。如果它没有打开,那么这将不起作用,你必须在应用程序的清单文件中提供你正在做的事情。 – Daneo 2014-04-25 08:43:18

回答

-1

如果您使用的是Nexus 4,则该设备无法读取MIFARE经典标签。

+0

这不提供问题的答案。要批评或要求作者澄清,在他们的帖子下留下评论 - 你总是可以评论你自己的帖子,一旦你有足够的[声誉](http://stackoverflow.com/help/whats-reputation),你会能够[评论任何帖子](http://stackoverflow.com/help/privileges/comment)。 – Micha 2013-10-14 07:50:50