2014-03-27 58 views
1

你好我每一个工作的android应用程序,我需要扫描设备的nfc标签。我完全新来nfc和阅读了很多教程后,我发现一些方法来检查nfc是否启用或不在手机中,但我不知道如何阅读nfc标签。如何阅读nfc标签与Android应用程序

这里是我的oncreat

protected void onCreate(Bundle savedInstanceState) { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mainmenu); 
     mNfcAdapter = NfcAdapter.getDefaultAdapter(MainActivity.this); 
     pref = getApplicationContext().getSharedPreferences("Cordinate", 0); 
     editor = pref.edit(); 
     nfc1 = (ImageView) findViewById(R.id.checknfc1); 
     mDisplay = (TextView) findViewById(R.id.display); 
     username = (EditText) findViewById(R.id.username); 
     password = (EditText) findViewById(R.id.password1); 
     login = (TextView) findViewById(R.id.btnRegister); 
     context = getApplicationContext(); 

     TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
     imei = telephonyManager.getDeviceId(); 


     gcm = GoogleCloudMessaging.getInstance(this); 

     login.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       // Toast.makeText(MainActivity.this, "i am workin", 
       // Toast.LENGTH_LONG).show(); 

       GPSService gpstest = new GPSService(imei,MainActivity.this); 
       Thread data2 = new Thread(gpstest); 
       data2.start(); 
       gps = new GPSTracker(MainActivity.this); 
       if(gps.canGetLocation()){ 

        double latitude = gps.getLatitude(); 
        double longitude = gps.getLongitude(); 

        // \n is for new line 
        //Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
       } 
       else 
       { 

        Toast.makeText(getApplicationContext(), "GPS is not working", Toast.LENGTH_LONG).show(); 
       } 
       username1 = username.getText().toString(); 
       password2 = password.getText().toString(); 
       username.setText(""); 
       password.setText(""); 
       new RegisterBackground().execute(); 

      } 
     }); 


     nfc1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 


       if (mNfcAdapter != null) { 
        // "Read an NFC tag" 
        Toast.makeText(MainActivity.this, "Read an NFC tag", Toast.LENGTH_LONG).show(); 
        // create an intent with tag data and deliver to this activity 
        mPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, 
         new Intent(MainActivity.this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

        // set an intent filter for all MIME data 
        IntentFilter ndefIntent = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); 

        onNewIntent(getIntent()); 
        try { 
         ndefIntent.addDataType("*/*"); 
         mIntentFilters = new IntentFilter[] { ndefIntent }; 
        } catch (Exception e) { 
         Log.e("TagDispatch", e.toString()); 
        } 
       } else { 
        Toast.makeText(MainActivity.this, "not able to read NFC tag", Toast.LENGTH_LONG).show(); 
       } 

       //Toast.makeText(MainActivity.this, datanfc, Toast.LENGTH_LONG).show(); 


      } 
     }); 

    } 

我onNewIntent

public void onNewIntent(Intent intent) { 
    String action = intent.getAction(); 
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 

    String nfctag =tag.toString(); 

    Toast.makeText(MainActivity.this, nfctag, Toast.LENGTH_LONG).show(); 

} 

回答

0

请参阅本文档的更多信息http://developer.android.com/guide/topics/connectivity/nfc/nfc.html

基本上,你首先应该标记是什么类型的,你想读ACTION_TECH_DISCOVERED

然后更新mani因此巨星,

<activity> 
... 
<intent-filter> 
    <action android:name="android.nfc.action.TECH_DISCOVERED"/> 
</intent-filter> 

<meta-data android:name="android.nfc.action.TECH_DISCOVERED" 
    android:resource="@xml/nfc_tech_filter" /> 
... 
</activity> 

然后从扫描标签

public void onResume() { 
    super.onResume(); 
    ... 
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) { 
     Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 
     if (rawMsgs != null) { 
      msgs = new NdefMessage[rawMsgs.length]; 
      for (int i = 0; i < rawMsgs.length; i++) { 
       msgs[i] = (NdefMessage) rawMsgs[i]; 
      } 
     } 
    } 
    //process the msgs array 
} 
+0

是什么意图的方法的作用得到了数据... – Simmant

+0

它是用来打开你的应用程序时,您扫描NFC标签 – PsyGik

+0

你的回答帮助我兄弟终于我得到解决方案 – Simmant