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

android – 如何知道标签是否存在?,第1张

概述早上好,我想使用我在页面上找到的写法: https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html,写一个超轻的mifare.我还没有成功. public void writeTag(Tag tag, String tagText) { MifareUltralight ultralight = 早上好,我想使用我在页面上找到的写法: 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@OverrIDepublic 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(); }
总结

以上是内存溢出为你收集整理的android – 如何知道标签是否存在?全部内容,希望文章能够帮你解决android – 如何知道标签是否存在?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1135718.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存