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

前端之家收集整理的这篇文章主要介绍了android – 如何知道标签是否存在?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
早上好,我想使用我在页面上找到的写法: https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html,写一个超轻的mifare.我还没有成功.
  1. public void writeTag(Tag tag,String tagText) {
  2. MifareUltralight ultralight = MifareUltralight.get(tag);
  3. try {
  4. ultralight.connect();
  5. ultralight.writePage(4,"abcd".getBytes(Charset.forName("US-ASCII")));
  6. ultralight.writePage(5,"efgh".getBytes(Charset.forName("US-ASCII")));
  7. ultralight.writePage(6,"ijkl".getBytes(Charset.forName("US-ASCII")));
  8. ultralight.writePage(7,"mnop".getBytes(Charset.forName("US-ASCII")));
  9. } catch (IOException e) {
  10. Log.e(TAG,"IOException while closing MifareUltralight...",e);
  11. } finally {
  12. try {
  13. ultralight.close();
  14. } catch (IOException e) {
  15. Log.e(TAG,e);
  16. }
  17. }
  18. }

我如何从主打电话给它?

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

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

  1. package com.example.administrator.myapplication3;
  2.  
  3. import android.app.PendingIntent;
  4. import android.content.Intent;
  5. import android.content.IntentFilter;
  6. import android.nfc.NfcAdapter;
  7. import android.nfc.Tag;
  8. import android.nfc.tech.MifareUltralight;
  9. import android.nfc.tech.Ndef;
  10. import android.nfc.tech.NfcA;
  11. import android.nfc.tech.NfcB;
  12. import android.nfc.tech.NfcF;
  13. import android.nfc.tech.NfcV;
  14. import android.support.v7.app.AppCompatActivity;
  15. import android.os.Bundle;
  16. import android.util.Log;
  17. import android.view.View;
  18. import android.widget.Button;
  19. import android.widget.TextView;
  20.  
  21. import java.io.IOException;
  22. import java.nio.charset.Charset;
  23.  
  24. public class DisplayMessageActivity extends AppCompatActivity {
  25.  
  26. private NfcAdapter mAdapter;
  27. private PendingIntent pendingIntent;
  28. private IntentFilter[] mFilters;
  29. private String[][] mTechLists;
  30.  
  31. private static final String TAG = MainActivity.class.getSimpleName();
  32.  
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.activity_display_message);
  37.  
  38. // Get the Intent that started this activity and extract the string
  39. Intent intent = getIntent();
  40. String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
  41.  
  42. // Capture the layout's TextView and set the string as its text
  43. TextView textView = (TextView) findViewById(R.id.textView);
  44. textView.setText(message);
  45.  
  46. pendingIntent = PendingIntent.getActivity( this,new Intent(this,getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
  47.  
  48. mAdapter = NfcAdapter.getDefaultAdapter(this);
  49. pendingIntent = PendingIntent.getActivity(
  50. this,0);
  51.  
  52. // Setup an intent filter for all MIME based dispatches
  53. IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
  54. try {
  55. ndef.addDataType("*/*");
  56. } catch (IntentFilter.MalformedMimeTypeException e) {
  57. throw new RuntimeException("fail",e);
  58. }
  59. IntentFilter td = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
  60. mFilters = new IntentFilter[] {
  61. ndef,td
  62. };
  63.  
  64. // Setup a tech list for all NfcF tags
  65. mTechLists = new String[][] { new String[] { MifareUltralight.class.getName(),Ndef.class.getName(),NfcA.class.getName()}};
  66.  
  67.  
  68. Button b = (Button) findViewById(R.id.button2);
  69. b.setOnClickListener(new View.OnClickListener() {
  70. @Override
  71. public void onClick(View view) {
  72. TextView tv = (TextView) findViewById(R.id.textView);
  73.  
  74. Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
  75. tv.setText("Click!");
  76.  
  77. //byte[] x=tag.getId();
  78. writeTag(tag,"x");
  79. Log.e(TAG,"cc");
  80.  
  81.  
  82.  
  83. }
  84. });
  85.  
  86. }
  87.  
  88. @Override
  89. public void onResume()
  90. {
  91. super.onResume();
  92.  
  93. mAdapter.enableForegroundDispatch(this,pendingIntent,mFilters,mTechLists);
  94. }
  95.  
  96. @Override
  97. public void onPause()
  98. {
  99. super.onPause();
  100. mAdapter.disableForegroundDispatch(this);
  101. }
  102.  
  103. @Override
  104. public void onNewIntent(Intent intent){
  105.  
  106. // fetch the tag from the intent
  107. Tag t = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  108.  
  109. // String tlist = getTechList(t);
  110. // android.util.Log.v("NFC","Discovered tag ["+tlist+"]["+t+"] with intent: " + intent);
  111. // android.util.Log.v("NFC","{"+t+"}");
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118. public void writeTag(Tag tag,String tagText) {
  119. MifareUltralight ultralight = MifareUltralight.get(tag);
  120. try {
  121. ultralight.connect();
  122. ultralight.writePage(4,"abcd".getBytes(Charset.forName("US-ASCII")));
  123. ultralight.writePage(5,"efgh".getBytes(Charset.forName("US-ASCII")));
  124. ultralight.writePage(6,"ijkl".getBytes(Charset.forName("US-ASCII")));
  125. ultralight.writePage(7,"mnop".getBytes(Charset.forName("US-ASCII")));
  126. } catch (IOException e) {
  127. Log.e(TAG,e);
  128. } finally {
  129. try {
  130. ultralight.close();
  131. } catch (IOException e) {
  132. Log.e(TAG,e);
  133. }
  134. }
  135. }
  136.  
  137. public String readTag(Tag tag) {
  138. MifareUltralight mifare = MifareUltralight.get(tag);
  139. try {
  140. mifare.connect();
  141. byte[] payload = mifare.readPages(4);
  142. return new String(payload,Charset.forName("US-ASCII"));
  143. } catch (IOException e) {
  144. Log.e(TAG,"IOException while writing MifareUltralight message...",e);
  145. } finally {
  146. if (mifare != null) {
  147. try {
  148. mifare.close();
  149. }
  150. catch (IOException e) {
  151. Log.e(TAG,"Error closing tag...",e);
  152. }
  153. }
  154. }
  155. return null;
  156. }
  157. }

谢谢!

解决方法

扫描新的NFC标签后立即调用onNewIntent(Intent intent).例如,如果您想要写入每个扫描的标记,则onNewIntent()方法将如下所示:
  1. //This method is called whenever a new tag is scanned while the activity is running
  2. //Whatever you want to do with scanned tags,do it in here
  3. @Override
  4. public void onNewIntent(Intent intent){
  5. // fetch the tag from the intent
  6. Tag tag = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  7.  
  8. //Write a String onto the tag
  9. writeTag(tag,"Put any string here");
  10. }

你的writeTag方法当然还没有使用它给出的字符串,但你可能知道.

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

  1. @Override
  2. public void onNewIntent(Intent intent){
  3.  
  4. (new Thread(new Runnable() {
  5. @Override
  6. public void run() {
  7. // fetch the tag from the intent
  8. Tag tag = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  9. MifareUltralight myTag = MifareUltralight.get(tag);
  10. /* Alternative to MifareUltralight:
  11. NfcA myTag = NfcA.get(tag);
  12. */
  13. myTag.connect();
  14. while(true){
  15. //someBoolean is just a Boolean that you can use in other threads to ask if tag is still there
  16. someBoolean = true;
  17. //Keep reading the tag until it ends in an exception
  18. //when the exception occurs,you know the tag is gone
  19. try{
  20. myTag.transceive(new byte[] {
  21. (byte) 0xXX,// READ command for your tag here
  22. (byte) 0xXX,// Address of serial or UID field of your tag here
  23. });
  24. }catch(IOException e){
  25. myTag.disconnect();
  26. break;
  27. }
  28. }
  29. //When loop breaks,the tag is gone,so we set the variable back to false
  30. someBoolean = false;
  31. }
  32. })).start();
  33. }

猜你在找的Android相关文章