简体   繁体   English

如何显示nfc标签?

[英]how to display nfc tag?

When I read NFC tag Read an NFC tag message only will displayed. 当我读到NFC标签Read an NFC tag只会显示信息。 What did I miss? 我错过了什么? Is there a way to detect NFC ID ? 有没有办法检测NFC ID

I followed this tutorial. 我遵循了教程。

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

Add this in your onCreate() method: 将此添加到您的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);

Then write this for intercept NFC id: 然后将其编写为拦截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