NFC写Uri进Tag,与识别Tag



   

自己写段程序写Uri进Tag,很简单:

  1. import java.io.IOException;  
  2. import java.nio.charset.Charset;  
  3.   
  4. import android.content.Context;  
  5. import android.nfc.FormatException;  
  6. import android.nfc.NdefMessage;  
  7. import android.nfc.NdefRecord;  
  8. import android.nfc.Tag;  
  9. import android.nfc.TagLostException;  
  10. import android.nfc.tech.Ndef;  
  11. import android.nfc.tech.NdefFormatable;  
  12. import android.widget.Toast;  
  13.   
  14. public class NFC {  
  15.       
  16.     public static boolean writeTag(Context context, Tag tag) {      
  17.           
  18.         NdefRecord relayRecord = new NdefRecord(  
  19.                 NdefRecord.TNF_ABSOLUTE_URI ,  
  20.                 "zoeice://com.zoeice.example/applicationUp".getBytes(Charset.forName("US-ASCII")),  
  21.                 new byte[0], new byte[0]);  
  22.        
  23.         // Complete NDEF message with both records  
  24.         NdefMessage message = new NdefMessage(new NdefRecord[] {relayRecord});  
  25.        
  26.         try {  
  27.             // If the tag is already formatted, just write the message to it  
  28.             Ndef ndef = Ndef.get(tag);  
  29.             if(ndef != null) {  
  30.                 ndef.connect();  
  31.        
  32.                 // Make sure the tag is writable  
  33.                 if(!ndef.isWritable()) {  
  34.                      Toast.makeText(context, "nfcReadOnlyErrorTitle", Toast.LENGTH_SHORT ).show();  
  35.                     return false;  
  36.                 }  
  37.        
  38.                 // Check if there's enough space on the tag for the message  
  39.                 int size = message.toByteArray().length;  
  40.                 if(ndef.getMaxSize() < size) {  
  41.                     Toast.makeText(context, "nfcReadOnlyErrorTitle", Toast.LENGTH_SHORT ).show();  
  42.                     return false;  
  43.                 }  
  44.        
  45.                 try {  
  46.                     // Write the data to the tag  
  47.                     ndef.writeNdefMessage(message);  
  48.        
  49.                     Toast.makeText(context, "nfcWrittenTitle", Toast.LENGTH_SHORT ).show();  
  50.                     return true;  
  51.                 } catch (TagLostException tle) {  
  52.                      Toast.makeText(context, "nfcTagLostErrorTitle", Toast.LENGTH_SHORT ).show();  
  53.                     return false;  
  54.                 } catch (IOException ioe) {  
  55.                      Toast.makeText(context, "nfcFormattingErrorTitle", Toast.LENGTH_SHORT ).show();  
  56.                     return false;  
  57.                 } catch (FormatException fe) {  
  58.                      Toast.makeText(context, "nfcFormattingErrorTitle", Toast.LENGTH_SHORT ).show();  
  59.                     return false;  
  60.                 }  
  61.             // If the tag is not formatted, format it with the message  
  62.             } else {  
  63.                 NdefFormatable format = NdefFormatable.get(tag);  
  64.                 if(format != null) {  
  65.                     try {  
  66.                         format.connect();  
  67.                         format.format(message);  
  68.        
  69.                         Toast.makeText(context, "nfcWrittenTitle", Toast.LENGTH_SHORT ).show();  
  70.                         return true;  
  71.                     } catch (TagLostException tle) {  
  72.                          Toast.makeText(context, "nfcTagLostErrorTitle", Toast.LENGTH_SHORT ).show();  
  73.                         return false;  
  74.                     } catch (IOException ioe) {  
  75.                          Toast.makeText(context, "nfcFormattingErrorTitle", Toast.LENGTH_SHORT ).show();  
  76.                         return false;  
  77.                     } catch (FormatException fe) {  
  78.                          Toast.makeText(context, "nfcFormattingErrorTitle", Toast.LENGTH_SHORT ).show();  
  79.                         return false;  
  80.                     }  
  81.                 } else {  
  82.                      Toast.makeText(context, "nfcNoNdefErrorTitle", Toast.LENGTH_SHORT ).show();  
  83.                     return false;  
  84.                 }  
  85.             }  
  86.         } catch(Exception e) {  
  87.              Toast.makeText(context, "nfcUnknownErrorTitle", Toast.LENGTH_SHORT ).show();  
  88.         }  
  89.        
  90.         return false;  
  91.     }  
  92. }  
import java.io.IOException;
import java.nio.charset.Charset;

import android.content.Context;
import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.Tag;
import android.nfc.TagLostException;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.widget.Toast;

