簡體   English   中英

如何顯示nfc標簽?

[英]how to display nfc tag?

當我讀到NFC標簽Read an NFC tag只會顯示信息。 我錯過了什么? 有沒有辦法檢測NFC ID

我遵循了教程。

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);
    } 
}

將此添加到您的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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM