如何显示nfc标签?

时间:2014-11-26 15:07:04

标签: java android nfc

当我阅读NFC标签Read an NFC tag时,只显示消息。我错过了什么?有没有办法检测NFC ID

我遵循了this教程。

package com.example.zmynfc;    
import java.util.Arrays;    
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.NfcF;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.widget.TextView;    
public class MainActivity extends Activity  {    
    private TextView        mTextView;
    private NfcAdapter      mNfcAdapter;
    private PendingIntent   mPendingIntent;
    private IntentFilter[]  mIntentFilters;
    private String[][]      mNFCTechLists;          
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);         
        mTextView   = (TextView)findViewById(R.id.textView1);
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);    

        if (mNfcAdapter != null) {
            mTextView.setText("Read an NFC tag");
        } else {
            mTextView.setText("This phone is not NFC enabled.");
        }
        // create an intent with tag data and deliver to this activity
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(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);
        try {
            ndefIntent.addDataType("*/*");
            mIntentFilters = new IntentFilter[] { ndefIntent };
        } catch (Exception e) {
            Log.e("TagDispatch", e.toString());
        }     
        mNFCTechLists = new String[][] { new String[] { NfcF.class.getName() } };   

    }
    @Override
    public void onNewIntent(Intent intent) {
        String action = intent.getAction();
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);     
        String s = action + "\n\n" + tag.toString();            
     // parse through all NDEF messages and their records and pick text type only
        Parcelable[] data = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (data != null) {
            try {
                for (int i = 0; i < data.length; i++) {
                    NdefRecord [] recs = ((NdefMessage)data[i]).getRecords();
                    for (int j = 0; j < recs.length; j++) {
                        if (recs[j].getTnf() == NdefRecord.TNF_WELL_KNOWN &&
                            Arrays.equals(recs[j].getType(), NdefRecord.RTD_TEXT)) {
                            byte[] payload = recs[j].getPayload();
                            String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
                            int langCodeLen = payload[0] & 0077;

                            s += ("\n\nNdefMessage[" + i + "], NdefRecord[" + j + "]:\n\"" +
                                 new String(payload, langCodeLen + 1, payload.length - langCodeLen - 1,
                                 textEncoding) + "\"");
                        }
                    }
                }
            } catch (Exception e) {
                Log.e("TagDispatch", e.toString());
            }
        }     
        mTextView.setText(s);   
    }
    @Override
    public void onResume() {
        super.onResume();     
        if (mNfcAdapter != null)
            mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mIntentFilters, mNFCTechLists);
    }     
    @Override
    public void onPause() {
        super.onPause();     
        if (mNfcAdapter != null)
            mNfcAdapter.disableForegroundDispatch(this);
    } 
}

1 个答案:

答案 0 :(得分:0)

在onCreate()方法中添加:

private NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
private PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

然后写这个拦截NFC id:

public void onNewIntent(Intent intent) { 

    super.onNewIntent(intent); 

    byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
    String rfid = byteToHex(id);

}
public String byteToHex(byte[] args) {
    String risultato = "";

    for (int i = 0; i < args.length; i++) {
        risultato += Integer.toString((args[i] & 0xff) + 0x100,16).substring(1);
    }
    return risultato;
}

public void enableForegroundMode() {
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter[] writeTagFilters = new IntentFilter[] {tagDetected};
    adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null);
}

public void disableForegroundMode() {
    adapter.disableForegroundDispatch(this);
}

public void onResume() {
    super .onResume();
    enableForegroundMode();
}

public void onPause() {
    super .onPause();
    disableForegroundMode();
}