Android Open Source - NFCAndroid Nfc Tag






From Project

Back to project page NFCAndroid.

License

The source code is released under:

Copyright (c) 2012, Wireless Sensor Technologies, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following ...

If you think the Android project NFCAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.gototags.nfc;
//  w  w w.  ja v a  2  s.c  o m
import java.io.IOException;
import java.util.ArrayList;

import com.gototags.nfc.records.Record;

import android.content.Intent;
import android.nfc.*;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.os.Parcelable;

public final class NfcTag {
  private Tag tag;
  private byte[] id;
  private String tagTech;
  private boolean writable;
  private Integer maxSize;
  private Record[] records = new Record[0];
  
  NfcTag(Tag tag) {
    this.tag = tag;
  }
  
  public Tag getTag() {
    return tag;
  }
  
  public byte[] getId() {
    return this.id;
  }
  
  public String getIdString() {
    return Helper.toHex(this.getId());
  }
  
  public String getTagTech() {
    return this.tagTech;
  }
  
  public boolean isWritable() {
    return this.writable;
  }
  
  public Integer getMaxSize() {
    return this.maxSize;
  }
  
  public Record[] getRecords() {
    return this.records;
  }
  
  public void setRecords(Record[] records) {
    this.records = records;
  }
  
  public void setRecord(Record record) {
    this.records = new Record[]{ record };
  }
  
  private NdefMessage getNdefMessage() {
    ArrayList<NdefRecord> ndefRecords = new ArrayList<NdefRecord>(this.getRecords().length);
    
    for (Record record : this.getRecords()) {
      ndefRecords.add(record.getNdefRecord());
    }
    
    NdefMessage ndefMessage = new NdefMessage(ndefRecords.toArray(new NdefRecord[ndefRecords.size()]));
    
    return ndefMessage;
  }
  
  public boolean writeNdefMessage(boolean makeReadOnly) throws IOException, FormatException {
    
    NdefMessage ndefMessage = this.getNdefMessage();
    
    Ndef ndef = Ndef.get(this.getTag());
      
      if (ndef != null) {
          try {
              ndef.connect();

              if (!ndef.isWritable())
                return false;
              
            if (ndef.getMaxSize() < ndefMessage.toByteArray().length)
              return false;
            
            ndef.writeNdefMessage(ndefMessage);
            
            if (makeReadOnly)
              ndef.makeReadOnly();
            
            return true;
          } finally {
            if (ndef != null) {
              ndef.close();
              ndef = null;
            }
          }     
      } else {
        NdefFormatable ndefFormatable = NdefFormatable.get(this.getTag());
        
        if (ndefFormatable != null) {
          try {
            ndefFormatable.connect();
            ndefFormatable.format(ndefMessage);
            
            return true;
          } finally {
            if (ndefFormatable != null) {
              ndefFormatable.close();
              ndefFormatable = null;
            }
          }
        }
      }
      
      return false;
  }
  
  public static NfcTag fromIntent(Intent intent) throws IOException {
    if (intent == null) 
      return null;
    
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    
    if (tag == null) 
      return null;
    
    NfcTag nfcTag = new NfcTag(tag);
    nfcTag.id = tag.getId();
    
    Ndef ndef = Ndef.get(tag);
    
    if (ndef != null) {
      try {
              ndef.connect();

              nfcTag.writable = ndef.isWritable();
              nfcTag.maxSize = ndef.getMaxSize();
          } finally {
            if (ndef != null) {
              ndef.close();
              ndef = null;
            }
          } 
    }
    
    Parcelable[] parcelable = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    
    if (parcelable != null && parcelable.length == 1) {
      
      NdefMessage ndefMessage = (NdefMessage)parcelable[0];
      
      Record[] records = Record.fromNdefMessage(ndefMessage);
      nfcTag.setRecords(records);
    }
    
    return nfcTag;
  }
}




Java Source Code List

com.gototags.nfc.Helper.java
com.gototags.nfc.NfcTag.java
com.gototags.nfc.app.BaseActivity.java
com.gototags.nfc.app.MainActivity.java
com.gototags.nfc.ndef.ParsedNdefRecord.java
com.gototags.nfc.ndef.TextNdefRecord.java
com.gototags.nfc.ndef.UriNdefRecord.java
com.gototags.nfc.records.NdefRecordParser.java
com.gototags.nfc.records.PhoneRecord.java
com.gototags.nfc.records.RecordType.java
com.gototags.nfc.records.Record.java
com.gototags.nfc.records.TextRecord.java
com.gototags.nfc.records.WebsiteRecord.java