NFC写Uri进Tag,与识别Tag

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

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中重写:

@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中加入:

<activity android:name="com.carcon.navi.naviscreen.NaviViewManager"  android:launchMode="singleTask" >
           <intent-filter>
                  <action android:name="android.intent.action.MAIN"></action>
                     <category android:name="android.intent.category.LAUNCHER"></category>
              </intent-filter>
              <intent-filter>
                     <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                     <category android:name="android.intent.category.DEFAULT" />
                     <data android:scheme="zoeice"  android:host="com.zoeice.example"  android:path="/applicationUp"/>
              </intent-filter>
</activity>     



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值