public class NFC {
	
	public static boolean writeTag(Context context, Tag tag) {    
		
	    NdefRecord relayRecord = new NdefRecord(
	    	    NdefRecord.TNF_ABSOLUTE_URI ,
	    	    "zoeice://com.zoeice.example/applicationUp".getBytes(Charset.forName("US-ASCII")),
	    	    new byte[0], new byte[0]);
	 
	    // Complete NDEF message with both records
	    NdefMessage message = new NdefMessage(new NdefRecord[] {relayRecord});
	 
	    try {
	        // If the tag is already formatted, just write the message to it
	        Ndef ndef = Ndef.get(tag);
	        if(ndef != null) {
	            ndef.connect();
	 
	            // Make sure the tag is writable
	            if(!ndef.isWritable()) {
	            	 Toast.makeText(context, "nfcReadOnlyErrorTitle", Toast.LENGTH_SHORT ).show();
	                return false;
	            }
	 
	            // Check if there's enough space on the tag for the message
	            int size = message.toByteArray().length;
	            if(ndef.getMaxSize() < size) {
	            	Toast.makeText(context, "nfcReadOnlyErrorTitle", Toast.LENGTH_SHORT ).show();
	                return false;
	            }
	 
	            try {
	                // Write the data to the tag
	                ndef.writeNdefMessage(message);
	 
	                Toast.makeText(context, "nfcWrittenTitle", Toast.LENGTH_SHORT ).show();
	                return true;
	            } catch (TagLostException tle) {
	            	 Toast.makeText(context, "nfcTagLostErrorTitle", Toast.LENGTH_SHORT ).show();
	                return false;
	            } catch (IOException ioe) {
	            	 Toast.makeText(context, "nfcFormattingErrorTitle", Toast.LENGTH_SHORT ).show();
	                return false;
	            } catch (FormatException fe) {
	            	 Toast.makeText(context, "nfcFormattingErrorTitle", Toast.LENGTH_SHORT ).show();
	                return false;
	            }
	        // If the tag is not formatted, format it with the message
	        } else {
	            NdefFormatable format = NdefFormatable.get(tag);
	            if(format != null) {
	                try {
	                    format.connect();
	                    format.format(message);
	 
	                    Toast.makeText(context, "nfcWrittenTitle", Toast.LENGTH_SHORT ).show();
	                    return true;
	                } catch (TagLostException tle) {
	                	 Toast.makeText(context, "nfcTagLostErrorTitle", Toast.LENGTH_SHORT ).show();
	                    return false;
	                } catch (IOException ioe) {
	                	 Toast.makeText(context, "nfcFormattingErrorTitle", Toast.LENGTH_SHORT ).show();
	                    return false;
	                } catch (FormatException fe) {
	                	 Toast.makeText(context, "nfcFormattingErrorTitle", Toast.LENGTH_SHORT ).show();
	                    return false;
	                }
	            } else {
	            	 Toast.makeText(context, "nfcNoNdefErrorTitle", Toast.LENGTH_SHORT ).show();
	                return false;
	            }
	        }
	    } catch(Exception e) {
	    	 Toast.makeText(context, "nfcUnknownErrorTitle", Toast.LENGTH_SHORT ).show();
	    }
	 
	    return false;
	}
}


然后再activity中重写:

  1. @Override  
  2.        protected void onNewIntent(Intent intent){  
  3.            if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())){  
  4.                mytag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  
  5.                NFC.writeTag(ctx, mytag);  
  6.                Toast.makeText(this"ok_detection" + mytag.toString(), Toast.LENGTH_LONG ).show();  
  7.            }  
  8.        }  
@Override
       protected void onNewIntent(Intent intent){
           if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())){
               mytag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
               NFC.writeTag(ctx, mytag);
               Toast.makeText(this, "ok_detection" + mytag.toString(), Toast.LENGTH_LONG ).show();
           }
       }

识别Tag的方法是在AndroidManifest.xml中加入:

  1. <activity android:name="com.carcon.navi.naviscreen.NaviViewManager"  android:launchMode="singleTask" >  
  2.            <intent-filter>  
  3.                   <action android:name="android.intent.action.MAIN"></action>  
  4.                      <category android:name="android.intent.category.LAUNCHER"></category>  
  5.               </intent-filter>  
  6.               <intent-filter>  
  7.                      <action android:name="android.nfc.action.NDEF_DISCOVERED" />  
  8.                      <category android:name="android.intent.category.DEFAULT" />  
  9.                      <data android:scheme="zoeice"  android:host="com.zoeice.example"  android:path="/applicationUp"/>  
  10.               </intent-filter>  
  11. </activity>       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值