android – 如何知道标签是否存在?

前端之家收集整理的这篇文章主要介绍了android – 如何知道标签是否存在?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
早上好,我想使用我在页面上找到的写法: 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,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,new Intent(this,getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);

        mAdapter = NfcAdapter.getDefaultAdapter(this);
        pendingIntent = PendingIntent.getActivity(
                this,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,e);
        } finally {
            try {
                ultralight.close();
            } catch (IOException e) {
                Log.e(TAG,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;
    }
}

谢谢!

解决方法

扫描新的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方法当然还没有使用它给出的字符串,但你可能知道.

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

@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();
 }
原文链接:https://www.f2er.com/android/315186.html

猜你在找的Android相关文章