如何知道标签是否存在?

时间:2017-07-31 11:58:07

标签: android nfc mifare foreground dispatch

早上好,我想使用我在页面上找到的写法:https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html来写一个超轻的mifare。我还没有成功。

 public void writeTag(Tag tag, String tagText) {
   MifareUltralight ultralight = MifareUltralight.get(tag);
   try {
      ultralight.connect();
      ultralight.writePage(4, "abcd".getBytes(Charset.forName("US-ASCII")));
      ultralight.writePage(5, "efgh".getBytes(Charset.forName("US-ASCII")));
      ultralight.writePage(6, "ijkl".getBytes(Charset.forName("US-ASCII")));
      ultralight.writePage(7, "mnop".getBytes(Charset.forName("US-ASCII")));
   } catch (IOException e) {
      Log.e(TAG, "IOException while closing MifareUltralight...", e);
   } finally {
       try {
          ultralight.close();
       } catch (IOException e) {
          Log.e(TAG, "IOException while closing MifareUltralight...", e);
       }
   }
}

如何从主打电话给它?

我认为有必要插入一个按钮来检查标签是否存在。

我在我的活动中添加了“前台调度系统”(不是主要活动),但是如果标签存在与否,我仍然不明白如何显示消息,在使用读写方法之前要检查什么?

package com.example.administrator.myapplication3;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareUltralight;
import android.nfc.tech.Ndef;
import android.nfc.tech.NfcA;
import android.nfc.tech.NfcB;
import android.nfc.tech.NfcF;
import android.nfc.tech.NfcV;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;
import java.nio.charset.Charset;

public class DisplayMessageActivity extends AppCompatActivity {

    private NfcAdapter mAdapter;
    private PendingIntent pendingIntent;
    private IntentFilter[] mFilters;
    private String[][] mTechLists;

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);

        // Get the Intent that started this activity and extract the string
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Capture the layout's TextView and set the string as its text
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(message);

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

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

        // Setup an intent filter for all MIME based dispatches
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            ndef.addDataType("*/*");
        } catch (IntentFilter.MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
        IntentFilter td = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        mFilters = new IntentFilter[] {
                ndef, td
        };

        // Setup a tech list for all NfcF tags
        mTechLists = new String[][] { new String[] { MifareUltralight.class.getName(), Ndef.class.getName(), NfcA.class.getName()}};


        Button b = (Button) findViewById(R.id.button2);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TextView tv = (TextView) findViewById(R.id.textView);

               Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
                tv.setText("Click!");

                //byte[] x=tag.getId();
                writeTag(tag,"x");
                Log.e(TAG,"cc");



            }
        });

    }

    @Override
    public void onResume()
    {
        super.onResume();

        mAdapter.enableForegroundDispatch(this, pendingIntent, mFilters, mTechLists);
    }

    @Override
    public void onPause()
    {
        super.onPause();
        mAdapter.disableForegroundDispatch(this);
    }

    @Override
    public void onNewIntent(Intent intent){

        // fetch the tag from the intent
        Tag t = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

       // String tlist = getTechList(t);
       // android.util.Log.v("NFC", "Discovered tag ["+tlist+"]["+t+"] with intent: " + intent);
       // android.util.Log.v("NFC", "{"+t+"}");
    }





    public void writeTag(Tag tag, String tagText) {
        MifareUltralight ultralight = MifareUltralight.get(tag);
        try {
            ultralight.connect();
            ultralight.writePage(4, "abcd".getBytes(Charset.forName("US-ASCII")));
            ultralight.writePage(5, "efgh".getBytes(Charset.forName("US-ASCII")));
            ultralight.writePage(6, "ijkl".getBytes(Charset.forName("US-ASCII")));
            ultralight.writePage(7, "mnop".getBytes(Charset.forName("US-ASCII")));
        } catch (IOException e) {
            Log.e(TAG, "IOException while closing MifareUltralight...", e);
        } finally {
            try {
                ultralight.close();
            } catch (IOException e) {
                Log.e(TAG, "IOException while closing MifareUltralight...", e);
            }
        }
    }

    public String readTag(Tag tag) {
        MifareUltralight mifare = MifareUltralight.get(tag);
        try {
            mifare.connect();
            byte[] payload = mifare.readPages(4);
            return new String(payload, Charset.forName("US-ASCII"));
        } catch (IOException e) {
            Log.e(TAG, "IOException while writing MifareUltralight message...", e);
        } finally {
            if (mifare != null) {
                try {
                    mifare.close();
                }
                catch (IOException e) {
                    Log.e(TAG, "Error closing tag...", e);
                }
            }
        }
        return null;
    }
}

谢谢!

2 个答案:

答案 0 :(得分:5)

一旦扫描了新的NFC标签,就会调用onNewIntent(Intent intent)。例如,如果你想写入每个扫描的标签,你的onNewIntent()方法将如下所示:

//This method is called whenever a new tag is scanned while the activity is running
//Whatever you want to do with scanned tags, do it in here
@Override
public void onNewIntent(Intent intent){
    // fetch the tag from the intent
    Tag tag = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    //Write a String onto the tag
    writeTag(tag, "Put any string here");
}

你的writeTag方法当然还没有使用它给出的String,但你可能知道。

如果您希望保持最新的布尔值,您可以检查可用的标签,请执行以下操作:

 @Override
 public void onNewIntent(Intent intent){

     (new Thread(new Runnable() {
         @Override
         public void run() {
             // fetch the tag from the intent
             Tag tag = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
             MifareUltralight myTag = MifareUltralight.get(tag);
             /* Alternative to MifareUltralight:
             NfcA myTag = NfcA.get(tag);
             */
             myTag.connect();
             while(true){
                 //someBoolean is just a Boolean that you can use in other threads to ask if tag is still there
                 someBoolean = true;
                 //Keep reading the tag until it ends in an exception
                 //when the exception occurs, you know the tag is gone
                 try{
                     myTag.transceive(new byte[] {
                          (byte) 0xXX, // READ command for your tag here
                          (byte) 0xXX, // Address of serial or UID field of your tag here
                     });
                  }catch(IOException e){
                      myTag.disconnect();
                      break;
                  }
              }
              //When loop breaks, the tag is gone, so we  set the variable back to false
              someBoolean = false;
          }
      })).start();
 }

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.NfcA</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>
</resources>

这是我的xml文件的样子。 MifareUltralight和NfcA几乎是可以互换的。只需使用NfcA就可以了。